ICF 3.0.5.47
Technical documentation of ICF Libraries
TAttributeMember.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// Qt includes
10#include <QtCore/QCoreApplication>
11
12// ICF includes
13#include <iattr/TAttribute.h>
18
19
20namespace icomp
21{
22
23
29template <typename Attribute>
31{
32public:
33 typedef Attribute AttributeType;
34 typedef typename Attribute::ValueType AttributeValueType;
35 typedef void InterfaceType;
36
38
44 void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
45
49 bool IsValid() const;
50
54 const Attribute* GetAttributePtr() const;
60
64 const Attribute* operator->() const;
65
66 bool operator==(const Attribute* ptr) const;
67 bool operator!=(const Attribute* ptr) const;
73
74protected:
75 void SetAttribute(const Attribute* attributePtr);
76
84 bool InitInternal( const IComponent* ownerPtr,
85 const IRealAttributeStaticInfo& staticInfo,
86 const IComponent** definitionComponentPtr);
87
88private:
89 const Attribute* m_attributePtr;
90 bool m_isAssigned;
91};
92
93
94// public methods
95
96template <typename Attribute>
98: m_attributePtr(nullptr),
99 m_isAssigned(false)
100{
101}
102
103
104template <typename Attribute>
106 const IComponent* ownerPtr,
107 const IRealAttributeStaticInfo& staticInfo)
108{
109 InitInternal(ownerPtr, staticInfo, nullptr);
110}
111
112template <typename Attribute>
114{
115 Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
116
117 return (m_attributePtr != nullptr);
118}
119
120
121template <typename Attribute>
123{
124 Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
125
126 return m_attributePtr;
127}
128
129
130template <typename Attribute>
132{
133 Q_ASSERT(m_attributePtr != nullptr); // GetOriginalValue() was called for invalid object, or no IsValid() check was called.
134
135 return m_attributePtr->GetValue();
136}
137
138
139template <typename Attribute>
141{
142 Q_ASSERT_X(m_isAssigned, "Component initialization", "No I_ASSIGN used or attribute is used out of component context");
143
144 return m_attributePtr;
145}
146
147
148template <typename Attribute>
150{
151 Q_ASSERT(m_attributePtr != nullptr); // operator* was called for invalid object, or no IsValid() check was called.
152
153 return m_attributePtr->GetValue();
154}
155
156
157template <typename Attribute>
158bool TAttributeMember<Attribute>::operator==(const Attribute* ptr) const
159{
160 return m_attributePtr == ptr;
161}
162
163
164template <typename Attribute>
165bool TAttributeMember<Attribute>::operator!=(const Attribute* ptr) const
166{
167 return m_attributePtr != ptr;
168}
169
170
171// protected methods
172
173template <typename Attribute>
174void TAttributeMember<Attribute>::SetAttribute(const Attribute* attributePtr)
175{
176 m_attributePtr = attributePtr;
177}
178
179
180template <typename Attribute>
182 const IComponent* ownerPtr,
183 const IRealAttributeStaticInfo& staticInfo,
184 const IComponent** definitionComponentPtr)
185{
186 Q_ASSERT(ownerPtr != nullptr);
187
188 m_isAssigned = true;
189
190 const QByteArray& attributeId = staticInfo.GetAttributeId();
191 const IComponentContext* componentContextPtr = ownerPtr->GetComponentContext();
192 if (componentContextPtr != nullptr){
193 if (definitionComponentPtr != nullptr){
194 int definitionLevel = -1;
195 const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, &definitionLevel);
196 if (attributePtr != nullptr){
197 m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
198
199 if (m_attributePtr != nullptr){
200 Q_ASSERT(definitionLevel >= 0);
201
202 while (definitionLevel > 0){
203 ownerPtr = ownerPtr->GetParentComponent();
204 Q_ASSERT(ownerPtr != nullptr);
205
206 --definitionLevel;
207 }
208
209 *definitionComponentPtr = ownerPtr;
210
211 return true;
212 }
213 else{
214 qCritical( "Component '%s': Attribute '%s' type inconsistence!",
215 CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
216 attributeId.constData());
217 }
218 }
219 }
220 else{
221 const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, nullptr);
222 m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
223
224 if (m_attributePtr == nullptr){
225 if (attributePtr != nullptr){
226 qCritical( "Component %s: Attribute %s exists in the component context but has a wrong type",
227 CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
228 attributeId.constData());
229 }
230 }
231
232 return (m_attributePtr != nullptr);
233 }
234 }
235 else{
236 qCritical( "Error during resolving of attribute: %s in component %s: Component context not set",
237 CComponentContext::GetHierarchyAddress(componentContextPtr).constData(),
238 attributeId.constData());
239
240 m_attributePtr = nullptr;
241 }
242
243 return false;
244}
245
246
247// Translatable attribute
248
250{
251public:
253
255 {
256 }
257
258 explicit CTextAttribute(const QString& value)
259 : BaseClass(value)
260 {
261 }
262
268
269 static QByteArray GetTypeName()
270 {
271 return "Text";
272 }
273
274 // reimplemented (iser::IObject)
275 virtual QByteArray GetFactoryId() const
276 {
277 return GetTypeName();
278 }
279};
280
281
282class CTextAttributeMember: public TAttributeMember<CTextAttribute>
283{
284public:
286
287 QString operator*() const
288 {
289 return QCoreApplication::translate("Attribute", BaseClass::operator*().toUtf8());
290 }
291};
292
293
294} // namespace icomp
295
296
Template implementation of single component attribute.
Definition TAttribute.h:33
static QByteArray GetHierarchyAddress(const IComponentContext *contextPtr)
Get address of this component identifying it in component topology hierarchy.
CTextAttribute(const QString &value)
static QByteArray GetTypeName()
virtual QByteArray GetFactoryId() const
iattr::CStringAttribute BaseClass
TAttributeMember< CTextAttribute > BaseClass
@ AF_TRANSLATABLE
Attribute is able to translate.
Provide session context of component.
virtual const iser::IObject * GetAttribute(const QByteArray &attributeId, int *definitionLevelPtr=nullptr) const =0
Get attribute using its ID.
Main component interface.
Definition IComponent.h:35
virtual const IComponentContext * GetComponentContext() const =0
Get access to component context describing all application-specified component information loaded fro...
virtual const ICompositeComponent * GetParentComponent(bool ownerOnly=false) const =0
Get parent of this component.
Interface adding to attribute static info functionality existing only for real attributes.
virtual const QByteArray & GetAttributeId() const =0
Get ID of this attribute.
Pointer to component attribute.
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
Initialize this attribute.
bool IsValid() const
Check if this attribute is valid.
Attribute::ValueType AttributeValueType
bool operator==(const Attribute *ptr) const
const AttributeValueType & operator*() const
Get value of attribute.
const Attribute * operator->() const
Access to internal attribute pointer.
void SetAttribute(const Attribute *attributePtr)
bool operator!=(const Attribute *ptr) const
bool InitInternal(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo, const IComponent **definitionComponentPtr)
Internal initialize of attribute.
const AttributeValueType & GetOriginalValue() const
Get value of attribute.
const Attribute * GetAttributePtr() const
Access to internal attribute pointer.
Common interface for factorisable model objects.
Definition IObject.h:25
Package with interfaces and class used for components concept.

© Witold Gantzke and Kirill Lepskiy