Advertisement
WeltEnSTurm

class_string_map

Oct 24th, 2011
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1.  
  2. #ifndef CLASS_STRING_MAP_HPP_
  3. #define CLASS_STRING_MAP_HPP_
  4.  
  5. #include <string>
  6. #include <vector>
  7.  
  8. template<typename T_base, typename T_key = std::string>
  9. class class_string_map {
  10.     protected:
  11.         class_string_map(){}
  12.  
  13.         class Entry {
  14.             public:
  15.                 Entry(const T_key& key, T_base* (*func)()):
  16.                     Instantiate(func),
  17.                     Key(key)
  18.                 {}
  19.                 virtual ~Entry(){};
  20.                 T_base* (*Instantiate)();
  21.                 const T_key Key;
  22.         };
  23.  
  24.         static class_string_map& Get(){
  25.             static class_string_map Manager;
  26.             return Manager;
  27.         }
  28.  
  29.         std::vector<Entry*> Classes;
  30.  
  31.     public:
  32.         template<typename T_sub>
  33.         static void Register(const T_key& key){
  34.             Get().Classes.push_back(new Entry(key, []() -> T_base* {return new T_sub;}));
  35.         }
  36.  
  37.         static T_base* Instantiate(const T_key& key){
  38.             for(Entry* e: Get().Classes)
  39.                 if(e->Key == key)
  40.                     return e->Instantiate();
  41.  
  42.             return 0;
  43.         }
  44. };
  45.  
  46. #endif /* CLASS_STRING_MAP_HPP_ */
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement