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 <vector> |
---|
14 | #include <string> |
---|
15 | |
---|
16 | |
---|
17 | #define NewObjectListDeclaration(ClassName) \ |
---|
18 | static NewObjectList<ClassName> objectList |
---|
19 | |
---|
20 | #define NewObjectListDefinition(ClassName) \ |
---|
21 | NewObjectList<ClassName> ClassName::objectList(#ClassName) |
---|
22 | |
---|
23 | class NewObjectListBase |
---|
24 | { |
---|
25 | public: |
---|
26 | class IteratorBase { }; |
---|
27 | |
---|
28 | public: |
---|
29 | int id() const { return _id; }; |
---|
30 | const std::string& name() const { return _name; }; |
---|
31 | bool operator==(int id) const { return _id == id; }; |
---|
32 | bool operator==(const std::string& name) const { return _name == name; }; |
---|
33 | |
---|
34 | |
---|
35 | /// Comparing operators. |
---|
36 | bool compareName(const NewObjectListBase& more) const { return this->_name < more.name(); }; |
---|
37 | bool compareID(const NewObjectListBase& more) const { return this->_id < more.id(); }; |
---|
38 | |
---|
39 | virtual void debug() const = 0; |
---|
40 | |
---|
41 | static unsigned int classCount() { return _idCounter; }; |
---|
42 | static const std::string& IDToString(int classID); |
---|
43 | static int StringToID(const std::string& className); |
---|
44 | |
---|
45 | static const std::list<std::string>& getClassNames(); |
---|
46 | |
---|
47 | virtual void unregisterObject(std::list<NewObjectListBase::IteratorBase*> _iterators) = 0; |
---|
48 | |
---|
49 | protected: |
---|
50 | NewObjectListBase(const std::string& className); |
---|
51 | virtual ~NewObjectListBase(); |
---|
52 | |
---|
53 | private: |
---|
54 | NewObjectListBase(const NewObjectListBase&); |
---|
55 | |
---|
56 | static bool classNameExists(const std::string& className); |
---|
57 | |
---|
58 | protected: |
---|
59 | typedef std::set<NewObjectListBase*> cSet; //!< The Generic Set. |
---|
60 | typedef std::vector<NewObjectListBase*> cVector; //!< The |
---|
61 | |
---|
62 | int _id; //!< The ID of the class. |
---|
63 | std::string _name; //!< The Name of the Class. |
---|
64 | |
---|
65 | cVector _typeOfList; //!< A List of all classes this class is derived of, and the class itself, ordered by age of addition. |
---|
66 | cSet _typeOfSet; //!< A Set of all classes this is derived from and the class itself (for isA). |
---|
67 | private: |
---|
68 | |
---|
69 | static int _idCounter; //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe. |
---|
70 | static cSet* _classes; //!< A Set of all the classes in existance. |
---|
71 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
72 | }; |
---|
73 | |
---|
74 | |
---|
75 | ///////////////////////// |
---|
76 | //// TEMPLATISATION ///// |
---|
77 | ///////////////////////// |
---|
78 | //! Defines a ObjectsList handler for objects of type T. |
---|
79 | /** |
---|
80 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
81 | */ |
---|
82 | template<class T> |
---|
83 | class NewObjectList : public NewObjectListBase |
---|
84 | { |
---|
85 | public: |
---|
86 | typedef std::list<T*> list; |
---|
87 | typedef typename list::iterator iterator; |
---|
88 | typedef typename list::const_iterator const_iterator; |
---|
89 | |
---|
90 | class Iterator : public NewObjectListBase::IteratorBase |
---|
91 | { |
---|
92 | public: |
---|
93 | Iterator(iterator it) { it = it; } |
---|
94 | typename NewObjectList::iterator it; |
---|
95 | }; |
---|
96 | |
---|
97 | public: |
---|
98 | NewObjectList(const std::string& name); |
---|
99 | ~NewObjectList(); |
---|
100 | |
---|
101 | T* getObject(const std::string& name) const; |
---|
102 | inline const list& objects() const { return _objects; }; |
---|
103 | |
---|
104 | NewObjectListBase::IteratorBase* registerObject(T* object, NewObjectListBase* objectList); |
---|
105 | void unregisterObject(const IteratorBase& iterator); |
---|
106 | virtual void unregisterObject(std::list<NewObjectListBase::IteratorBase*> _iterators) = 0; |
---|
107 | |
---|
108 | virtual void debug() const; |
---|
109 | |
---|
110 | private: |
---|
111 | //! the copy constructor will be hidden. |
---|
112 | NewObjectList(const NewObjectList& definer) {}; |
---|
113 | |
---|
114 | private: |
---|
115 | list _objects; |
---|
116 | }; |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | ///////////////////////// |
---|
123 | //// IMPLEMENTATION ///// |
---|
124 | ///////////////////////// |
---|
125 | template <class T> |
---|
126 | NewObjectList<T>::NewObjectList(const std::string& name) |
---|
127 | : NewObjectListBase(name) |
---|
128 | {} |
---|
129 | |
---|
130 | template <class T> |
---|
131 | NewObjectList<T>::~NewObjectList() |
---|
132 | { |
---|
133 | // assert(_objects.empty()); |
---|
134 | } |
---|
135 | |
---|
136 | template <class T> |
---|
137 | T* NewObjectList<T>::getObject(const std::string& name) const |
---|
138 | { |
---|
139 | iterator it = std::find(this->_objects.begin(), this->_objects.end(), name); |
---|
140 | if (it != this->_objects.end()) |
---|
141 | return *it; |
---|
142 | else |
---|
143 | return NULL; |
---|
144 | } |
---|
145 | |
---|
146 | template <class T> |
---|
147 | NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object, NewObjectListBase* objectList) |
---|
148 | { |
---|
149 | if(this->_typeOfList.empty()) |
---|
150 | { |
---|
151 | this->_typeOfList.push_back(objectList); |
---|
152 | } |
---|
153 | |
---|
154 | this->_objects.push_front(object); |
---|
155 | return new Iterator(this->_objects.begin()); |
---|
156 | } |
---|
157 | |
---|
158 | template <class T> |
---|
159 | void NewObjectList<T>::unregisterObject(const IteratorBase& iterator) |
---|
160 | { |
---|
161 | this->_objects.erase(static_cast<Iterator>(iterator)); |
---|
162 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
163 | } |
---|
164 | |
---|
165 | #include <iostream> |
---|
166 | |
---|
167 | template <class T> |
---|
168 | void NewObjectList<T>::debug() const |
---|
169 | { |
---|
170 | const_iterator it; |
---|
171 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
172 | { |
---|
173 | std::cout << (*it)->name() << std::endl; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | #endif /* _NEW_OBJECT_LIST_H */ |
---|