ACF $AcfVersion:0$
TFactoryMember.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
7#include <istd/TDelPtr.h>
8
12
13
14namespace icomp
15{
16
17
22template <class Interface>
24 public TAttributeMember<CFactoryAttribute>,
26 virtual public istd::TInterfaceFactory<Interface>
27{
28public:
31 typedef Interface InterfaceType;
33
35
36 void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
37
42 bool IsValid() const;
43
48
60 static Interface* ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId = "");
61
62 // reimplemented (TInterfaceFactory)
63 virtual istd::TUniqueInterfacePtr<Interface> CreateInstance(const QByteArray& typeId = QByteArray()) const override;
64
65 // reimplemented (istd::IFactoryInfo)
66 virtual KeyList GetFactoryKeys() const override;
67
68protected:
70
71private:
72 const IComponent* m_definitionComponentPtr;
73};
74
75
76// public methods
77
78template <class Interface>
80: m_definitionComponentPtr(NULL)
81{
82}
83
84
85template <class Interface>
87{
88 BaseClass::InitInternal(ownerPtr, staticInfo, &m_definitionComponentPtr);
89}
90
91
92template <class Interface>
94{
95 return (m_definitionComponentPtr != NULL) && BaseClass::IsValid();
96}
97
98
99
100template <class Interface>
102{
103 if ((m_definitionComponentPtr != NULL) && BaseClass::IsValid()){
104 const ICompositeComponent* parentPtr = dynamic_cast<const ICompositeComponent*>(m_definitionComponentPtr->GetParentComponent());
105 if (parentPtr != NULL){
106 const QByteArray& componentId = BaseClass::operator*();
107
108 QByteArray baseId;
109 QByteArray subId;
110 BaseClass2::SplitId(componentId, baseId, subId);
111 Q_ASSERT(subId.isEmpty()); // explicit subelement ID are not implemented correctly
112
113 return parentPtr->CreateSubcomponent(baseId);
114 }
115 else{
116 qCritical("Component %s is defined, but definition component has no parent", BaseClass::operator*().constData());
117 }
118 }
119
120 return nullptr;
121}
122
123
124template <class Interface>
126{
127 IComponentUniquePtr newComponentPtr = CreateComponent();
128 if (newComponentPtr != nullptr){
129 Interface* retVal = BaseClass2::ExtractInterface<Interface>(newComponentPtr.get());
130 if (retVal != NULL) {
131 return istd::TUniqueInterfacePtr<Interface>(newComponentPtr.release(), retVal);
132 }
133 }
134
136}
137
138
139// reimplemented (istd::IFactoryInfo)
140
141template <class Interface>
143{
144 static KeyList defaultList;
145 if (defaultList.isEmpty()){
146 defaultList << "Component";
147 }
148
149 return defaultList;
150}
151
152
153// static methods
154
155template <class Interface>
156Interface* TFactoryMember<Interface>::ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId)
157{
158 if (instancePtr != NULL){
159 icomp::IComponent* componentPtr = dynamic_cast<icomp::IComponent*>(instancePtr);
160 Q_ASSERT(componentPtr != NULL); // Only objects returned by \b CreateComponent should be used as input
161
162 if (componentPtr != NULL){
163 return BaseClass2::ExtractInterface<Interface>(componentPtr, subId);
164 }
165 }
166
167 return NULL;
168}
169
170
171// protected methods
172
173template <class Interface>
175: BaseClass(ptr),
176 m_definitionComponentPtr(ptr.m_definitionComponentPtr)
177{
178}
179
180
181} // namespace icomp
182
183
Main component interface.
Definition IComponent.h:32
Composite component interface.
virtual IComponentUniquePtr CreateSubcomponent(const QByteArray &componentId) const =0
Create instance of subcomponent using its ID.
Interface adding to attribute static info functionality existing only for real attributes.
Pointer to component attribute.
Factory of components used as component member.
TAttributeMember< CFactoryAttribute > BaseClass
istd::IFactoryInfo::KeyList KeyList
IComponentUniquePtr CreateComponent() const
Create component without extracting any interface.
static Interface * ExtractInterface(istd::IPolymorphic *instancePtr, const QByteArray &subId="")
Extract interface from some component.
bool IsValid() const
Check if this factory can be resolved.
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
CInterfaceManipBase BaseClass2
virtual istd::TUniqueInterfacePtr< Interface > CreateInstance(const QByteArray &typeId=QByteArray()) const override
virtual KeyList GetFactoryKeys() const override
Returns all possible keys for this factory.
QSet< QByteArray > KeyList
Base interface for all used interfaces and implementations.
Unique ownership smart pointer for interface types.
#define NULL
Definition istd.h:74
Package with interfaces and class used for components concept.
std::unique_ptr< IComponent > IComponentUniquePtr
Definition IComponent.h:72