Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CLASS_STRING_MAP_HPP_
- #define CLASS_STRING_MAP_HPP_
- #include <string>
- #include <vector>
- template<typename T_base, typename T_key = std::string>
- class class_string_map {
- protected:
- class_string_map(){}
- class Entry {
- public:
- Entry(const T_key& key, T_base* (*func)()):
- Instantiate(func),
- Key(key)
- {}
- virtual ~Entry(){};
- T_base* (*Instantiate)();
- const T_key Key;
- };
- static class_string_map& Get(){
- static class_string_map Manager;
- return Manager;
- }
- std::vector<Entry*> Classes;
- public:
- template<typename T_sub>
- static void Register(const T_key& key){
- Get().Classes.push_back(new Entry(key, []() -> T_base* {return new T_sub;}));
- }
- static T_base* Instantiate(const T_key& key){
- for(Entry* e: Get().Classes)
- if(e->Key == key)
- return e->Instantiate();
- return 0;
- }
- };
- #endif /* CLASS_STRING_MAP_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement