#include <map>
#include <string>
+// 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 <typename Base>
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;
};
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; }
- size_t size() const { return assoc.size(); }
+ bool exists(const std::string& name) const
+ { return assoc.find(name) != assoc.end(); }
iterator begin() const { return assoc.begin(); }
iterator end() const { return assoc.end(); }
//===== arity 2 =====
-#include <map>
-#include <string>
-
template <typename Base, typename Arg1, typename Arg2>
class named_object_list2 {
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;
};
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; }
//===================
+// "NOL" like in Named_Object_List....
#define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
#endif // !NAMED_OBJECT_LIST_H