TFactoryMember.h
Go to the documentation of this file.
1 #ifndef icomp_TFactoryMember_included
2 #define icomp_TFactoryMember_included
3 
4 
5 // ACF includes
6 #include <istd/TIFactory.h>
7 #include <istd/TDelPtr.h>
8 
12 
13 
14 namespace icomp
15 {
16 
17 
22 template <class Interface>
24  public TAttributeMember<CFactoryAttribute>,
25  public CInterfaceManipBase,
26  virtual public istd::TIFactory<Interface>
27 {
28 public:
31  typedef Interface InterfaceType;
33 
35 
36  void Init(const IComponent* ownerPtr, const IRealAttributeStaticInfo& staticInfo);
37 
42  bool IsValid() const;
43 
47  IComponent* CreateComponent() const;
48 
60  static Interface* ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId = "");
61 
62  // reimplemented (istd::TIFactory)
63  virtual Interface* CreateInstance(const QByteArray& keyId = "") const;
64 
65  // reimplemented (istd::IFactoryInfo)
66  virtual KeyList GetFactoryKeys() const;
67 
68 protected:
69  TFactoryMember(const TFactoryMember& ptr);
70 
71 private:
72  const IComponent* m_definitionComponentPtr;
73 };
74 
75 
76 // public methods
77 
78 template <class Interface>
80 : m_definitionComponentPtr(NULL)
81 {
82 }
83 
84 
85 template <class Interface>
87 {
88  BaseClass::InitInternal(ownerPtr, staticInfo, &m_definitionComponentPtr);
89 }
90 
91 
92 template <class Interface>
94 {
95  return (m_definitionComponentPtr != NULL) && BaseClass::IsValid();
96 }
97 
98 
99 
100 template <class Interface>
102 {
103  if ((m_definitionComponentPtr != NULL) && BaseClass::IsValid()){
104  const ICompositeComponent* parentPtr = 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 NULL;
121 }
122 
123 
124 template <class Interface>
125 Interface* TFactoryMember<Interface>::CreateInstance(const QByteArray& /*keyId*/) const
126 {
127  istd::TDelPtr<IComponent> newComponentPtr(CreateComponent());
128  if (newComponentPtr.IsValid()){
129  Interface* retVal = BaseClass2::ExtractInterface<Interface>(newComponentPtr.GetPtr());
130  if (retVal != NULL){
131  newComponentPtr.PopPtr();
132 
133  return retVal;
134  }
135  }
136 
137  return NULL;
138 }
139 
140 
141 // reimplemented (istd::IFactoryInfo)
142 
143 template <class Interface>
145 {
146  static KeyList defaultList;
147  if (defaultList.isEmpty()){
148  defaultList << "Component";
149  }
150 
151  return defaultList;
152 }
153 
154 
155 // static methods
156 
157 template <class Interface>
158 Interface* TFactoryMember<Interface>::ExtractInterface(istd::IPolymorphic* instancePtr, const QByteArray& subId)
159 {
160  if (instancePtr != NULL){
161  icomp::IComponent* componentPtr = dynamic_cast<icomp::IComponent*>(instancePtr);
162  Q_ASSERT(componentPtr != NULL); // Only objects returned by \b CreateComponent should be used as input
163 
164  if (componentPtr != NULL){
165  return BaseClass2::ExtractInterface<Interface>(componentPtr, subId);
166  }
167  }
168 
169  return NULL;
170 }
171 
172 
173 // protected methods
174 
175 template <class Interface>
177 : BaseClass(ptr),
178  m_definitionComponentPtr(ptr.m_definitionComponentPtr)
179 {
180 }
181 
182 
183 } // namespace icomp
184 
185 
186 #endif // !icomp_TFactoryMember_included
187 
188 
static Interface * ExtractInterface(istd::IPolymorphic *instancePtr, const QByteArray &subId="")
Extract interface from some component.
QSet< QByteArray > KeyList
Definition: IFactoryInfo.h:23
bool IsValid() const
Check if internal pointer not NULL.
Definition: TPointerBase.h:136
Main component interface.
Definition: IComponent.h:31
CInterfaceManipBase BaseClass2
Type * PopPtr()
Reset internal pointer value without deleting instance and return previos value.
Definition: TDelPtr.h:149
virtual IComponent * CreateSubcomponent(const QByteArray &componentId) const =0
Create instance of subcomponent using its ID.
virtual const ICompositeComponent * GetParentComponent(bool ownerOnly=false) const =0
Get parent of this component.
Interface adding to attribute static info functionality existing only for real attributes.
Factory of components used as component member.
Generic interface for a factory.
Definition: TIFactory.h:17
Base interface for all used interfaces and implementations.
Definition: IPolymorphic.h:17
virtual KeyList GetFactoryKeys() const
Returns all posible keys for this factory.
TAttributeMember< CFactoryAttribute > BaseClass
Type * GetPtr() const
Return access to internal stored pointer.
Definition: TPointerBase.h:129
bool IsValid() const
Check if this factory can be resolved.
void operator*(const icomp::TReferenceMember< void > &)
#define NULL
Definition: istd.h:64
virtual Interface * CreateInstance(const QByteArray &keyId="") const
Create an instance of the object, mapped to the keyId keyId.
Pointer to component attribute.
Composite component interface.
istd::IFactoryInfo::KeyList KeyList
void Init(const IComponent *ownerPtr, const IRealAttributeStaticInfo &staticInfo)
Pointer wrapper providing automatic deleting pointed object during destruction.
Definition: TDelPtr.h:20
IComponent * CreateComponent() const
Create component without extracting any interface.

© 2007-2017 Witold Gantzke and Kirill Lepskiy