1 | /*! |
---|
2 | * @file new_object_list.h |
---|
3 | * @brief Definition of a dynamically allocating ClassID |
---|
4 | * |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef _NEW_OBJECT_LIST_H |
---|
8 | #define _NEW_OBJECT_LIST_H |
---|
9 | |
---|
10 | #include "type_info.h" |
---|
11 | #include <set> |
---|
12 | #include <list> |
---|
13 | #include <string> |
---|
14 | |
---|
15 | |
---|
16 | #define NewObjectListDeclaration(ClassName) \ |
---|
17 | static NewObjectList<ClassName> objectList |
---|
18 | |
---|
19 | #define NewObjectListDefinition(ClassName) \ |
---|
20 | NewObjectList<ClassName> ClassName::objectList(#ClassName) |
---|
21 | |
---|
22 | class NewObjectListBase |
---|
23 | { |
---|
24 | public: |
---|
25 | class IteratorBase { }; |
---|
26 | |
---|
27 | public: |
---|
28 | int id() const { return _id; }; |
---|
29 | const std::string& name() const { return _name; }; |
---|
30 | bool operator==(int id) const { return _id == id; }; |
---|
31 | bool operator==(const std::string& name) const { return _name == name; }; |
---|
32 | |
---|
33 | |
---|
34 | /// Comparing operators. |
---|
35 | bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); }; |
---|
36 | bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); }; |
---|
37 | |
---|
38 | static unsigned int classCount() { return _idCounter; }; |
---|
39 | static const std::string& IDToString(int classID); |
---|
40 | static int StringToID(const std::string& className); |
---|
41 | |
---|
42 | static const std::list<std::string>& getClassNames(); |
---|
43 | |
---|
44 | protected: |
---|
45 | |
---|
46 | NewObjectListBase(const std::string& className); |
---|
47 | ~NewObjectListBase(); |
---|
48 | |
---|
49 | private: |
---|
50 | NewObjectListBase(const NewObjectListBase&); |
---|
51 | |
---|
52 | static bool classNameExists(const std::string& className); |
---|
53 | |
---|
54 | private: |
---|
55 | int _id; |
---|
56 | std::string _name; |
---|
57 | |
---|
58 | private: |
---|
59 | typedef std::set<NewObjectListBase*> cList; |
---|
60 | |
---|
61 | static int _idCounter; //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe. |
---|
62 | static cList* _classes; //!< A Set of all the classes in existance. |
---|
63 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
64 | }; |
---|
65 | |
---|
66 | |
---|
67 | ///////////////////////// |
---|
68 | //// TEMPLATISATION ///// |
---|
69 | ///////////////////////// |
---|
70 | //! Defines a ObjectsList handler for objects of type T. |
---|
71 | /** |
---|
72 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
73 | */ |
---|
74 | template<class T> |
---|
75 | class NewObjectList : public NewObjectListBase |
---|
76 | { |
---|
77 | public: |
---|
78 | typedef std::list<T*> list; |
---|
79 | typedef typename list::iterator iterator; |
---|
80 | |
---|
81 | class Iterator : public NewObjectListBase::IteratorBase |
---|
82 | { |
---|
83 | public: |
---|
84 | Iterator(iterator it) { it = it; } |
---|
85 | typename NewObjectList::iterator it; |
---|
86 | }; |
---|
87 | |
---|
88 | public: |
---|
89 | NewObjectList(const std::string& name); |
---|
90 | ~NewObjectList(); |
---|
91 | |
---|
92 | T* getObject(const std::string& name) const; |
---|
93 | inline const list& objects() const { return _objects; }; |
---|
94 | |
---|
95 | NewObjectListBase::IteratorBase* registerObject(T* object); |
---|
96 | void unregisterObject(const IteratorBase& iterator); |
---|
97 | |
---|
98 | private: |
---|
99 | //! the copy constructor will be hidden. |
---|
100 | NewObjectList(const NewObjectList& definer) {}; |
---|
101 | |
---|
102 | private: |
---|
103 | list _objects; |
---|
104 | }; |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | ///////////////////////// |
---|
111 | //// IMPLEMENTATION ///// |
---|
112 | ///////////////////////// |
---|
113 | template <class T> |
---|
114 | NewObjectList<T>::NewObjectList(const std::string& name) |
---|
115 | : NewObjectListBase(name) |
---|
116 | {} |
---|
117 | |
---|
118 | template <class T> |
---|
119 | NewObjectList<T>::~NewObjectList() |
---|
120 | { |
---|
121 | // assert(_objects.empty()); |
---|
122 | } |
---|
123 | |
---|
124 | template <class T> |
---|
125 | T* NewObjectList<T>::getObject(const std::string& name) const |
---|
126 | { |
---|
127 | iterator it = std::find(this->_objects.begin(), this->_objects.end(), name); |
---|
128 | if (it != this->_objects.end()) |
---|
129 | return *it; |
---|
130 | else |
---|
131 | return NULL; |
---|
132 | } |
---|
133 | |
---|
134 | template <class T> |
---|
135 | NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object) |
---|
136 | { |
---|
137 | this->_objects.push_front(object); |
---|
138 | return new Iterator(this->_objects.begin()); |
---|
139 | } |
---|
140 | |
---|
141 | template <class T> |
---|
142 | void NewObjectList<T>::unregisterObject(const IteratorBase& iterator) |
---|
143 | { |
---|
144 | this->_objects.erase(static_cast<Iterator>(iterator)); |
---|
145 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | #endif /* _NEW_OBJECT_LIST_H */ |
---|