ICF 3.1.1.10
Technical documentation of ICF Libraries
TFactoryMember.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// ICF includes
10#include <istd/TIFactory.h>
14
15
16namespace icomp
17{
18
19
24template <class Interface>
26 public TAttributeMember<CFactoryAttribute>,
28 virtual public istd::TIFactory<Interface>
29{
30public:
33 typedef Interface InterfaceType;
35
37
38 void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
39
44 bool IsValid() const;
45
49 std::unique_ptr<IComponent> CreateComponent() const;
50
62 static Interface* ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId = "");
63
64 // reimplemented (istd::TIFactory)
65 virtual std::unique_ptr<Interface> CreateInstance(const QByteArray& keyId = "") const;
66
67 // reimplemented (istd::IFactoryInfo)
68 virtual KeyList GetFactoryKeys() const;
69
70protected:
72
73private:
74 const IComponent* m_definitionComponentPtr;
75};
76
77
78// public methods
79
80template <class Interface>
82: m_definitionComponentPtr(nullptr)
83{
84}
85
86
87template <class Interface>
89{
90 BaseClass::InitInternal(ownerPtr, staticInfo, &m_definitionComponentPtr);
91}
92
93
94template <class Interface>
96{
97 return (m_definitionComponentPtr != nullptr) && BaseClass::IsValid();
98}
99
100
101
102template <class Interface>
103std::unique_ptr<IComponent> TFactoryMember<Interface>::CreateComponent() const
104{
105 if ((m_definitionComponentPtr != nullptr) && BaseClass::IsValid()){
106 const ICompositeComponent* parentPtr = m_definitionComponentPtr->GetParentComponent();
107 if (parentPtr != nullptr){
108 const QByteArray& componentId = BaseClass::operator*();
109
110 QByteArray baseId;
111 QByteArray subId;
112 BaseClass2::SplitId(componentId, baseId, subId);
113 Q_ASSERT(subId.isEmpty()); // explicit subelement ID are not implemented correctly
114
115 return parentPtr->CreateSubcomponent(baseId);
116 }
117 else{
118 qCritical("Component %s is defined, but definition component has no parent", BaseClass::operator*().constData());
119 }
120 }
121
122 return nullptr;
123}
124
125
126template <class Interface>
127std::unique_ptr<Interface> TFactoryMember<Interface>::CreateInstance(const QByteArray& /*keyId*/) const
128{
129 auto newComponentPtr = CreateComponent();
130 if (newComponentPtr != nullptr){
131 Interface* retVal = BaseClass2::ExtractInterface<Interface>(newComponentPtr.get());
132 if (retVal != nullptr){
133 newComponentPtr.release();
134
135 return std::unique_ptr<Interface>{retVal};
136 }
137 }
138
139 return nullptr;
140}
141
142
143// reimplemented (istd::IFactoryInfo)
144
145template <class Interface>
147{
148 static KeyList defaultList;
149 if (defaultList.isEmpty()){
150 defaultList << "Component";
151 }
152
153 return defaultList;
154}
155
156
157// static methods
158
159template <class Interface>
160Interface* TFactoryMember<Interface>::ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId)
161{
162 if (instancePtr != nullptr){
163 if (auto* componentPtr = dynamic_cast<IComponent*>(instancePtr)) {
164 return BaseClass2::ExtractInterface<Interface>(componentPtr, subId);
165 }
166
167 Q_UNREACHABLE(); // Only objects returned by \b CreateComponent should be used as input, so this line cannot be run.
168 }
169
170 return nullptr;
171}
172
173
174// protected methods
175
176template <class Interface>
178: BaseClass(ptr),
179 m_definitionComponentPtr(ptr.m_definitionComponentPtr)
180{
181}
182
183
184} // namespace icomp
185
186
Main component interface.
Definition IComponent.h:35
virtual const ICompositeComponent * GetParentComponent(bool ownerOnly=false) const =0
Get parent of this component.
Composite component interface.
virtual std::unique_ptr< IComponent > 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.
virtual std::unique_ptr< Interface > CreateInstance(const QByteArray &keyId="") const
Create an instance of the object, mapped to the keyId keyId.
TAttributeMember< CFactoryAttribute > BaseClass
static Interface * ExtractInterface(istd::IPolymorphic *instancePtr, const QByteArray &subId="")
Extract interface from some component.
bool IsValid() const
Check if this factory can be resolved.
virtual KeyList GetFactoryKeys() const
Returns all posible keys for this factory.
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
CInterfaceManipBase BaseClass2
std::unique_ptr< IComponent > CreateComponent() const
Create component without extracting any interface.
istd::IFactoryInfo::KeyList KeyList
QSet< QByteArray > KeyList
Generic interface for a factory.
Definition TIFactory.h:22
Package with interfaces and class used for components concept.

© Witold Gantzke and Kirill Lepskiy