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 | NewObjectListBase::NewObjectListBase(const std::string& className) |
---|
23 | : _id(-1), _name(className) |
---|
24 | { |
---|
25 | if (NewObjectListBase::_classesByID == NULL) |
---|
26 | { |
---|
27 | NewObjectListBase::_classesByID = new classIDMap; |
---|
28 | assert (NewObjectListBase::_classesByName == NULL); |
---|
29 | NewObjectListBase::_classesByName = new classNameMap; |
---|
30 | } |
---|
31 | assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name/ID (key value)"); |
---|
32 | |
---|
33 | |
---|
34 | this->_id = NewObjectListBase::_idCounter++; |
---|
35 | |
---|
36 | (*NewObjectListBase::_classesByID)[this->_id] = this; |
---|
37 | (*NewObjectListBase::_classesByName)[this->_name] = this; |
---|
38 | } |
---|
39 | |
---|
40 | NewObjectListBase::~NewObjectListBase() |
---|
41 | { |
---|
42 | NewObjectListBase::_classesByName->erase(this->_name); |
---|
43 | NewObjectListBase::_classesByID->erase(this->_id); |
---|
44 | |
---|
45 | if (NewObjectListBase::_classesByID->empty()) |
---|
46 | { |
---|
47 | delete NewObjectListBase::_classesByID; |
---|
48 | NewObjectListBase::_classesByID = NULL; |
---|
49 | assert(NewObjectListBase::_classesByName != NULL); |
---|
50 | delete NewObjectListBase::_classesByName; |
---|
51 | NewObjectListBase::_classesByName = NULL; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | int NewObjectListBase::_idCounter = 0; |
---|
56 | NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL; |
---|
57 | NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL; |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | * @brief Checks if a Class with name already exists. |
---|
63 | * @param name The Name of the Class to check. |
---|
64 | * @return true if such a class already exists. |
---|
65 | */ |
---|
66 | bool NewObjectListBase::classNameExists(const std::string& name) |
---|
67 | { |
---|
68 | return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end()); |
---|
69 | // classNameMap::iterator it; |
---|
70 | // for (it = NewObjectListBase::_classesByID->begin(); it != NewObjectListBase::_classesByID->end(); it++) |
---|
71 | // if(*it != NULL && (*it)->name() != name) |
---|
72 | // return true; |
---|
73 | // return false; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * @brief Converts an ID into a ClassName String. |
---|
79 | * @param classID The ID to convert. |
---|
80 | * @return The ClassName or an empty string if the ID was not found. |
---|
81 | */ |
---|
82 | const std::string& NewObjectListBase::IDToString(int classID) |
---|
83 | { |
---|
84 | assert (NewObjectListBase::_classesByID != NULL); |
---|
85 | NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID); |
---|
86 | if (it != NewObjectListBase::_classesByID->end()) |
---|
87 | return (*it).second->name(); |
---|
88 | else |
---|
89 | { |
---|
90 | static std::string empty; |
---|
91 | return empty; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief Converts a String into an ID |
---|
98 | * @param className the Name of the Class to search for |
---|
99 | * @return The Classes ID if found, -1 otherwise. |
---|
100 | */ |
---|
101 | int NewObjectListBase::StringToID(const std::string& className) |
---|
102 | { |
---|
103 | assert (NewObjectListBase::_classesByName != NULL); |
---|
104 | NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className); |
---|
105 | if (it != NewObjectListBase::_classesByName->end()) |
---|
106 | return (*it).second->id(); |
---|
107 | else |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|