RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
factoryRegistry.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
13#define RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
14
15#include <memory>
16#include <mutex>
17#include <vector>
18
20
45template <class BaseIf>
46class FactoryRegistry final {
47public:
48 using AdapterIdType = typename BaseIf::AdapterIdType;
49
56 class FactoryIf {
57 public:
58 using AdapterIdType = typename BaseIf::AdapterIdType;
59
60 FactoryIf() = default;
61 virtual ~FactoryIf() = default;
62
73 virtual bool CanHandle(const AdapterIdType& id) const = 0;
74
84 virtual std::unique_ptr<BaseIf> CreateInstance(const AdapterIdType& id) const = 0;
85
86 // Do not allow copying of this class.
87 FactoryIf(const FactoryIf&) = delete;
88 FactoryIf& operator=(const FactoryIf&) = delete;
89 FactoryIf(FactoryIf&&) = delete;
91 };
92
93 FactoryRegistry() = default;
94
95 // Note that virtual is not needed, since this class is final and is not part of a class tree.
97
107 void Register(const FactoryIf* factory);
108
118 void Deregister(const FactoryIf* factory) noexcept;
119
133 const FactoryIf* FindCompatibleFactory(const AdapterIdType& id) const noexcept;
134
143 static std::shared_ptr<FactoryRegistry<BaseIf>> GetInstance();
144
145 // Do not allow copying of this class.
150
151private:
153 mutable std::mutex m_mutex;
154
156 std::vector<const FactoryIf*> m_factories;
157};
158
185template <class BaseIf, class Factory>
186class RegisterFactory final {
187public:
190
191 // Do not allow copying of this class.
196
197public:
198 // Need to hold a shared pointer to the registry that we will be registering with, so that the
199 // registry is kept alive as long as we have not unregistered the factory.
200 std::shared_ptr<FactoryRegistry<BaseIf>> m_registry;
201
202 // The instance of the factory that will be registered with the registry.
203 Factory m_factory;
204};
205
206} // namespace rtctk::componentFramework
207
209
210#endif // RTCTK_COMPONENTFRAMEWORK_FACTORYREGISTRY_HPP
The base class for all factory objects that are registered in the FactoryRegistry.
Definition factoryRegistry.hpp:56
typename BaseIf::AdapterIdType AdapterIdType
Definition factoryRegistry.hpp:58
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:36
void Deregister(const FactoryIf *factory) noexcept
Remove the factory from the registry if it was already registered.
Definition factoryRegistry.ipp:47
FactoryRegistry & operator=(const FactoryRegistry &)=delete
~FactoryRegistry()
Definition factoryRegistry.ipp:23
static std::shared_ptr< FactoryRegistry< BaseIf > > GetInstance()
Returns the registry singleton.
Definition factoryRegistry.ipp:71
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:59
typename BaseIf::AdapterIdType AdapterIdType
Definition factoryRegistry.hpp:48
FactoryRegistry(const FactoryRegistry &)=delete
FactoryRegistry(FactoryRegistry &&)=delete
Factory m_factory
Definition factoryRegistry.hpp:203
RegisterFactory()
Definition factoryRegistry.ipp:103
std::shared_ptr< FactoryRegistry< BaseIf > > m_registry
Definition factoryRegistry.hpp:200
~RegisterFactory()
Definition factoryRegistry.ipp:109
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:21