ACF $AcfVersion:0$
TAttributeMember.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QCoreApplication>
7
8// ACF includes
9#include <iattr/TAttribute.h>
14
15
16namespace icomp
17{
18
19
25template <typename Attribute>
27{
28public:
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;
56
60 const Attribute* operator->() const;
61
67
68protected:
69 void SetAttribute(const Attribute* attributePtr);
70
78 bool InitInternal( const IComponent* ownerPtr,
79 const IRealAttributeStaticInfo& staticInfo,
80 const IComponent** definitionComponentPtr);
81
82private:
83 const Attribute* m_attributePtr;
84 bool m_isAssigned;
85};
86
87
88// public methods
89
90template <typename Attribute>
92: m_attributePtr(nullptr),
93 m_isAssigned(false)
94{
95}
96
97
98template <typename Attribute>
100 const IComponent* ownerPtr,
101 const IRealAttributeStaticInfo& staticInfo)
102{
103 InitInternal(ownerPtr, staticInfo, nullptr);
104}
105
106template <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 != nullptr);
112}
113
114
115template <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
124template <typename Attribute>
126{
127 Q_ASSERT(m_attributePtr != nullptr); // GetOriginalValue() was called for invalid object, or no IsValid() check was called.
128
129 return m_attributePtr->GetValue();
130}
131
132
133template <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
142template <typename Attribute>
144{
145 Q_ASSERT(m_attributePtr != nullptr); // 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
153template <typename Attribute>
154void TAttributeMember<Attribute>::SetAttribute(const Attribute* attributePtr)
155{
156 m_attributePtr = attributePtr;
157}
158
159
160template <typename Attribute>
162 const IComponent* ownerPtr,
163 const IRealAttributeStaticInfo& staticInfo,
164 const IComponent** definitionComponentPtr)
165{
166 Q_ASSERT(ownerPtr != nullptr);
167
168 m_isAssigned = true;
169
170 const QByteArray& attributeId = staticInfo.GetAttributeId();
171 IComponentContextSharedPtr componentContextPtr = ownerPtr->GetComponentContext();
172 if (componentContextPtr != nullptr){
173 if (definitionComponentPtr != nullptr){
174 int definitionLevel = -1;
175 const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, &definitionLevel);
176 if (attributePtr != nullptr){
177 m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
178
179 if (m_attributePtr != nullptr){
180 Q_ASSERT(definitionLevel >= 0);
181
182 while (definitionLevel > 0 && (ownerPtr != nullptr)){
183 ownerPtr = ownerPtr->GetParentComponent();
184 // Q_ASSERT(ownerPtr != nullptr);
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.get()).constData(),
196 attributeId.constData());
197 }
198 }
199 }
200 else{
201 const iser::IObject* attributePtr = componentContextPtr->GetAttribute(attributeId, nullptr);
202 m_attributePtr = dynamic_cast<const Attribute*>(attributePtr);
203
204 if (m_attributePtr == nullptr){
205 if (attributePtr != nullptr){
206 qCritical( "Component %s: Attribute %s exists in the component context but has a wrong type",
207 CComponentContext::GetHierarchyAddress(componentContextPtr.get()).constData(),
208 attributeId.constData());
209 }
210 }
211
212 return (m_attributePtr != nullptr);
213 }
214 }
215 else{
216 qCritical( "Error during resolving of attribute: %s in component %s: Component context not set",
217 CComponentContext::GetHierarchyAddress(componentContextPtr.get()).constData(),
218 attributeId.constData());
219
220 m_attributePtr = nullptr;
221 }
222
223 return false;
224}
225
226
227// Translatable attribute
228
230{
231public:
233
235 {
236 }
237
238 explicit CTextAttribute(const QString& value)
239 : BaseClass(value)
240 {
241 }
242
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
262class CTextAttributeMember: public TAttributeMember<CTextAttribute>
263{
264public:
266
267 QString operator*() const
268 {
269 return QCoreApplication::translate("Attribute", BaseClass::operator*().toUtf8());
270 }
271};
272
273
274} // namespace icomp
275
276
Template implementation of single component attribute.
Definition TAttribute.h:29
static QByteArray GetHierarchyAddress(const IComponentContext *contextPtr)
Get address of this component identifying it in component topology hierarchy.
CTextAttribute(const QString &value)
iattr::CStringAttribute BaseClass
static QByteArray GetTypeName()
virtual QByteArray GetFactoryId() const
TAttributeMember< CTextAttribute > BaseClass
@ AF_TRANSLATABLE
Attribute is able to translate.
Main component interface.
Definition IComponent.h:32
virtual const icomp::IComponent * GetParentComponent(bool ownerOnly=false) const =0
Get parent of this component.
virtual IComponentContextSharedPtr GetComponentContext() const =0
Get access to component context describing all application-specified component information loaded fro...
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.
const AttributeValueType & operator*() const
Get value of attribute.
const Attribute * operator->() const
Access to internal attribute pointer.
void SetAttribute(const Attribute *attributePtr)
Attribute::ValueType AttributeValueType
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:23
Package with interfaces and class used for components concept.
std::shared_ptr< icomp::IComponentContext > IComponentContextSharedPtr