9#ifndef CLASSDESC_FACTORY_H
10#define CLASSDESC_FACTORY_H
18 template <
class B,
class Type>
23 virtual ~CreatorBase() {}
24 virtual B*
create()
const=0;
26 typedef std::map<Type, shared_ptr<CreatorBase> > Fmap;
30 struct Creator:
public CreatorBase
32 B*
create()
const {
return new T();}
41 (make_pair(t, shared_ptr<CreatorBase>(
new Creator<T>)));
62 struct InvalidType:
public exception
65 InvalidType(
const Type& t) {
66 std::ostringstream os;
67 os<<
"invalid type "<<t;
70 const char* what()
const throw() {
return s.c_str();}
71 ~InvalidType()
throw() {}
77 typename Fmap::const_iterator i=fmap.find(t);
81 return i->second->create();
85 std::vector<string>
types()
const
87 std::vector<string> r;
88 for (
typename Fmap::const_iterator i=fmap.begin(); i!=fmap.end(); ++i)
89 r.push_back(i->first);
void registerType(const Type &t)
register type T for use with this factory
Definition factory.h:38
B * create(const Type &t) const
Create a default constructed object given by the type string.
Definition factory.h:75
std::vector< string > types() const
return list of registered types
Definition factory.h:85
void registerType()
convenience method for registering a type.
Definition factory.h:44
Contains definitions related to classdesc functionality.