25 #include "cafAssert.h" 42 #define CAFFA_FACTORY_CONCATENATE_STRINGS( foo, bar ) CAFFA_FACTORY_CONCATENATE_STRINGS_IMPL_( foo, bar ) 43 #define CAFFA_FACTORY_CONCATENATE_STRINGS_IMPL_( foo, bar ) foo##bar 45 #define CAFFA_UNIQUE_COMPILE_UNIT_VAR_NAME( foo ) CAFFA_FACTORY_CONCATENATE_STRINGS( foo, __LINE__ ) 50 #define CAFFA_FACTORY_REGISTER( BaseType, TypeToCreate, KeyType, key ) \ 51 static bool CAFFA_UNIQUE_COMPILE_UNIT_VAR_NAME( my##TypeToCreate ) = \ 52 caffa::RestServiceFactory<BaseType, KeyType>::instance() -> registerCreator<TypeToCreate>( key ) 53 #define CAFFA_FACTORY_REGISTER2( BaseType, TypeToCreate, KeyType, key ) \ 54 static bool CAFFA_UNIQUE_COMPILE_UNIT_VAR_NAME( my2##TypeToCreate ) = \ 55 caffa::RestServiceFactory<BaseType, KeyType>::instance() -> registerCreator<TypeToCreate>( key ) 59 template <
typename BaseType,
typename KeyType>
62 class ObjectCreatorBase;
71 template <
typename TypeToCreate>
72 bool registerCreator(
const KeyType& key )
74 auto entryIt = m_factoryMap.find( key );
75 if ( entryIt == m_factoryMap.end() )
77 m_factoryMap[key] = std::make_unique<ObjectCreator<TypeToCreate>>();
83 std::shared_ptr<BaseType> create(
const KeyType& key )
85 auto entryIt = m_factoryMap.find( key );
86 if ( entryIt != m_factoryMap.end() )
88 return entryIt->second->create();
96 std::vector<KeyType> allKeys()
98 std::vector<KeyType> keys;
100 for (
auto entryIt = m_factoryMap.begin(); entryIt != m_factoryMap.end(); ++entryIt )
102 keys.push_back( entryIt->first );
114 class ObjectCreatorBase
117 ObjectCreatorBase() {}
118 virtual ~ObjectCreatorBase() {}
119 virtual std::shared_ptr<BaseType> create() = 0;
122 template <
typename TypeToCreate>
123 class ObjectCreator :
public ObjectCreatorBase
126 std::shared_ptr<BaseType> create()
override {
return std::make_shared<TypeToCreate>(); }
130 std::map<KeyType, std::unique_ptr<ObjectCreatorBase>> m_factoryMap;
Definition: cafRestServiceFactory.h:60
Main Caffa namespace.
Definition: cafApplication.h:30