ICF 3.0.5.47
Technical documentation of ICF Libraries
TMultiFactoryMember.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
13
14
15namespace icomp
16{
17
18
23template <class Interface>
24class TMultiFactoryMember: public TMultiAttributeMember<CMultiFactoryAttribute>, public CInterfaceManipBase
25{
26public:
29 typedef Interface InterfaceType;
30
32
33 void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
34
39 bool IsValid() const;
40
44 std::unique_ptr<IComponent> CreateComponent(int index) const;
45
51 std::unique_ptr<Interface> CreateInstance(int index) const;
52
63 static Interface* ExtractInterface(IComponent* componentPtr, const QByteArray& subId = "");
64
65protected:
67
68private:
69 const IComponent* m_definitionComponentPtr;
70};
71
72
73// public methods
74
75template <class Interface>
77: m_definitionComponentPtr(nullptr)
78{
79}
80
81
82template <class Interface>
84{
85 BaseClass::InitInternal(ownerPtr, staticInfo, &m_definitionComponentPtr);
86}
87
88
89template <class Interface>
91{
92 return (m_definitionComponentPtr != nullptr) && BaseClass::IsValid();
93}
94
95
96
97template <class Interface>
98std::unique_ptr<IComponent> TMultiFactoryMember<Interface>::CreateComponent(int index) const
99{
100 if ((m_definitionComponentPtr != nullptr) && BaseClass::IsValid()){
101 const ICompositeComponent* parentPtr = m_definitionComponentPtr->GetParentComponent();
102 if (parentPtr != nullptr){
103 const QByteArray& componentId = BaseClass::operator[](index);
104
105 QByteArray baseId;
106 QByteArray subId;
107 BaseClass2::SplitId(componentId, baseId, subId);
108 Q_ASSERT(subId.isEmpty()); // explicit subelement ID are not implemented correctly
109
110 return parentPtr->CreateSubcomponent(baseId);
111 }
112 else{
113 qCritical("Component %s is defined, but definition component has no parent", BaseClass::operator[](index).constData());
114 }
115 }
116
117 return nullptr;
118}
119
120
121template <class Interface>
122std::unique_ptr<Interface> TMultiFactoryMember<Interface>::CreateInstance(int index) const
123{
124 auto newComponentPtr = CreateComponent(index);
125 if (newComponentPtr != nullptr){
126 Interface* retVal = BaseClass2::ExtractInterface<Interface>(newComponentPtr.get());
127 if (retVal != nullptr){
128 newComponentPtr.release();
129
130 return std::unique_ptr<Interface>{retVal};
131 }
132 }
133
134 return nullptr;
135}
136
137
138// static methods
139
140template <class Interface>
141Interface* TMultiFactoryMember<Interface>::ExtractInterface(IComponent* componentPtr, const QByteArray& subId)
142{
143 return BaseClass2::ExtractInterface<Interface>(componentPtr, subId);
144}
145
146
147// protected methods
148
149template <class Interface>
151: BaseClass(ptr),
152 m_definitionComponentPtr(ptr.m_definitionComponentPtr)
153{
154}
155
156
157} // namespace icomp
158
159
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.
bool IsValid() const
Check if this factory can be resolved.
std::unique_ptr< IComponent > CreateComponent(int index) const
Create component for specified index without extracting any interface.
std::unique_ptr< Interface > CreateInstance(int index) const
Create instance of interface for specified index.
TMultiAttributeMember< CMultiFactoryAttribute > BaseClass
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
static Interface * ExtractInterface(IComponent *componentPtr, const QByteArray &subId="")
Extract interface from some component.
Package with interfaces and class used for components concept.

© Witold Gantzke and Kirill Lepskiy