1 #ifndef _utl_ObjectFactory_h_
2 #define _utl_ObjectFactory_h_
25 template<
typename IdentifierType,
class ObjectPtrType>
32 template<
typename ArgumentType>
37 template<
typename Argument1Type,
typename Argument2Type>
39 const Argument1Type& ,
40 const Argument2Type& )
67 template<
class ObjPtrType,
69 typename CreatType = ObjPtrType (*)(),
70 class FactoryErrorPolicy = FactoryErrorIgnore<IdentType, ObjPtrType>>
80 typedef typename RegistryType::const_iterator
Iterator;
90 const CreatorType& creator)
93 fgRegistry =
new RegistryType;
94 return fgRegistry->insert(std::make_pair(
id, creator)).second;
99 {
return fgRegistry ? fgRegistry->size() : 0; }
107 return ObjectFactory::Unregistered(
id);
109 const auto it = fgRegistry->find(
id);
110 if (it != fgRegistry->end())
111 return (it->second)();
113 return ObjectFactory::Unregistered(
id);
117 template<
typename ArgumentType>
120 Create(
const IdentifierType&
id,
const ArgumentType& arg)
123 return ObjectFactory::Unregistered(
id, arg);
125 const auto it = fgRegistry->find(
id);
126 if (it != fgRegistry->end())
127 return (it->second)(arg);
129 return ObjectFactory::Unregistered(
id, arg);
133 template<
typename Argument1Type,
typename Argument2Type>
137 const Argument1Type& arg1,
138 const Argument2Type& arg2)
141 return ObjectFactory::Unregistered(
id, arg1, arg2);
143 const auto it = fgRegistry->find(
id);
144 if (it != fgRegistry->end())
145 return (it->second)(arg1, arg2);
147 return ObjectFactory::Unregistered(
id, arg1, arg2);
156 fgRegistry =
new RegistryType;
157 return fgRegistry->begin();
166 fgRegistry =
new RegistryType;
167 return fgRegistry->end();
180 template<
class ObjectPtrType,
181 typename IdentifierType,
182 typename CreatorType,
183 class FactoryErrorPolicy>
185 CreatorType, FactoryErrorPolicy>::RegistryType*
187 CreatorType, FactoryErrorPolicy>::fgRegistry =
nullptr;
RegistryType::const_iterator Iterator
static RegistryType * fgRegistry
Default error policy for ObjectFactory: return 0.
static ObjectPtrType Unregistered(const IdentifierType &)
static Iterator Begin()
Begin iterator over the internal map (read only)
static ObjectPtrType Create(const IdentifierType &id, const Argument1Type &arg1, const Argument2Type &arg2)
Create an object (2-argument constructor)
static ObjectPtrType Unregistered(const IdentifierType &, const Argument1Type &, const Argument2Type &)
static Iterator End()
End iterator over the internal map (read only)
static unsigned int GetNumberOfCreators()
Get number of object types know to this factory.
static bool Register(const IdentifierType &id, const CreatorType &creator)
Register a factory function with an ID.
Template for object factory.
std::map< IdentType, CreatType > RegistryType
static ObjectPtrType Create(const IdentifierType &id, const ArgumentType &arg)
Create an object (1-argument constructor)
static ObjectPtrType Unregistered(const IdentifierType &, const ArgumentType &)
static ObjectPtrType Create(const IdentifierType &id)
Create an object (0-argument constructor)