Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Constructable.h
Go to the documentation of this file.
1 #ifndef Protium_Constructable_h_
2 #define Protium_Constructable_h_
3 
4 #include <map>
5 #include <string>
6 
7 namespace Protium{
8  namespace Design{
9 
13  };
14 
15  template<class Group>
17  typedef std::map< std::string, ConstructablePrototype*(*)() > map_type;
18 
19 
20  static ConstructablePrototype* createInstance(const std::string& s, ConstructablePrototype*(*creator)()=0 ){
21  map_type::iterator it = getMap()->find(s);
22  if( it==getMap()->end() ){
23  return NULL;
24  }
25  return it->second();
26  }
27  protected:
28  static map_type* getMap(){
29  static map_type* map = new map_type();
30  return map;
31  }
32  };
33 
34  template<class T, class Group>
36  static ConstructablePrototype* create() {return (new T()); }
37  DerivedRegister(const std::string& s ){
38  getMap->insert(std::make_pair(s, &create) );
39  }
40 
41  };
42  }
43 }
44 
45 #define REGISTER_DEC_TYPE(NAME) \
46  static Protium::Design::DerivedRegister<NAME> reg;
47 
48 #define REGISTER_DEF_TYPE(NAME) \
49  Protium::Design::DerivedRegister<NAME> NAME::reg(#NAME);
50 
51 #endif //File Guardian
static ConstructablePrototype * create()
Definition: Constructable.h:36
DerivedRegister(const std::string &s)
Definition: Constructable.h:37
static ConstructablePrototype * createInstance(const std::string &s, ConstructablePrototype *(*creator)()=0)
Definition: Constructable.h:20
std::map< std::string, ConstructablePrototype *(*)() > map_type
Definition: Constructable.h:17
static map_type * getMap()
Definition: Constructable.h:28