16template<
class ValueType>
18 auto it = m_constructors.find(_key);
19 if (it != m_constructors.end()) {
20 OT_LOG_E(
"Constructor already registered");
23 m_constructors.insert_or_assign(_key, _constructor);
26template<
class ValueType>
28 m_constructors.erase(_key);
31template<
class ValueType>
33 auto it = m_constructors.find(_key);
34 if (it == m_constructors.end()) {
35 OT_LOG_E(
"Constructor \"" + _key +
"\" not found");
43template<
class ValueType>
45 std::string t = json::getString(_jsonObject, _typeKey);
50 ValueType* ret = this->createFromKey(t);
61template<
class ValueType>
63 return this->createFromJSON(_jsonObject, _typeKey.c_str());
72template <
class FactoryType,
typename CreatedType>
76 static_assert(std::is_same<
decltype(&FactoryType::instance),
decltype(&FactoryType::instance)>
::value,
77 "Factory must have a static 'instance' function.");
79 static_assert(std::is_same<
decltype(&FactoryType::instance), FactoryType& (*)()>
::value,
80 "Factory must have a static 'instance' function which returns a reference to the global factory instance.");
82 FactoryType::instance().registerConstructor(_key, []() {
83 return new CreatedType;
87template <
class FactoryType,
typename CreatedType>
89 FactoryType::instance().deregisterConstructor(m_key);
bsoncxx::types::value value
Definition DocumentManager.h:16
#define OT_LOG_E(___text)
Log a error message according to the service logger configuration.
Definition Logger.h:186
virtual ~FactoryRegistrarTemplate()
Destructor. When called the constructor will be deregistered from the factory.
Definition FactoryTemplate.hpp:88
FactoryRegistrarTemplate(const std::string &_key)
Constructor.
Definition FactoryTemplate.hpp:73
void deregisterConstructor(const std::string &_key)
Remove the constructor for the given key.
Definition FactoryTemplate.hpp:27
std::function< ValueType *()> ConstructorFunctionType
Constructor type definition.
Definition FactoryTemplate.h:27
ValueType * createFromJSON(const ConstJsonObject &_jsonObject, const char *_typeKey)
Creates an instance according to the key in the provided JSON object. If the key is empty the request...
Definition FactoryTemplate.hpp:44
void registerConstructor(const std::string &_key, ConstructorFunctionType _constructor)
Will register the prvided constructor under the given key. If a constructor for the given key already...
Definition FactoryTemplate.hpp:17
ValueType * createFromKey(const std::string &_key)
Call the constructor for the given key and return the created instance.
Definition FactoryTemplate.hpp:32
The Serializable class is the default interface of serializable objects.
Definition Serializable.h:17
virtual void setFromJsonObject(const ot::ConstJsonObject &_object)=0
Set the object contents from the provided JSON object.
rapidjson::GenericObject< true, rapidjson::GenericValue< rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > > > ConstJsonObject
Read only JSON Object.
Definition JSON.h:35