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 | * Print out some very nice debug information |
---|
111 | */ |
---|
112 | void ClassList::debug(unsigned int debugLevel) |
---|
113 | { |
---|
114 | if (debugLevel > 2) |
---|
115 | debugLevel = 2; |
---|
116 | PRINT(0)("==========================\n"); |
---|
117 | PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); |
---|
118 | PRINT(0)("==========================\n"); |
---|
119 | PRINT(0)("has %d Elements\n\n", ClassList::classCount); |
---|
120 | ClassList* tmp = ClassList::first; |
---|
121 | char niceString[100]; |
---|
122 | unsigned int lenCount = 0; |
---|
123 | |
---|
124 | while (likely(tmp != NULL)) |
---|
125 | { |
---|
126 | if (debugLevel >= 1 || tmp->objectList->getSize() > 0) |
---|
127 | { |
---|
128 | lenCount = 1; |
---|
129 | while (pow(10,lenCount) <= tmp->objectList->getSize()) |
---|
130 | ++lenCount; |
---|
131 | for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) |
---|
132 | (niceString[i]) = ' '; |
---|
133 | niceString[30-strlen(tmp->className) - lenCount] = '\0'; |
---|
134 | |
---|
135 | PRINT(0)(" CLASS %s:%s %d instances\n", tmp->className, niceString, tmp->objectList->getSize()); |
---|
136 | |
---|
137 | if (debugLevel >=2 && tmp->objectList->getSize() > 0) |
---|
138 | { |
---|
139 | PRINT(0)(" Listing Instances:\n"); |
---|
140 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
141 | BaseObject* enumBO = iterator->nextElement(); |
---|
142 | while (enumBO) |
---|
143 | { |
---|
144 | PRINT(0)(" (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO); |
---|
145 | enumBO = iterator->nextElement(); |
---|
146 | } |
---|
147 | delete iterator; |
---|
148 | } |
---|
149 | } |
---|
150 | tmp = tmp->next; |
---|
151 | } |
---|
152 | PRINT(0)("==============CL=\n"); |
---|
153 | } |
---|