RTC Toolkit 4.0.1
Loading...
Searching...
No Matches
factoryRegistry.hpp
Go to the documentation of this file.
1
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 private:
88 // Do not allow copying of this class.
89 FactoryIf(const FactoryIf&) = delete;
90 FactoryIf& operator=(const FactoryIf&) = delete;
91 FactoryIf(FactoryIf&&) = delete;
92 FactoryIf& operator=(FactoryIf&&) = delete;
93 };
94
95 FactoryRegistry() = default;
96
97 // Note that virtual is not needed, since this class is final and is not part of a class tree.
99
109 void Register(const FactoryIf* factory);
110
120 void Deregister(const FactoryIf* factory) noexcept;
121
135 const FactoryIf* FindCompatibleFactory(const AdapterIdType& id) const noexcept;
136
145 static std::shared_ptr<FactoryRegistry<BaseIf>> GetInstance();
146
147private:
148 // Do not allow copying of this class.
149 FactoryRegistry(const FactoryRegistry&) = delete;
150 FactoryRegistry& operator=(const FactoryRegistry&) = delete;
152 FactoryRegistry& operator=(FactoryRegistry&&) = delete;
153
155 mutable std::mutex m_mutex;
156
158 std::vector<const FactoryIf*> m_factories;
159};
160
187template <class BaseIf, class Factory>
188class RegisterFactory final {
189public:
192
193private:
194 // Do not allow copying of this class.
195 RegisterFactory(const RegisterFactory&) = delete;
196 RegisterFactory& operator=(const RegisterFactory&) = delete;
198 RegisterFactory& operator=(RegisterFactory&&) = delete;
199
200 // Need to hold a shared pointer to the registry that we will be registering with, so that the
201 // registry is kept alive as long as we have not unregistered the factory.
202 std::shared_ptr<FactoryRegistry<BaseIf>> m_registry;
203
204 // The instance of the factory that will be registered with the registry.
205 Factory m_factory;
206};
207
208} // namespace rtctk::componentFramework
209
211
212#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
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.
A simple registry of various factory objects.
Definition: factoryRegistry.hpp:47
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()
Definition: factoryRegistry.ipp:24
static std::shared_ptr< FactoryRegistry< BaseIf > > GetInstance()
Returns the registry singleton.
Definition: factoryRegistry.ipp:71
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
A helper class used to perform registration and deregistration of a factory.
Definition: factoryRegistry.hpp:188
RegisterFactory()
Definition: factoryRegistry.ipp:103
~RegisterFactory()
Definition: factoryRegistry.ipp:109
Implementation file for FactoryRegistry and related helper template classes.
Definition: commandReplier.cpp:22