RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
factoryRegistry.hpp
Go to the documentation of this file.
1
12
13#ifndef RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
14#define RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
15
16#include <memory>
17#include <mutex>
18#include <vector>
19
21
46template <class BaseIf>
47class FactoryRegistry final {
48public:
49 using AdapterIdType = typename BaseIf::AdapterIdType;
50
57 class FactoryIf {
58 public:
59 using AdapterIdType = typename BaseIf::AdapterIdType;
60
61 FactoryIf() = default;
62 virtual ~FactoryIf() = default;
63
74 virtual bool CanHandle(const AdapterIdType& id) const = 0;
75
85 virtual std::unique_ptr<BaseIf> CreateInstance(const AdapterIdType& id) const = 0;
86
87 // Do not allow copying of this class.
88 FactoryIf(const FactoryIf&) = delete;
89 FactoryIf& operator=(const FactoryIf&) = delete;
90 FactoryIf(FactoryIf&&) = delete;
92 };
93
94 FactoryRegistry() = default;
95
96 // Note that virtual is not needed, since this class is final and is not part of a class tree.
98
108 void Register(const FactoryIf* factory);
109
119 void Deregister(const FactoryIf* factory) noexcept;
120
134 const FactoryIf* FindCompatibleFactory(const AdapterIdType& id) const noexcept;
135
144 static std::shared_ptr<FactoryRegistry<BaseIf>> GetInstance();
145
146 // Do not allow copying of this class.
151
152private:
154 mutable std::mutex m_mutex;
155
157 std::vector<const FactoryIf*> m_factories;
158};
159
186template <class BaseIf, class Factory>
187class RegisterFactory final {
188public:
191
192 // Do not allow copying of this class.
197
198public:
199 // Need to hold a shared pointer to the registry that we will be registering with, so that the
200 // registry is kept alive as long as we have not unregistered the factory.
201 std::shared_ptr<FactoryRegistry<BaseIf>> m_registry;
202
203 // The instance of the factory that will be registered with the registry.
204 Factory m_factory;
205};
206
207} // namespace rtctk::componentFramework
208
210
211#endif // RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
The base class for all factory objects that are registered in the FactoryRegistry.
Definition factoryRegistry.hpp:57
typename BaseIf::AdapterIdType AdapterIdType
Definition factoryRegistry.hpp:59
FactoryIf & operator=(const FactoryIf &)=delete
virtual std::unique_ptr< BaseIf > CreateInstance(const AdapterIdType &id) const =0
Create a new adapter instance to access the service specified by the given identifier.
virtual bool CanHandle(const AdapterIdType &id) const =0
Check if this factory is compatible with the given service identifier.
void Register(const FactoryIf *factory)
Register the factory if it was not already registered.
Definition factoryRegistry.ipp:37
void Deregister(const FactoryIf *factory) noexcept
Remove the factory from the registry if it was already registered.
Definition factoryRegistry.ipp:48
FactoryRegistry & operator=(const FactoryRegistry &)=delete
~FactoryRegistry()
Definition factoryRegistry.ipp:24
static std::shared_ptr< FactoryRegistry< BaseIf > > GetInstance()
Returns the registry singleton.
Definition factoryRegistry.ipp:72
FactoryRegistry & operator=(FactoryRegistry &&)=delete
const FactoryIf * FindCompatibleFactory(const AdapterIdType &id) const noexcept
Finds the first factory that can handle the given identifier.
Definition factoryRegistry.ipp:60
typename BaseIf::AdapterIdType AdapterIdType
Definition factoryRegistry.hpp:49
FactoryRegistry(const FactoryRegistry &)=delete
FactoryRegistry(FactoryRegistry &&)=delete
Factory m_factory
Definition factoryRegistry.hpp:204
RegisterFactory()
Definition factoryRegistry.ipp:104
std::shared_ptr< FactoryRegistry< BaseIf > > m_registry
Definition factoryRegistry.hpp:201
~RegisterFactory()
Definition factoryRegistry.ipp:110
RegisterFactory(RegisterFactory &&)=delete
RegisterFactory & operator=(const RegisterFactory &)=delete
RegisterFactory(const RegisterFactory &)=delete
RegisterFactory & operator=(RegisterFactory &&)=delete
Implementation file for FactoryRegistry and related helper template classes.
Definition commandReplier.cpp:22