1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 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 "class_list.h" |
---|
19 | #include "base_object.h" |
---|
20 | |
---|
21 | #include "list.h" |
---|
22 | #include "compiler.h" |
---|
23 | #include "debug.h" |
---|
24 | #include <string.h> |
---|
25 | #include <math.h> |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | \brief Creates a new ClassList |
---|
32 | */ |
---|
33 | ClassList::ClassList(const long& classID, const char* className) |
---|
34 | { |
---|
35 | this->next = NULL; |
---|
36 | this->className = className; |
---|
37 | this->classID = classID; |
---|
38 | this->objectList = new tList<BaseObject>; |
---|
39 | |
---|
40 | ++ClassList::classCount; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | \brief standard deconstructor |
---|
46 | |
---|
47 | */ |
---|
48 | ClassList::~ClassList () |
---|
49 | { |
---|
50 | --ClassList::classCount; |
---|
51 | } |
---|
52 | |
---|
53 | //! the first class that is registered |
---|
54 | ClassList* ClassList::first = NULL; |
---|
55 | |
---|
56 | //! the Count of classes |
---|
57 | unsigned int ClassList::classCount = 0; |
---|
58 | |
---|
59 | /** |
---|
60 | * Adds a new Object to the ClassList (and if necessary a new Class) |
---|
61 | * @param objectPointer Pointer to the Object at hand |
---|
62 | * @param classID ID of the Given ObjectType \see ClassID |
---|
63 | * @param className name of the Class to add |
---|
64 | */ |
---|
65 | void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className) |
---|
66 | { |
---|
67 | ClassList* regClass; |
---|
68 | |
---|
69 | if(ClassList::first == NULL) |
---|
70 | ClassList::first = regClass = new ClassList(classID, className); |
---|
71 | else |
---|
72 | { |
---|
73 | ClassList* tmp = ClassList::first; |
---|
74 | while (likely(tmp != NULL)) |
---|
75 | { |
---|
76 | if (tmp->classID == classID) |
---|
77 | { |
---|
78 | regClass = tmp; |
---|
79 | break; |
---|
80 | } |
---|
81 | |
---|
82 | if (tmp->next == NULL) |
---|
83 | tmp->next = regClass = new ClassList(classID, className); |
---|
84 | tmp = tmp->next; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | regClass->objectList->add(objectPointer); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * removes an Object from a the ClassList |
---|
93 | * @param objectPointer the Object to delete from the List |
---|
94 | */ |
---|
95 | void ClassList::removeFromClassList(BaseObject* objectPointer) |
---|
96 | { |
---|
97 | ClassList* tmp = ClassList::first; |
---|
98 | while (likely(tmp != NULL)) |
---|
99 | { |
---|
100 | if (objectPointer->isA(tmp->classID)) |
---|
101 | { |
---|
102 | tmp->objectList->remove(objectPointer); |
---|
103 | } |
---|
104 | |
---|
105 | tmp = tmp->next; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * checks if the BaseObject* object exists. |
---|
111 | * @param object the Pointer to a BaseObject to check if it exists |
---|
112 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
113 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
114 | * @todo: speed this up!! |
---|
115 | */ |
---|
116 | bool ClassList::exists(BaseObject* object, long classID) |
---|
117 | { |
---|
118 | if(unlikely(ClassList::first == NULL)) |
---|
119 | return false; |
---|
120 | else |
---|
121 | { |
---|
122 | ClassList* tmp = ClassList::first; |
---|
123 | while (likely(tmp != NULL)) |
---|
124 | { |
---|
125 | if (tmp->classID == classID || (classID == CL_NULL && tmp->classID == CL_BASE_OBJECT)) |
---|
126 | { |
---|
127 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
128 | BaseObject* enumBO = iterator->nextElement(); |
---|
129 | while (enumBO) |
---|
130 | { |
---|
131 | if (enumBO == object) |
---|
132 | return true; |
---|
133 | enumBO = iterator->nextElement(); |
---|
134 | } |
---|
135 | delete iterator; |
---|
136 | break; |
---|
137 | } |
---|
138 | tmp = tmp->next; |
---|
139 | } |
---|
140 | } |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Print out some very nice debug information |
---|
146 | */ |
---|
147 | void ClassList::debug(unsigned int debugLevel) |
---|
148 | { |
---|
149 | if (debugLevel > 2) |
---|
150 | debugLevel = 2; |
---|
151 | PRINT(0)("==========================\n"); |
---|
152 | PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); |
---|
153 | PRINT(0)("==========================\n"); |
---|
154 | PRINT(0)(" knows %d Classes\n\n", ClassList::classCount); |
---|
155 | ClassList* tmp = ClassList::first; |
---|
156 | char niceString[100]; |
---|
157 | unsigned int lenCount = 0; |
---|
158 | |
---|
159 | while (likely(tmp != NULL)) |
---|
160 | { |
---|
161 | if (debugLevel >= 1 || tmp->objectList->getSize() > 0) |
---|
162 | { |
---|
163 | lenCount = 1; |
---|
164 | while (pow(10,lenCount) <= tmp->objectList->getSize()) |
---|
165 | ++lenCount; |
---|
166 | for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) |
---|
167 | (niceString[i]) = ' '; |
---|
168 | niceString[30-strlen(tmp->className) - lenCount] = '\0'; |
---|
169 | |
---|
170 | PRINT(0)(" CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize()); |
---|
171 | |
---|
172 | if (debugLevel >=2 && tmp->objectList->getSize() > 0) |
---|
173 | { |
---|
174 | PRINT(0)(" Listing Instances:\n"); |
---|
175 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
176 | BaseObject* enumBO = iterator->nextElement(); |
---|
177 | while (enumBO) |
---|
178 | { |
---|
179 | PRINT(0)(" (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO); |
---|
180 | enumBO = iterator->nextElement(); |
---|
181 | } |
---|
182 | delete iterator; |
---|
183 | } |
---|
184 | } |
---|
185 | tmp = tmp->next; |
---|
186 | } |
---|
187 | PRINT(0)("==============CL=\n"); |
---|
188 | } |
---|