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(); }
77 template <typename Base, typename Arg1, typename Arg2>
78 class named_object_list2 {
81 std::string description;
82 creator_base(const std::string& descr): description(descr) { }
83 creator_base(const char* descr): description(descr) { }
84 virtual Base* operator()(Arg1, Arg2) const = 0;
87 template <typename Derived>
88 struct creator: public creator_base {
89 creator(const std::string& descr): creator_base(descr) { }
90 creator(const char* descr): creator_base(descr) { }
91 Base* operator()(Arg1 arg1, Arg2 arg2) const
92 { return new Derived(arg1, arg2); }
95 typedef std::map<std::string, const creator_base*> map_type;
99 void insert(const std::string& name, const creator_base* creat)
101 assoc.insert(std::make_pair(name, creat));
104 void insert(const char* name, const creator_base* creat)
106 assoc.insert(std::make_pair(std::string(name), creat));
110 typedef typename map_type::const_iterator iterator;
112 named_object_list2() { };
113 ~named_object_list2()
115 for (iterator it = begin(); it != end(); ++it)
119 Base* new_instance(const std::string& name, Arg1 arg1, Arg2 arg2) const
121 iterator it = assoc.find(name);
122 if (it != assoc.end())
123 return (*it->second)(arg1, arg2);
128 Base* new_instance(const char* name, Arg1 arg1, Arg2 arg2) const
130 return new_instance(std::string(name), arg1, arg2);
133 const std::string& get_name(iterator& it) const { return it->first; }
134 const std::string& get_descr(iterator& it) const
135 { return it->second->description; }
137 bool exists(const std::string& name) const
138 { return assoc.find(name) != assoc.end(); }
139 iterator begin() const { return assoc.begin(); }
140 iterator end() const { return assoc.end(); }
144 //===================
146 #define NOL_INSERT(name, descr, class) insert(name, new creator<class>(descr))
148 #endif // !NAMED_OBJECT_LIST_H