TComposedFactory.h
Go to the documentation of this file.
1 #ifndef istd_TComposedFactory_included
2 #define istd_TComposedFactory_included
3 
4 
5 // Qt includes
6 #include <QtCore/QList>
7 
8 // ACF includes
9 #include <istd/TIFactory.h>
10 #include <istd/TOptDelPtr.h>
11 
12 
13 namespace istd
14 {
15 
16 
20 template <class InterfaceType>
21 class TComposedFactory: virtual public TIFactory<InterfaceType>
22 {
23 public:
25 
32  bool RegisterFactory(FactoryInterface* factoryPtr, bool releaseFlag = false);
33 
34  // reimplemented (istd::IFactoryInfo)
35  virtual IFactoryInfo::KeyList GetFactoryKeys() const;
36 
37  // reimplemented (istd::TIFactory)
38  virtual InterfaceType* CreateInstance(const QByteArray& keyId = "") const;
39 
40 protected:
42  typedef QList<FactoryPtr> FactoryList;
43 
45 };
46 
47 
48 // public methods
49 
50 template <class InterfaceType>
52 {
53  Q_ASSERT(factoryPtr != NULL);
54 
55  if ((factoryPtr == NULL) || factoryPtr->GetFactoryKeys().isEmpty()){
56  return false;
57  }
58 
59  m_factoryList.push_back(FactoryPtr());
60  m_factoryList.back().SetPtr(factoryPtr, releaseFlag);
61 
62  return true;
63 }
64 
65 
66 // reimplemented (istd::IFactoryInfo)
67 
68 template <class InterfaceType>
70 {
71  IFactoryInfo::KeyList retVal;
72 
73  for ( typename FactoryList::const_iterator iter = m_factoryList.begin();
74  iter != m_factoryList.end();
75  ++iter){
76  const FactoryPtr& factoryPtr = *iter;
77 
78  IFactoryInfo::KeyList factoryKeys = factoryPtr->GetFactoryKeys();
79 
80  retVal += factoryKeys;
81  }
82 
83  return retVal;
84 }
85 
86 
87 // reimplemented (istd::TIFactory)
88 
89 template <class InterfaceType>
91 {
92  for ( typename FactoryList::const_iterator iter = m_factoryList.begin();
93  iter != m_factoryList.end();
94  ++iter){
95  const FactoryPtr& factoryPtr = *iter;
96 
97  InterfaceType* createdPtr = factoryPtr->CreateInstance(keyId);
98 
99  if (createdPtr != NULL){
100  return createdPtr;
101  }
102  }
103 
104  return NULL;
105 }
106 
107 
108 } // namespace istd
109 
110 
111 #endif // !istd_TComposedFactory_included
112 
113 
QSet< QByteArray > KeyList
Definition: IFactoryInfo.h:23
Pointer wrapper providing activatable deleting pointed object during destruction. ...
Definition: TOptDelPtr.h:20
Generic interface for a factory.
Definition: TIFactory.h:17
bool RegisterFactory(FactoryInterface *factoryPtr, bool releaseFlag=false)
Registers object factory factoryPtr that will be mapped to the keyId.
QList< FactoryPtr > FactoryList
virtual KeyList GetFactoryKeys() const =0
Returns all posible keys for this factory.
TIFactory< InterfaceType > FactoryInterface
#define NULL
Definition: istd.h:64
virtual InterfaceType * CreateInstance(const QByteArray &keyId="") const
Create an instance of the object, mapped to the keyId keyId.
Interface InterfaceType
Definition: TIFactory.h:20
istd::TOptDelPtr< FactoryInterface > FactoryPtr
virtual IFactoryInfo::KeyList GetFactoryKeys() const
Returns all posible keys for this factory.
Standard generic implementation of the composed factory.

© 2007-2017 Witold Gantzke and Kirill Lepskiy