X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/blobdiff_plain/014983949544db9a324cece29a0ccc46b1e3e1f3..20ee10ed13aee7d9fb9c844648d5159ed4a054d2:/named_object_list.h diff --git a/named_object_list.h b/named_object_list.h index ae7229d..2c691fa 100644 --- a/named_object_list.h +++ b/named_object_list.h @@ -4,6 +4,17 @@ #include #include +// Define an associative container that maps a name with a class and a +// description. All classes must be derived from a same base class. +// +// We can then use the name to create an object of the associated +// class, and to retrieve a pointer to this object. +// +// Furthermore, it is possible to iterate over the elements to get +// their name and their description. + +// I am too lazy to comment the code, which should be obvious... + //===== arity 0 ===== template @@ -13,6 +24,7 @@ protected: std::string description; creator_base(const std::string& descr): description(descr) { } creator_base(const char* descr): description(descr) { } + virtual ~creator_base() { } virtual Base* operator()() const = 0; }; @@ -56,11 +68,6 @@ public: return NULL; } - Base* new_instance(const char* name) const - { - return new_instance(std::string(name)); - } - const std::string& get_name(iterator& it) const { return it->first; } const std::string& get_descr(iterator& it) const { return it->second->description; } @@ -74,9 +81,6 @@ public: //===== arity 2 ===== -#include -#include - template class named_object_list2 { protected: @@ -84,6 +88,7 @@ protected: std::string description; creator_base(const std::string& descr): description(descr) { } creator_base(const char* descr): description(descr) { } + virtual ~creator_base() { } virtual Base* operator()(Arg1, Arg2) const = 0; }; @@ -128,11 +133,6 @@ public: return NULL; } - Base* new_instance(const char* name, Arg1 arg1, Arg2 arg2) const - { - return new_instance(std::string(name), arg1, arg2); - } - const std::string& get_name(iterator& it) const { return it->first; } const std::string& get_descr(iterator& it) const { return it->second->description; } @@ -146,6 +146,7 @@ public: //=================== +// "NOL" like in Named_Object_List.... #define NOL_INSERT(name, descr, class) insert(name, new creator(descr)) #endif // !NAMED_OBJECT_LIST_H