TAttributeMember.h
Go to the documentation of this file.
1 #ifndef icomp_TAttributeMember_included
2 #define icomp_TAttributeMember_included
3 
4 
5 // Qt includes
6 #include <QtCore/QCoreApplication>
7 
8 // ACF includes
9 #include <iattr/TAttribute.h>
14 
15 
16 namespace icomp
17 {
18 
19 
25 template <typename Attribute>
27 {
28 public:
29  typedef Attribute AttributeType;
30  typedef typename Attribute::ValueType AttributeValueType;
31  typedef void InterfaceType;
32 
34 
40  void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
41 
45  bool IsValid() const;
46 
50  const Attribute* GetAttributePtr() const;
55  const AttributeValueType& GetOriginalValue() const;
56 
60  const Attribute* operator->() const;
61 
66  const AttributeValueType& operator*() const;
67 
68 protected:
69  void SetAttribute(const Attribute* attributePtr);
70 
78  bool InitInternal( const IComponent* ownerPtr,
79  const IRealAttributeStaticInfo& staticInfo,
80  const IComponent** definitionComponentPtr);
81 
82 private:
83  const Attribute* m_attributePtr;
84  bool m_isAssigned;
85 };
86 
87 
88 // public methods
89 
90 template <typename Attribute>
92 : m_attributePtr(NULL),
93  m_isAssigned(false)
94 {
95 }
96 
97 
98 template <typename Attribute>
100  const IComponent* ownerPtr,
101  const IRealAttributeStaticInfo& staticInfo)
102 {
103  InitInternal(ownerPtr, staticInfo, NULL);
104 }
105 
106 template <typename Attribute>
108 {
109  Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
110 
111  return (m_attributePtr != NULL);
112 }
113 
114 
115 template <typename Attribute>
117 {
118  Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
119 
120  return m_attributePtr;
121 }
122 
123 
124 template <typename Attribute>
126 {
127  Q_ASSERT(m_attributePtr != NULL); // GetOriginalValue() was called for invalid object, or no IsValid() check was called.
128 
129  return m_attributePtr->GetValue();
130 }
131 
132 
133 template <typename Attribute>
135 {
136  Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
137 
138  return m_attributePtr;
139 }
140 
141 
142 template <typename Attribute>
144 {
145  Q_ASSERT(m_attributePtr != NULL); // operator* was called for invalid object, or no IsValid() check was called.
146 
147  return m_attributePtr->GetValue();
148 }
149 
150 
151 // protected methods
152 
153 template <typename Attribute>
154 void TAttributeMember<Attribute>::SetAttribute(const Attribute* attributePtr)
155 {
156  m_attributePtr = attributePtr;
157 }
158 
159 
160 template <typename Attribute>
162  const IComponent* ownerPtr,
163  const IRealAttributeStaticInfo& staticInfo,
164  const IComponent** definitionComponentPtr)
165 {
166  Q_ASSERT(ownerPtr != NULL);
167 
168  m_isAssigned = true;
169 
170  const QByteArray& attributeId = staticInfo.GetAttributeId();
171  const IComponentContext* componentContextPtr = ownerPtr->GetComponentContext();
172  if (componentContextPtr != NULL){
173  if (definitionComponentPtr != NULL){
174  int definitionLevel = -1;
175  const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, &definitionLevel);
176  if (attributePtr != NULL){
177  m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
178 
179  if (m_attributePtr != NULL){
180  Q_ASSERT(definitionLevel >= 0);
181 
182  while (definitionLevel > 0){
183  ownerPtr = ownerPtr->GetParentComponent();
184  Q_ASSERT(ownerPtr != NULL);
185 
186  --definitionLevel;
187  }
188 
189  *definitionComponentPtr = ownerPtr;
190 
191  return true;
192  }
193  else{
194  qCritical( "Component '%s': Attribute '%s' type inconsistence!",
195  CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
196  attributeId.constData());
197  }
198  }
199  }
200  else{
201  const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, NULL);
202  m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
203 
204  if (m_attributePtr == NULL){
205  if (attributePtr != NULL){
206  qCritical( "Component %s: Attribute %s exists in the component context but has a wrong type",
207  CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
208  attributeId.constData());
209  }
210  }
211 
212  return (m_attributePtr != NULL);
213  }
214  }
215  else{
216  qCritical( "Error during resolving of attribute: %s in component %s: Component context not set",
217  CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
218  attributeId.constData());
219 
220  m_attributePtr = NULL;
221  }
222 
223  return false;
224 }
225 
226 
227 // Translatable attribute
228 
230 {
231 public:
233 
235  {
236  }
237 
238  explicit CTextAttribute(const QString& value)
239  : BaseClass(value)
240  {
241  }
242 
244  {
247  };
248 
249  static QByteArray GetTypeName()
250  {
251  return "Text";
252  }
253 
254  // reimplemented (iser::IObject)
255  virtual QByteArray GetFactoryId() const
256  {
257  return GetTypeName();
258  }
259 };
260 
261 
262 class CTextAttributeMember: public TAttributeMember<CTextAttribute>
263 {
264 public:
266 
267  QString operator*() const
268  {
269  return QCoreApplication::translate("Attribute", BaseClass::operator*().toUtf8());
270  }
271 };
272 
273 
274 } // namespace icomp
275 
276 
277 #endif // !icomp_TAttributeMember_included
278 
279 
const AttributeValueType & operator*() const
Get value of attribute.
virtual QByteArray GetFactoryId() const
iattr::CStringAttribute BaseClass
const Attribute * operator->() const
Access to internal attribute pointer.
virtual const IComponentContext * GetComponentContext() const =0
Get access to component context describing all application-specified component information loaded fro...
CTextAttribute(const QString &value)
Main component interface.
Definition: IComponent.h:31
Provide session context of component.
Common interface for factorisable model objects.
Definition: IObject.h:19
virtual const ICompositeComponent * GetParentComponent(bool ownerOnly=false) const =0
Get parent of this component.
virtual const QByteArray & GetAttributeId() const =0
Get ID of this attribute.
Interface adding to attribute static info functionality existing only for real attributes.
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
Initialize this attribute.
const Attribute * GetAttributePtr() const
Access to internal attribute pointer.
Template implementation of single component attribute.
Definition: TAttribute.h:25
const AttributeValueType & GetOriginalValue() const
Get value of attribute.
#define NULL
Definition: istd.h:64
bool InitInternal(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo, const IComponent **definitionComponentPtr)
Internal initialize of attribute.
Pointer to component attribute.
virtual const iser::IObject * GetAttribute(const QByteArray &attributeId, int *definitionLevelPtr=NULL) const =0
Get attribute using its ID.
TAttributeMember< CTextAttribute > BaseClass
static QByteArray GetHierarchyAddress(const IComponentContext *contextPtr)
Get address of this component identifying it in component topology hierarchy.
void SetAttribute(const Attribute *attributePtr)
bool IsValid() const
Check if this attribute is valid.
Attribute::ValueType AttributeValueType
static QByteArray GetTypeName()

© 2007-2017 Witold Gantzke and Kirill Lepskiy