1 #ifndef NAMED_OBJECT_LIST_H
2 #define NAMED_OBJECT_LIST_H
9 template <typename Base>
10 class named_object_list {
13 std::string description;
14 creator_base(const std::string& descr): description(descr) { }
15 creator_base(const char* descr): description(descr) { }
16 virtual Base* operator()() const = 0;
19 template <typename Derived>
20 struct creator: public creator_base {
21 creator(const std::string& descr): creator_base(descr) { }
22 creator(const char* descr): creator_base(descr) { }
23 Base* operator()() const { return new Derived(); }
26 typedef std::map<std::string, const creator_base*> map_type;
30 void insert(const std::string& name, const creator_base* creat)
32 assoc.insert(std::make_pair(name, creat));
35 void insert(const char* name, const creator_base* creat)
37 assoc.insert(std::make_pair(std::string(name), creat));
41 typedef typename map_type::const_iterator iterator;
43 named_object_list() { };
46 for (iterator it = begin(); it != end(); ++it)
50 Base* new_instance(const std::string& name) const
52 iterator it = assoc.find(name);
53 if (it != assoc.end())
54 return (*it->second)();
59 Base* new_instance(const char* name) const
61 return new_instance(std::string(name));
64 const std::string& get_name(iterator& it) const { return it->first; }
65 const std::string& get_descr(iterator& it) const
66 { return it->second->description; }
68 bool exists(const std::string& name) const
69 { return assoc.find(name) != assoc.end(); }
70 iterator begin() const { return assoc.begin(); }
71 iterator end() const { return assoc.end(); }
80 template <typename Base, typename Arg1, typename Arg2>
81 class named_object_list2 {
84 std::string description;
85 creator_base(const std::string& descr): description(descr) { }
86 creator_base(const char* descr): description(descr) { }
87 virtual Base* operator()(Arg1, Arg2) const = 0;
90 template <typename Derived>
91 struct creator: public creator_base {
92 creator(const std::string& descr): creator_base(descr) { }
93 creator(const char* descr): creator_base(descr) { }
94 Base* operator()(Arg1 arg1, Arg2 arg2) const
95 { return new Derived(arg1, arg2); }
98 typedef std::map<std::string, const creator_base*> map_type;
102 void insert(const std::string& name, const creator_base* creat)
104 assoc.insert(std::make_pair(name, creat));
107 void insert(const char* name, const creator_base* creat)
109 assoc.insert(std::make_pair(std::string(name), creat));
113 typedef typename map_type::const_iterator iterator;
115 named_object_list2() { };
116 ~named_object_list2()
118 for (iterator it = begin(); it != end(); ++it)
122 Base* new_instance(const std::string& name, Arg1 arg1, Arg2 arg2) const
124 iterator it = assoc.find(name);
125 if (it != assoc.end())
126 return (*it->second)(arg1, arg2);
131 Base* new_instance(const char* name, Arg1 arg1, Arg2 arg2) const
133 return new_instance(std::string(name), 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 #define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
151 #endif // !NAMED_OBJECT_LIST_H