diff options
Diffstat (limited to 'meowpp')
-rw-r--r-- | meowpp/oo/ObjBase.h | 1 | ||||
-rw-r--r-- | meowpp/oo/ObjSelector.h | 51 | ||||
-rw-r--r-- | meowpp/oo/Properties.hpp | 2 | ||||
-rw-r--r-- | meowpp/oo/Register_Implement.h | 55 | ||||
-rw-r--r-- | meowpp/oo/Register_Implement.hpp | 36 |
5 files changed, 54 insertions, 91 deletions
diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h index 8bd0f1c..2c8cb70 100644 --- a/meowpp/oo/ObjBase.h +++ b/meowpp/oo/ObjBase.h @@ -11,6 +11,7 @@ namespace meow{ public: virtual ~ObjBase(){ } // + virtual ObjBase* create() const = 0; virtual char const* ctype() const{ static char const* ptr = typeid(*this).name(); return ptr; 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__ diff --git a/meowpp/oo/Properties.hpp b/meowpp/oo/Properties.hpp index 90d9805..58be1b4 100644 --- a/meowpp/oo/Properties.hpp +++ b/meowpp/oo/Properties.hpp @@ -1,5 +1,7 @@ #include "Properties.h" +#include "ObjBase.h" + #include <map> #include <queue> diff --git a/meowpp/oo/Register_Implement.h b/meowpp/oo/Register_Implement.h deleted file mode 100644 index 910ee11..0000000 --- a/meowpp/oo/Register_Implement.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef oo_Register_Implement_H__ -#define oo_Register_Implement_H__ - -#include <map> -#include <vector> - -namespace meow{ - template<class T> - class ImplementInterface{ - private: - T identify_; - protected: - ImplementInterface(T const& id): identify_(id) { } - public: - T const& identify() const { return identify_; } - virtual ~ImplementInterface(){ } - }; - // - template<class T> - class RegisterInterface{ - private: - std::map<T, ImplementInterface<T>*> implements; - protected: - RegisterInterface(); - public: - virtual bool regImplement(ImplementInterface<T>*imp); - virtual ImplementInterface<T>* getImplement(T const& identify); - virtual ~RegisterInterface(){ } - std::vector<T> getIdentifys() const; - }; - //# - //# === meow:: *ImplementInterface/RegisterInterface* (C++ Class) - //# ==== Description - //# Assume there is a problem which can be solved by different algorithms. - //# Then you can write multiple classes to approach this problem. + - //# Now if you want to decide which algorithm to use in runtime, you can just - //# approach this case by a simple way: - //# - //# * Let all the problem-solving classes inherit from - //# `class ImplementInterface<T>` , and call the constructure with giving - //# `identify` (type `T` ) . - //# * Create an class inherit from `RegisterInterface<T>` , - //# and register all your implement class to it by call - //# `regImplement(pointer to the class)`. - //# * Select which implement class you want by call - //# `getImplement(identify)` , which will return the pointer - //# to the corresponding class. - //# - //# ''' - //# -} - -#include "Register_Implement.hpp" - -#endif // oo_Register_Implement_H__ diff --git a/meowpp/oo/Register_Implement.hpp b/meowpp/oo/Register_Implement.hpp deleted file mode 100644 index bee7f3e..0000000 --- a/meowpp/oo/Register_Implement.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "Register_Implement.h" - - -#include <map> -#include <vector> - -namespace meow{ - template<class T> - inline RegisterInterface<T>::RegisterInterface(){ } - template<class T> - inline bool RegisterInterface<T>::regImplement(ImplementInterface<T>* imp){ - if(implements.find(imp->identify()) != implements.end()){ - return false; - } - implements[imp->identify()] = imp; - return true; - } - template<class T> - inline ImplementInterface<T>* - RegisterInterface<T>::getImplement(T const& identify){ - if(implements.find(identify) == implements.end()){ - return NULL; - } - return implements[identify]; - } - template<class T> - inline std::vector<T> - RegisterInterface<T>::getIdentifys() const{ - std::vector<T> ret; - for(typename std::map<T, ImplementInterface<T>*>::const_iterator - it = implements.begin(); it != implements.end(); it++){ - ret.push_back(it->first); - } - return ret; - } -} |