1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "new_object_list.h" |
---|
19 | #include <cassert> |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's) |
---|
24 | * @param className The Name of the Class to create an ObjectList for. |
---|
25 | * @param id The ID if you like, or -1 otherwise. |
---|
26 | * @return a new NewObejctList |
---|
27 | */ |
---|
28 | NewObjectListBase::NewObjectListBase(const std::string& className, int id) |
---|
29 | : _name(className) |
---|
30 | { |
---|
31 | if (NewObjectListBase::_classesByID == NULL) |
---|
32 | { |
---|
33 | NewObjectListBase::_classesByID = new classIDMap; |
---|
34 | assert (NewObjectListBase::_classesByName == NULL); |
---|
35 | NewObjectListBase::_classesByName = new classNameMap; |
---|
36 | } |
---|
37 | assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)"); |
---|
38 | |
---|
39 | if (id == -1) |
---|
40 | { |
---|
41 | id = NewObjectListBase::_classesByID->size(); |
---|
42 | // searching for a free ID |
---|
43 | while (NewObjectListBase::classIDExists(id)) ++id; |
---|
44 | } |
---|
45 | assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)"); |
---|
46 | |
---|
47 | _id = id; |
---|
48 | /// Some Output, that will fall out later |
---|
49 | //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl; |
---|
50 | |
---|
51 | this->_identity = NewClassID(this); |
---|
52 | (*NewObjectListBase::_classesByID)[this->_identity.id()] = this; |
---|
53 | (*NewObjectListBase::_classesByName)[this->_identity.name()] = this; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * Destructor. |
---|
59 | * |
---|
60 | * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps. |
---|
61 | */ |
---|
62 | NewObjectListBase::~NewObjectListBase() |
---|
63 | { |
---|
64 | assert (NewObjectListBase::_classesByName != NULL && NewObjectListBase::_classesByID != NULL); |
---|
65 | /* |
---|
66 | std::cout << "Erasing: " << this->_name << " "<< this->_id << std::endl; |
---|
67 | std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl; |
---|
68 | std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl; |
---|
69 | */ |
---|
70 | NewObjectListBase::_classesByID->erase(this->_identity.id()); |
---|
71 | NewObjectListBase::_classesByName->erase(this->_identity.name()); |
---|
72 | |
---|
73 | if (NewObjectListBase::_classesByID->empty()) |
---|
74 | { |
---|
75 | delete NewObjectListBase::_classesByID; |
---|
76 | NewObjectListBase::_classesByID = NULL; |
---|
77 | assert(NewObjectListBase::_classesByName != NULL); |
---|
78 | delete NewObjectListBase::_classesByName; |
---|
79 | NewObjectListBase::_classesByName = NULL; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL; |
---|
84 | NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL; |
---|
85 | |
---|
86 | /** |
---|
87 | * @returns the Registered Class Count. |
---|
88 | */ |
---|
89 | unsigned int NewObjectListBase::classCount() |
---|
90 | { |
---|
91 | assert (NewObjectListBase::_classesByID != NULL); |
---|
92 | return NewObjectListBase::_classesByID->size(); |
---|
93 | }; |
---|
94 | |
---|
95 | /** |
---|
96 | * @brief Checks if a Class with name already exists. |
---|
97 | * @param name The Name of the Class to check. |
---|
98 | * @return true if such a class already exists. |
---|
99 | */ |
---|
100 | bool NewObjectListBase::classNameExists(const std::string& name) |
---|
101 | { |
---|
102 | return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end()); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * @brief Checks if a Class with name already exists. |
---|
107 | * @param name The Name of the Class to check. |
---|
108 | * @return true if such a class already exists. |
---|
109 | */ |
---|
110 | bool NewObjectListBase::classIDExists(int id) |
---|
111 | { |
---|
112 | return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end()); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | * @brief Searches for a ObjectList with the ID classID |
---|
118 | * @param classID the ID to search for. |
---|
119 | * @return The NewObjectList if found, NULL otherwise. |
---|
120 | */ |
---|
121 | const NewObjectListBase* const NewObjectListBase::getObjectList(int classID) |
---|
122 | { |
---|
123 | assert (NewObjectListBase::_classesByID != NULL); |
---|
124 | NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID); |
---|
125 | if (it != NewObjectListBase::_classesByID->end()) |
---|
126 | return (*it).second; |
---|
127 | else |
---|
128 | return NULL; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * @brief Searches for a ObjectList with the Name className |
---|
133 | * @param className the Name to search for. |
---|
134 | * @return The NewObjectList if found, NULL otherwise. |
---|
135 | */ |
---|
136 | const NewObjectListBase* const NewObjectListBase::getObjectList(const std::string& className) |
---|
137 | { |
---|
138 | assert (NewObjectListBase::_classesByName != NULL); |
---|
139 | NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className); |
---|
140 | if (it != NewObjectListBase::_classesByName->end()) |
---|
141 | return (*it).second; |
---|
142 | else |
---|
143 | return NULL; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * @brief Searches for a ObjectList with the NewClassID classID |
---|
148 | * @param classID the ID to search for. |
---|
149 | * @return The NewObjectList if found, NULL otherwise. |
---|
150 | */ |
---|
151 | const NewObjectListBase* const NewObjectListBase::getObjectList(const NewClassID& classID) |
---|
152 | { |
---|
153 | return NewObjectListBase::getObjectList(classID.id()); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. |
---|
158 | * @param classID the ID of the List. |
---|
159 | * @param objectName the Name of the Object to search for |
---|
160 | */ |
---|
161 | BaseObject* NewObjectListBase::getBaseObject(int classID, const std::string& objectName) |
---|
162 | { |
---|
163 | const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); |
---|
164 | |
---|
165 | if (base != NULL) |
---|
166 | return base->getBaseObject(objectName); |
---|
167 | else |
---|
168 | return NULL; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching className. |
---|
173 | * @param className the Name of the List. |
---|
174 | * @param objectName the Name of the Object to search for |
---|
175 | */ |
---|
176 | BaseObject* NewObjectListBase::getBaseObject(const std::string& className, const std::string& objectName) |
---|
177 | { |
---|
178 | const NewObjectListBase* const base = NewObjectListBase::getObjectList(className); |
---|
179 | |
---|
180 | if (base != NULL) |
---|
181 | return base->getBaseObject(objectName); |
---|
182 | else |
---|
183 | return NULL; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. |
---|
188 | * @param classID The NewClassID of the List. |
---|
189 | * @param objectName the Name of the Object to search for |
---|
190 | */ |
---|
191 | BaseObject* NewObjectListBase::getBaseObject(const NewClassID& classID, const std::string& objectName) |
---|
192 | { |
---|
193 | const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); |
---|
194 | |
---|
195 | if (base != NULL) |
---|
196 | return base->getBaseObject(objectName); |
---|
197 | else |
---|
198 | return NULL; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Converts an ID into a ClassName String. |
---|
204 | * @param classID The ID to convert. |
---|
205 | * @return The ClassName or an empty string if the ID was not found. |
---|
206 | */ |
---|
207 | const std::string& NewObjectListBase::IDToString(int classID) |
---|
208 | { |
---|
209 | const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); |
---|
210 | |
---|
211 | if (base != NULL) |
---|
212 | return base->name(); |
---|
213 | else |
---|
214 | { |
---|
215 | static std::string empty; |
---|
216 | return empty; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * @brief Converts a String into an ID |
---|
223 | * @param className the Name of the Class to search for |
---|
224 | * @return The Classes ID if found, -1 otherwise. |
---|
225 | */ |
---|
226 | int NewObjectListBase::StringToID(const std::string& className) |
---|
227 | { |
---|
228 | const NewObjectListBase* const base = NewObjectListBase::getObjectList(className); |
---|
229 | |
---|
230 | if (base != NULL) |
---|
231 | return base->id(); |
---|
232 | else |
---|
233 | return -1; |
---|
234 | } |
---|
235 | |
---|