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 ~creator_base() { }
28 virtual Base* operator()() const = 0;
31 template <typename Derived>
32 struct creator: public creator_base {
33 creator(const std::string& descr): creator_base(descr) { }
34 creator(const char* descr): creator_base(descr) { }
35 Base* operator()() const { return new Derived(); }
38 typedef std::map<std::string, const creator_base*> map_type;
42 void insert(const std::string& name, const creator_base* creat)
44 assoc.insert(std::make_pair(name, creat));
47 void insert(const char* name, const creator_base* creat)
49 assoc.insert(std::make_pair(std::string(name), creat));
53 typedef typename map_type::const_iterator iterator;
55 named_object_list() { };
58 for (iterator it = begin(); it != end(); ++it)
62 Base* new_instance(const std::string& name) const
64 iterator it = assoc.find(name);
65 if (it != assoc.end())
66 return (*it->second)();
71 const std::string& get_name(iterator& it) const { return it->first; }
72 const std::string& get_descr(iterator& it) const
73 { return it->second->description; }
75 bool exists(const std::string& name) const
76 { return assoc.find(name) != assoc.end(); }
77 iterator begin() const { return assoc.begin(); }
78 iterator end() const { return assoc.end(); }
84 template <typename Base, typename Arg1, typename Arg2>
85 class named_object_list2 {
88 std::string description;
89 creator_base(const std::string& descr): description(descr) { }
90 creator_base(const char* descr): description(descr) { }
91 virtual ~creator_base() { }
92 virtual Base* operator()(Arg1, Arg2) const = 0;
95 template <typename Derived>
96 struct creator: public creator_base {
97 creator(const std::string& descr): creator_base(descr) { }
98 creator(const char* descr): creator_base(descr) { }
99 Base* operator()(Arg1 arg1, Arg2 arg2) const
100 { return new Derived(arg1, arg2); }
103 typedef std::map<std::string, const creator_base*> map_type;
107 void insert(const std::string& name, const creator_base* creat)
109 assoc.insert(std::make_pair(name, creat));
112 void insert(const char* name, const creator_base* creat)
114 assoc.insert(std::make_pair(std::string(name), creat));
118 typedef typename map_type::const_iterator iterator;
120 named_object_list2() { };
121 ~named_object_list2()
123 for (iterator it = begin(); it != end(); ++it)
127 Base* new_instance(const std::string& name, Arg1 arg1, Arg2 arg2) const
129 iterator it = assoc.find(name);
130 if (it != assoc.end())
131 return (*it->second)(arg1, arg2);
136 const std::string& get_name(iterator& it) const { return it->first; }
137 const std::string& get_descr(iterator& it) const
138 { return it->second->description; }
140 bool exists(const std::string& name) const
141 { return assoc.find(name) != assoc.end(); }
142 iterator begin() const { return assoc.begin(); }
143 iterator end() const { return assoc.end(); }
147 //===================
149 // "NOL" like in Named_Object_List....
150 #define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
152 #endif // !NAMED_OBJECT_LIST_H