12#ifndef USMTFFACTORY_HPP
13#define USMTFFACTORY_HPP
15#include "usmtf_export.h"
19#include <unordered_map>
23#include "UtMemory.hpp"
38template<
class T,
class U>
43 using ConstructorCb = std::function<std::unique_ptr<T>(
const std::string& aType, U aContents)>;
57 static std::unique_ptr<V>
Construct(
const std::string& aType, U&& aContents);
70 USMTF_Factory<T, U>() =
default;
71 using CallbackMap = std::unordered_map<std::string, ConstructorCb>;
74 CallbackMap mRegisteredEntities;
75 static std::unique_ptr<USMTF_Factory<T, U>> mFactoryInstance;
84#if defined(SWDEV_ALL_USE_DLL)
90template<
class T,
class U>
93 if (!mFactoryInstance)
95 mFactoryInstance = std::unique_ptr<USMTF_Factory<T, U>>(
new USMTF_Factory<T, U>);
98 return mFactoryInstance.get();
101template<
class T,
class U>
105 typename CallbackMap::iterator it = mRegisteredEntities.find(aEntity.GetType());
106 if (it != mRegisteredEntities.end())
108 return dynamic_cast<const V*
>(&aEntity);
113template<
class T,
class U>
117 return ut::make_unique<V>(aType, std::move(aContents));
120template<
class T,
class U>
124 mRegisteredEntities[aType] = aConstructorCb;
127template<
class T,
class U>
130 mRegisteredEntities.erase(aType);
133template<
class T,
class U>
136 typename CallbackMap::iterator it = mRegisteredEntities.find(aType);
137 if (it != mRegisteredEntities.end())
139 return (it->second)(aType, std::move(aContents));
141 return ut::make_unique<T>(aType, std::move(aContents));
144template<
class T,
class U>
145std::unique_ptr<USMTF_Factory<T, U>> USMTF_Factory<T, U>::mFactoryInstance;
Uses of this is highly documented in pretty much every tests Suite.
Definition USMTF_Factory.hpp:40
static std::unique_ptr< V > Construct(const std::string &aType, U &&aContents)
Definition USMTF_Factory.hpp:115
std::function< std::unique_ptr< T >(const std::string &aType, U aContents)> ConstructorCb
Constructor Callback specifies the expected format of the function needed to instantiate objects.
Definition USMTF_Factory.hpp:43
std::unique_ptr< T > CreateInstance(const std::string &aType, U aContents)
Instantiate an instance of the Set or Message. Used Only by parser.
Definition USMTF_Factory.hpp:134
static USMTF_Factory< T, U > * GetFactory()
Return the singleton instance of the factory.
Definition USMTF_Factory.hpp:91
void RegisterEntity(const std::string &aType, ConstructorCb aCb) noexcept
Definition USMTF_Factory.hpp:121
const V * CastIfRegistered(const T &aEntity) noexcept
Definition USMTF_Factory.hpp:103
void UnregisterEntity(const std::string &aType) noexcept
Unregister a previously registers derived type.
Definition USMTF_Factory.hpp:128
USMTF_Factory< Message, std::vector< std::unique_ptr< Set > > > MessageFactory
Definition USMTF_Factory.hpp:80
USMTF_Factory< Set, std::vector< Field > > SetFactory
Definition USMTF_Factory.hpp:81