ACF $AcfVersion:0$
TComposedFactory.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// Qt includes
6#include <QtCore/QList>
7
8// ACF includes
9#include <istd/TIFactory.h>
10#include <istd/TSmartPtr.h>
11
12
13namespace istd
14{
15
16
20template <class InterfaceType>
21class TComposedFactory: virtual public TIFactory<InterfaceType>
22{
23public:
25
26 template <class FactoryImpl>
27 bool RegisterFactory(const QByteArray& typeId)
28 {
29 istd::TSmartPtr<FactoryInterface> factoryPtr(new FactoryImpl(typeId));
30
31 if (factoryPtr->GetFactoryKeys().isEmpty()){
32 return false;
33 }
34
35 m_factoryList.push_back(factoryPtr);
36
37 return true;
38 }
39
40 // reimplemented (istd::IFactoryInfo)
41 virtual IFactoryInfo::KeyList GetFactoryKeys() const override;
42
43 // reimplemented (istd::TIFactory)
44 virtual istd::TUniqueInterfacePtr<InterfaceType> CreateInstance(const QByteArray& keyId = "") const override;
45
46protected:
48 typedef QList<FactoryPtr> FactoryList;
49
51};
52
53
54// reimplemented (istd::IFactoryInfo)
55
56template <class InterfaceType>
58{
60
61 for ( typename FactoryList::const_iterator iter = m_factoryList.begin();
62 iter != m_factoryList.end();
63 ++iter){
64 const FactoryPtr& factoryPtr = *iter;
65
66 IFactoryInfo::KeyList factoryKeys = factoryPtr->GetFactoryKeys();
67
68 retVal += factoryKeys;
69 }
70
71 return retVal;
72}
73
74
75// reimplemented (istd::TIFactory)
76
77template <class InterfaceType>
79{
80 for ( typename FactoryList::const_iterator iter = m_factoryList.begin();
81 iter != m_factoryList.end();
82 ++iter){
83 const FactoryPtr& factoryPtr = *iter;
84
85 istd::TUniqueInterfacePtr<InterfaceType> createdPtr = factoryPtr->CreateInstance(keyId);
86
87 if (createdPtr.IsValid()){
88 return createdPtr;
89 }
90 }
91
93}
94
95
96} // namespace istd
97
98
99
100
QSet< QByteArray > KeyList
Standard generic implementation of the composed factory.
virtual istd::TUniqueInterfacePtr< InterfaceType > CreateInstance(const QByteArray &keyId="") const override
Create an instance of the object, mapped to the keyId keyId.
TIFactory< InterfaceType > FactoryInterface
istd::TSmartPtr< FactoryInterface > FactoryPtr
virtual IFactoryInfo::KeyList GetFactoryKeys() const override
Returns all possible keys for this factory.
bool RegisterFactory(const QByteArray &typeId)
QList< FactoryPtr > FactoryList
Generic interface for a factory.
Definition TIFactory.h:19
bool IsValid() const noexcept
Unique ownership smart pointer for interface types.
Standard library.
Definition IComponent.h:17