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 size_t size() const { return assoc.size(); }
69 iterator begin() const { return assoc.begin(); }
70 iterator end() const { return assoc.end(); }
79 template <typename Base, typename Arg1, typename Arg2>
80 class named_object_list2 {
83 std::string description;
84 creator_base(const std::string& descr): description(descr) { }
85 creator_base(const char* descr): description(descr) { }
86 virtual Base* operator()(Arg1, Arg2) const = 0;
89 template <typename Derived>
90 struct creator: public creator_base {
91 creator(const std::string& descr): creator_base(descr) { }
92 creator(const char* descr): creator_base(descr) { }
93 Base* operator()(Arg1 arg1, Arg2 arg2) const
94 { return new Derived(arg1, arg2); }
97 typedef std::map<std::string, const creator_base*> map_type;
101 void insert(const std::string& name, const creator_base* creat)
103 assoc.insert(std::make_pair(name, creat));
106 void insert(const char* name, const creator_base* creat)
108 assoc.insert(std::make_pair(std::string(name), creat));
112 typedef typename map_type::const_iterator iterator;
114 named_object_list2() { };
115 ~named_object_list2()
117 for (iterator it = begin(); it != end(); ++it)
121 Base* new_instance(const std::string& name, Arg1 arg1, Arg2 arg2) const
123 iterator it = assoc.find(name);
124 if (it != assoc.end())
125 return (*it->second)(arg1, arg2);
130 Base* new_instance(const char* name, Arg1 arg1, Arg2 arg2) const
132 return new_instance(std::string(name), arg1, arg2);
135 const std::string& get_name(iterator& it) const { return it->first; }
136 const std::string& get_descr(iterator& it) const
137 { return it->second->description; }
139 bool exists(const std::string& name) const
140 { return assoc.find(name) != assoc.end(); }
141 iterator begin() const { return assoc.begin(); }
142 iterator end() const { return assoc.end(); }
146 //===================
148 #define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
150 #endif // !NAMED_OBJECT_LIST_H