ACF $AcfVersion:0$
TSimComponentWrap.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// ACF includes
6#include <istd/CClassInfo.h>
10
11
12namespace icomp
13{
14
15
22template <class Base>
24 public TComponentWrap<Base>,
25 virtual public ICompositeComponent
26{
27public:
29
31
33 {
34 m_contextPtr.reset();
35 }
36
40 void InitComponent();
41
47 bool SetAttr(const QByteArray& attributeId, const iser::IObject* attributePtr)
48 {
49 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
50
51 return implPtr->SetAttr(attributeId, attributePtr);
52 }
53
57 bool SetRef(const QByteArray& referenceId, IComponentSharedPtr componentPtr, const QByteArray& subelementId = "")
58 {
59 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
60
61 return implPtr->SetRef(referenceId, componentPtr, subelementId);
62 }
63
67 bool InsertMultiRef(const QByteArray& referenceId, IComponentSharedPtr componentPtr, const QByteArray& subelementId = "")
68 {
69 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
70
71 return implPtr->InsertMultiRef(referenceId, componentPtr, subelementId);
72 }
73
77 bool SetFactory(const QByteArray& factoryId, const CSimComponentContextBase::ComponentsFactory* factoryPtr)
78 {
79 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
80
81 return implPtr->SetFactory(factoryId, factoryPtr);
82 }
83
87 bool InsertMultiFactory(const QByteArray& factoryId, const CSimComponentContextBase::ComponentsFactory* factoryPtr)
88 {
89 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
90
91 return implPtr->InsertMultiFactory(factoryId, factoryPtr);
92 }
93
97 bool SetBoolAttr(const QByteArray& attributeId, bool value)
98 {
99 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
100
101 return implPtr->SetBoolAttr(attributeId, value);
102 }
103
107 bool SetIntAttr(const QByteArray& attributeId, int value)
108 {
109 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
110
111 return implPtr->SetIntAttr(attributeId, value);
112 }
113
117 bool SetDoubleAttr(const QByteArray& attributeId, double value)
118 {
119 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
120
121 return implPtr->SetDoubleAttr(attributeId, value);
122 }
123
127 bool SetStringAttr(const QByteArray& attributeId, const QString& value)
128 {
129 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
130
131 return implPtr->SetStringAttr(attributeId, value);
132 }
133
137 bool SetIdAttr(const QByteArray& attributeId, const QByteArray& value)
138 {
139 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
140
141 return implPtr->SetIdAttr(attributeId, value);
142 }
143
149 template <class Attribute>
150 bool InsertMultiAttr(const QByteArray& attributeId, const Attribute& attribute)
151 {
152 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
153
154 return implPtr->InsertMultiAttr(attributeId, attribute);
155 }
156
157 bool InsertMultiAttr(const QByteArray& attributeId, const QString& attribute)
158 {
159 SimulationContext* implPtr = dynamic_cast<SimulationContext*>(m_contextPtr.get());
160
161 return implPtr->InsertMultiAttr(attributeId, attribute);
162 }
163
164
165 // reimplemented (icomp::ICompositeComponent)
166 virtual IComponentSharedPtr GetSubcomponent(const QByteArray& componentId) const override;
167 virtual IComponentContextSharedPtr GetSubcomponentContext(const QByteArray& componentId) const override;
168 virtual IComponentUniquePtr CreateSubcomponent(const QByteArray& componentId) const override;
169 virtual void OnSubcomponentDeleted(const IComponent* subcomponentPtr) override;
170
171 // reimplemented (icomp::IComponent)
172 virtual const IComponent* GetParentComponent(bool ownerOnly = false) const override;
173
174protected:
176 {
177 public:
180 {
181 }
182
184 {
185 }
186
187 // reimplemented (icomp::IComponentContext)
188 virtual const QByteArray& GetContextId() const override
189 {
190 static QByteArray id = istd::CClassInfo::GetName<Base>();
191
192 return id;
193 }
194
195 IComponentSharedPtr GetSubcomponent(const QByteArray& componentId) const
196 {
197 ComponentsMap::ConstIterator iter = m_componentsMap.constFind(componentId);
198 if (iter != m_componentsMap.constEnd()){
199 return iter.value();
200 }
201
202 return IComponentSharedPtr();
203 }
204
205 IComponentUniquePtr CreateSubcomponent(const QByteArray& componentId) const
206 {
207 FactoriesMap::ConstIterator iter = m_factoriesMap.constFind(componentId);
208 if (iter != m_factoriesMap.constEnd()){
209 Q_ASSERT(iter.value() != nullptr);
210
211 return IComponentUniquePtr(iter.value()->CreateInstance().PopPtr());
212 }
213
214 return nullptr;
215 }
216 };
217
220};
221
222
223// public methods
224
225template <class Base>
227 :BaseClass()
228{
229 m_contextPtr.reset(new SimulationContext(&BaseClass::GetComponentStaticInfo()));
230}
231
232
233template <class Base>
235{
236 SetComponentContext(m_contextPtr, NULL, false);
237}
238
239
240// reimplemented (icomp::ICompositeComponent)
241
242template <class Base>
244{
245 const SimulationContext* implPtr = dynamic_cast<const SimulationContext*>(m_contextPtr.get());
246
247 return implPtr->GetSubcomponent(componentId);
248}
249
250
251template <class Base>
256
257
258template <class Base>
260{
261 const SimulationContext* implPtr = dynamic_cast<const SimulationContext*>(m_contextPtr.get());
262
263 return implPtr->CreateSubcomponent(componentId);
264}
265
266
267template <class Base>
269{
270 qFatal("Simulated component cannot have sub-components");
271}
272
273
274// reimplemented (icomp::IComponent)
275
276template <class Base>
278{
279 if (ownerOnly){
280 return NULL;
281 }
282 else{
283 return const_cast<TSimComponentWrap<Base>* >(this);
284 }
285}
286
287
288template <class Base>
290{
291public:
293
295 {
296 Impl* ptr = new Impl;
297
298 reset(ptr);
299 }
300
301 const Impl& GetImpl() const
302 {
303 return *dynamic_cast<Impl*>(get());
304 }
305
307 {
308 return *dynamic_cast<Impl*>(get());
309 }
310
312 {
313 return dynamic_cast<Impl*>(get());
314 }
315
317 {
318 return *dynamic_cast<Impl*>(get());
319 }
320};
321
322
323} // namespace icomp
324
325
bool SetIdAttr(const QByteArray &attributeId, const QByteArray &value)
Set instance of QByteArray attribute.
bool InsertMultiAttr(const QByteArray &attributeId, const Attribute &attribute)
Insert new attribute to multi attributes.
bool SetFactory(const QByteArray &factoryId, const ComponentsFactory *factoryPtr)
Set factory of component instance.
bool SetBoolAttr(const QByteArray &attributeId, bool value)
Set instance of bool attribute.
bool SetAttr(const QByteArray &attributeId, const iser::IObject *attributePtr)
Set named attribute.
bool SetStringAttr(const QByteArray &attributeId, const QString &value)
Set instance of QString attribute.
bool InsertMultiFactory(const QByteArray &factoryId, const ComponentsFactory *factoryPtr)
Insert new factory instance into multi-factory attribute.
bool SetDoubleAttr(const QByteArray &attributeId, double value)
Set instance of double attribute.
bool SetIntAttr(const QByteArray &attributeId, int value)
Set instance of int attribute.
bool SetRef(const QByteArray &referenceId, IComponentSharedPtr componentPtr, const QByteArray &subelementId="")
Set named reference to some component.
bool InsertMultiRef(const QByteArray &referenceId, IComponentSharedPtr componentPtr, const QByteArray &subelementId="")
Set named reference to some component.
Main component interface.
Definition IComponent.h:32
This interface provide static information about component meta info.
Composite component interface.
Wrapper of end component implementation used to correct control of component life-cycle.
IComponentUniquePtr CreateSubcomponent(const QByteArray &componentId) const
virtual const QByteArray & GetContextId() const override
Get ID of this component in the context tree.
IComponentSharedPtr GetSubcomponent(const QByteArray &componentId) const
SimulationContext(const IComponentStaticInfo *infoPtr)
Simulation wrapper of component.
IComponentSharedPtr m_componentPtr
bool SetFactory(const QByteArray &factoryId, const CSimComponentContextBase::ComponentsFactory *factoryPtr)
Set factory of component instance.
virtual IComponentContextSharedPtr GetSubcomponentContext(const QByteArray &componentId) const override
Get access to context of subcomponent using its ID.
IComponentContextSharedPtr m_contextPtr
TComponentWrap< Base > BaseClass
bool SetBoolAttr(const QByteArray &attributeId, bool value)
Set instance of bool attribute.
virtual IComponentUniquePtr CreateSubcomponent(const QByteArray &componentId) const override
Create instance of subcomponent using its ID.
bool SetAttr(const QByteArray &attributeId, const iser::IObject *attributePtr)
Set named attribute.
bool SetRef(const QByteArray &referenceId, IComponentSharedPtr componentPtr, const QByteArray &subelementId="")
Set named reference to some component.
bool InsertMultiAttr(const QByteArray &attributeId, const QString &attribute)
void InitComponent()
Initialilze component after setting all its attributes and references.
virtual const IComponent * GetParentComponent(bool ownerOnly=false) const override
Get parent of this component.
bool InsertMultiAttr(const QByteArray &attributeId, const Attribute &attribute)
Insert new attribute to multi attributes.
bool SetIdAttr(const QByteArray &attributeId, const QByteArray &value)
Set instance of QByteArray attribute.
bool SetIntAttr(const QByteArray &attributeId, int value)
Set instance of int attribute.
virtual void OnSubcomponentDeleted(const IComponent *subcomponentPtr) override
Called if subcomponent is removed from memory.
bool SetDoubleAttr(const QByteArray &attributeId, double value)
Set instance of double attribute.
bool SetStringAttr(const QByteArray &attributeId, const QString &value)
Set instance of QString attribute.
virtual IComponentSharedPtr GetSubcomponent(const QByteArray &componentId) const override
Get access to subcomponent using its ID.
bool InsertMultiRef(const QByteArray &referenceId, IComponentSharedPtr componentPtr, const QByteArray &subelementId="")
Set named reference to some component.
bool InsertMultiFactory(const QByteArray &factoryId, const CSimComponentContextBase::ComponentsFactory *factoryPtr)
Insert new factory instance into multi-factory attribute.
icomp::TSimComponentWrap< Base > Impl
Common interface for factorisable model objects.
Definition IObject.h:23
#define NULL
Definition istd.h:74
Package with interfaces and class used for components concept.
std::unique_ptr< IComponent > IComponentUniquePtr
Definition IComponent.h:72
std::shared_ptr< icomp::IComponentContext > IComponentContextSharedPtr
std::shared_ptr< IComponent > IComponentSharedPtr
Definition IComponent.h:71