aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/oo/ObjSelector.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/oo/ObjSelector.h')
-rw-r--r--meowpp/oo/ObjSelector.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/meowpp/oo/ObjSelector.h b/meowpp/oo/ObjSelector.h
new file mode 100644
index 0000000..e70c496
--- /dev/null
+++ b/meowpp/oo/ObjSelector.h
@@ -0,0 +1,51 @@
+#ifndef oo_ObjSelector_H__
+#define oo_ObjSelector_H__
+
+#include "ObjBase.h"
+
+#include <cstdlib>
+
+namespace meow{
+ template<size_t id>
+ class ObjSelector{
+ private:
+ typedef std::map <std::string, ObjBase*> Funcs;
+ typedef std::vector<std::string > Types;
+ //
+ static Funcs& funcs(){
+ static Funcs f;
+ return f;
+ }
+ //
+ std::string name;
+ ObjBase* ptr;
+ public:
+ ObjSelector( ObjBase* o){ add(name = o->type(), ptr = o); }
+ ObjSelector(std::string n, ObjBase* o){ add(name = n , ptr = o); }
+ ~ObjSelector(){ del(name); }
+ //
+ static void add(std::string n, ObjBase* b){
+ del(n);
+ funcs()[n] = b;
+ }
+ static void del(std::string s){
+ if(funcs().find(s) != funcs().end()){
+ delete funcs()[s];
+ funcs().erase(s);
+ }
+ }
+ static ObjBase* get(std::string s){
+ if(funcs().find(s) == funcs().end() || funcs()[s] == NULL)
+ return NULL;
+ return funcs()[s]->create();
+ }
+ static Types lst(){
+ Types ret;
+ for(Funcs::iterator it = funcs().begin(); it != funcs().end(); it++)
+ ret.push_back(it->first);
+ return ret;
+ }
+ };
+}
+
+#endif // oo_ObjSelector_H__