9 #ifndef _GENERIC_FACTORY_
10 #define _GENERIC_FACTORY_
12 #include <tr1/functional>
21 #define REGISTER(basename,derivedname) \
23 static const Registrar<basename,derivedname> reg;
26 #define DEFINEREGISTER(basename,derivedname,key) \
27 const Registrar<basename,derivedname> derivedname::reg("key");
40 template <
class Derived>
43 return typename Derived::shared_pointer(
new Derived);
57 template <
class Derived>
60 return Derived::instance();
122 shared_pointer
create(
const typename instanciatorMap_t::key_type & type)
125 typename instanciatorMap_t::const_iterator it(
_iMap.find(type));
126 if (it ==
_iMap.end())
127 throw invalid_argument(
"Factory::create(): Type '" + type +
"' is not registered");
128 return (it->second)();
136 template <
class Derived>
137 char addType(
const typename instanciatorMap_t::key_type &type)
139 _iMap.insert(make_pair(type,&instanciator<Derived>));
148 template <
class Derived>
151 _iMap.insert(make_pair(type,&singletoninstanciator<Derived>));
170 template<
class Base,
class Derived>
185 Registrar(
const typename factory_t::instanciatorMap_t::key_type &type)
187 factory_r factory(factory_t::instance());
188 factory.template addType<Derived>(type);
197 Registrar(
const typename factory_t::instanciatorMap_t::key_type &type,
bool)
199 factory_r factory(factory_t::instance());
200 factory.template addSingletonType<Derived>(type);
std::tr1::function< shared_pointer()> instanciator_t
define how the instanctiator function should look like
helper struct that will add Derived to the factory map
char addSingletonType(const typename instanciatorMap_t::key_type &type)
register a derived singleton type to the map
std::map< std::string, instanciator_t > instanciatorMap_t
define the map of instanciator functions
Derived::shared_pointer instanciator()
function to create an instance of a class
instanciatorMap_t _iMap
map human readable names to instanciators for the object
Base::shared_pointer shared_pointer
define a shared pointer of the base class
char addType(const typename instanciatorMap_t::key_type &type)
register a derived type to the map
shared_pointer create(const typename instanciatorMap_t::key_type &type)
create an instance of the requested derived type
Factory< Base > factory_t
define the factory
factory_t::reference factory_r
define a reference to the factory
Derived::shared_pointer singletoninstanciator()
function to create a singleton of a class
Registrar(const typename factory_t::instanciatorMap_t::key_type &type)
constuctor
Factory< Base > & reference
define a reference to this factory
static reference instance()
get an instance of the factory
Registrar(const typename factory_t::instanciatorMap_t::key_type &type, bool)
constuctor
Factory for creating shared_pointers of classes derived from a base class.