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 | int id() const { return _id; }; |
---|
26 | const std::string& name() const { return _name; }; |
---|
27 | bool operator==(int id) const { return _id == id; }; |
---|
28 | bool operator==(const std::string& name) const { return _name == name; }; |
---|
29 | |
---|
30 | |
---|
31 | /// Comparing operators. |
---|
32 | bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); }; |
---|
33 | bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); }; |
---|
34 | |
---|
35 | |
---|
36 | static unsigned int classCount() { return _idCounter; }; |
---|
37 | static const std::string& IDToString(int classID); |
---|
38 | static int StringToID(const std::string& className); |
---|
39 | |
---|
40 | static const std::list<std::string>& getClassNames(); |
---|
41 | |
---|
42 | protected: |
---|
43 | NewObjectListBase(const std::string& className); |
---|
44 | ~NewObjectListBase(); |
---|
45 | |
---|
46 | private: |
---|
47 | NewObjectListBase(const NewObjectListBase&); |
---|
48 | |
---|
49 | static bool classNameExists(const std::string& className); |
---|
50 | |
---|
51 | private: |
---|
52 | int _id; |
---|
53 | std::string _name; |
---|
54 | |
---|
55 | private: |
---|
56 | typedef std::set<NewObjectListBase*> cList; |
---|
57 | |
---|
58 | static int _idCounter; //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe. |
---|
59 | static cList* _classes; //!< A Set of all the classes in existance. |
---|
60 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | ///////////////////////// |
---|
65 | //// TEMPLATISATION ///// |
---|
66 | ///////////////////////// |
---|
67 | //! Defines a ObjectsList handler for objects of type T. |
---|
68 | /** |
---|
69 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
70 | */ |
---|
71 | template<class T> class NewObjectList : public NewObjectListBase |
---|
72 | { |
---|
73 | public: |
---|
74 | typedef std::list<T*> list; |
---|
75 | typedef typename list::iterator iterator; |
---|
76 | |
---|
77 | |
---|
78 | public: |
---|
79 | NewObjectList(const std::string& name); |
---|
80 | ~NewObjectList(); |
---|
81 | |
---|
82 | T* getObject(const std::string& name) const; |
---|
83 | inline const list& objects() const { return _objects; }; |
---|
84 | |
---|
85 | |
---|
86 | private: |
---|
87 | //! the copy constructor will be hidden. |
---|
88 | NewObjectList(const NewObjectList& definer) {}; |
---|
89 | |
---|
90 | list _objects; |
---|
91 | }; |
---|
92 | |
---|
93 | template <class T> |
---|
94 | NewObjectList<T>::NewObjectList(const std::string& name) |
---|
95 | : NewObjectListBase(name) |
---|
96 | {} |
---|
97 | |
---|
98 | template <class T> |
---|
99 | NewObjectList<T>::~NewObjectList() |
---|
100 | { |
---|
101 | assert(_objects.empty()); |
---|
102 | } |
---|
103 | |
---|
104 | template <class T> |
---|
105 | T* NewObjectList<T>::getObject(const std::string& name) const |
---|
106 | { |
---|
107 | iterator it = std::find(this->_objects.begin(), this->_objects.end(), name); |
---|
108 | if (it != this->_objects.end()) |
---|
109 | return *it; |
---|
110 | else |
---|
111 | return NULL; |
---|
112 | } |
---|
113 | |
---|
114 | #endif /* _NEW_OBJECT_LIST_H */ |
---|