1 #ifndef NAMED_OBJECT_LIST_H
2 #define NAMED_OBJECT_LIST_H
7 // Define an associative container that maps a name with a class and a
8 // description. All classes must be derived from a same base class.
10 // We can then use the name to create an object of the associated
11 // class, and to retrieve a pointer to this object.
13 // Furthermore, it is possible to iterate over the elements to get
14 // their name and their description.
16 // I am too lazy to comment the code, which should be obvious...
20 template <typename Base>
21 class named_object_list {
24 std::string description;
25 creator_base(const std::string& descr): description(descr) { }
26 creator_base(const char* descr): description(descr) { }
27 virtual Base* operator()() const = 0;
30 template <typename Derived>
31 struct creator: public creator_base {
32 creator(const std::string& descr): creator_base(descr) { }
33 creator(const char* descr): creator_base(descr) { }
34 Base* operator()() const { return new Derived(); }
37 typedef std::map<std::string, const creator_base*> map_type;
41 void insert(const std::string& name, const creator_base* creat)
43 assoc.insert(std::make_pair(name, creat));
46 void insert(const char* name, const creator_base* creat)
48 assoc.insert(std::make_pair(std::string(name), creat));
52 typedef typename map_type::const_iterator iterator;
54 named_object_list() { };
57 for (iterator it = begin(); it != end(); ++it)
61 Base* new_instance(const std::string& name) const
63 iterator it = assoc.find(name);
64 if (it != assoc.end())
65 return (*it->second)();
70 Base* new_instance(const char* name) const
72 return new_instance(std::string(name));
75 const std::string& get_name(iterator& it) const { return it->first; }
76 const std::string& get_descr(iterator& it) const
77 { return it->second->description; }
79 bool exists(const std::string& name) const
80 { return assoc.find(name) != assoc.end(); }
81 iterator begin() const { return assoc.begin(); }
82 iterator end() const { return assoc.end(); }
88 template <typename Base, typename Arg1, typename Arg2>
89 class named_object_list2 {
92 std::string description;
93 creator_base(const std::string& descr): description(descr) { }
94 creator_base(const char* descr): description(descr) { }
95 virtual Base* operator()(Arg1, Arg2) const = 0;
98 template <typename Derived>
99 struct creator: public creator_base {
100 creator(const std::string& descr): creator_base(descr) { }
101 creator(const char* descr): creator_base(descr) { }
102 Base* operator()(Arg1 arg1, Arg2 arg2) const
103 { return new Derived(arg1, arg2); }
106 typedef std::map<std::string, const creator_base*> map_type;
110 void insert(const std::string& name, const creator_base* creat)
112 assoc.insert(std::make_pair(name, creat));
115 void insert(const char* name, const creator_base* creat)
117 assoc.insert(std::make_pair(std::string(name), creat));
121 typedef typename map_type::const_iterator iterator;
123 named_object_list2() { };
124 ~named_object_list2()
126 for (iterator it = begin(); it != end(); ++it)
130 Base* new_instance(const std::string& name, Arg1 arg1, Arg2 arg2) const
132 iterator it = assoc.find(name);
133 if (it != assoc.end())
134 return (*it->second)(arg1, arg2);
139 Base* new_instance(const char* name, Arg1 arg1, Arg2 arg2) const
141 return new_instance(std::string(name), arg1, arg2);
144 const std::string& get_name(iterator& it) const { return it->first; }
145 const std::string& get_descr(iterator& it) const
146 { return it->second->description; }
148 bool exists(const std::string& name) const
149 { return assoc.find(name) != assoc.end(); }
150 iterator begin() const { return assoc.begin(); }
151 iterator end() const { return assoc.end(); }
155 //===================
157 // "NOL" like in Named_Object_List....
158 #define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
160 #endif // !NAMED_OBJECT_LIST_H