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: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_OBJECT_MANAGER |
---|
16 | |
---|
17 | #include "object_manager.h" |
---|
18 | #include "garbage_collector.h" |
---|
19 | #include "list.h" |
---|
20 | |
---|
21 | #include "debug.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | /** |
---|
26 | * Initializes a FastFactory |
---|
27 | * @param classID the ClassID this Class belongs to (the top-most) |
---|
28 | * @param fastFactoryName the Name of the ObjectClass-handled here |
---|
29 | * @return a new FastFactory |
---|
30 | */ |
---|
31 | FastFactory::FastFactory (ClassID classID, const char* fastFactoryName) |
---|
32 | { |
---|
33 | this->setClassID(CL_FAST_FACTORY, "FastFactory"); |
---|
34 | this->setName(fastFactoryName); |
---|
35 | |
---|
36 | this->storedClassID = classID; |
---|
37 | this->next = NULL; |
---|
38 | |
---|
39 | this->deadList = NULL; |
---|
40 | this->unusedContainers = NULL; |
---|
41 | |
---|
42 | this->storedDeadObjects = 0; |
---|
43 | |
---|
44 | FastFactory::registerFastFactory(this); |
---|
45 | } |
---|
46 | |
---|
47 | /** a reference to the First FastFactory */ |
---|
48 | FastFactory* FastFactory::first = NULL; |
---|
49 | |
---|
50 | /** |
---|
51 | * destructor |
---|
52 | * deletes all the Instances of the FastFactory. |
---|
53 | */ |
---|
54 | FastFactory::~FastFactory () |
---|
55 | { |
---|
56 | if (this == first) |
---|
57 | this->first = NULL; |
---|
58 | |
---|
59 | if (this->next) |
---|
60 | delete this->next; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * registers a Factory to the List of known factories. |
---|
65 | * @param fastFactory The factory to add |
---|
66 | * |
---|
67 | * needed, to step through all the FastFactories. |
---|
68 | */ |
---|
69 | void FastFactory::registerFastFactory(FastFactory* fastFactory) |
---|
70 | { |
---|
71 | PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName()); |
---|
72 | |
---|
73 | if( FastFactory::first == NULL) |
---|
74 | FastFactory::first = fastFactory; |
---|
75 | else |
---|
76 | { |
---|
77 | FastFactory* tmpFac = FastFactory::first; |
---|
78 | while( tmpFac->next != NULL) |
---|
79 | tmpFac = tmpFac->next; |
---|
80 | tmpFac->setNext(fastFactory); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | * searches for a FastFactory |
---|
87 | * @param factoryName the Name of the Factory to search for (not used) |
---|
88 | * @param classID the ClassID of the FastFactory to search for |
---|
89 | * @returns true if found, false otherwise. |
---|
90 | */ |
---|
91 | FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName) |
---|
92 | { |
---|
93 | if (FastFactory::first == NULL) |
---|
94 | return NULL; |
---|
95 | else |
---|
96 | { |
---|
97 | FastFactory* tmpFac = FastFactory::first; |
---|
98 | while (tmpFac != NULL) |
---|
99 | { |
---|
100 | if (tmpFac->storedClassID == classID) |
---|
101 | return tmpFac; |
---|
102 | tmpFac = tmpFac->next; |
---|
103 | } |
---|
104 | } |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Removes all the stored Containers, and sets the Lists back to emptienes. |
---|
110 | * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !! |
---|
111 | */ |
---|
112 | void FastFactory::flushAll(bool hardFLUSH) |
---|
113 | { |
---|
114 | FastFactory* tmpFac = FastFactory::first; |
---|
115 | while (tmpFac != NULL) |
---|
116 | { |
---|
117 | tmpFac->flush(hardFLUSH); |
---|
118 | tmpFac = tmpFac->next; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /** |
---|
124 | * ereases all the remaining containers, without deleting the stored Objects inside of them. |
---|
125 | * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !! |
---|
126 | */ |
---|
127 | void FastFactory::flush(bool hardFLUSH) |
---|
128 | { |
---|
129 | FastObjectMember* tmpMember = this->deadList, *delMember = NULL; |
---|
130 | while (tmpMember != NULL) |
---|
131 | { |
---|
132 | delMember = tmpMember; |
---|
133 | tmpMember = tmpMember->next; |
---|
134 | if (unlikely(hardFLUSH == true)) |
---|
135 | delete delMember->objectPointer; |
---|
136 | delete delMember; |
---|
137 | } |
---|
138 | this->deadList = NULL; |
---|
139 | |
---|
140 | tmpMember = this->unusedContainers; |
---|
141 | while (tmpMember != NULL) |
---|
142 | { |
---|
143 | delMember = tmpMember; |
---|
144 | tmpMember = tmpMember->next; |
---|
145 | delete delMember; |
---|
146 | } |
---|
147 | this->unusedContainers = NULL; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * generates count new Object of the Corresponding class. (precaching) |
---|
152 | * @param count How many instances of the class should be generated. |
---|
153 | */ |
---|
154 | void FastFactory::prepare(unsigned int count) |
---|
155 | { |
---|
156 | /* if (this->storedDeadObjects + this->storedLivingObjects >= count) |
---|
157 | { |
---|
158 | PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName()); |
---|
159 | }*/ |
---|
160 | for (int i = this->storedDeadObjects; i < count; i++) |
---|
161 | { |
---|
162 | this->fabricate(); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * gives back live to one Object. |
---|
168 | * @return the Object to resurrect. |
---|
169 | */ |
---|
170 | BaseObject* FastFactory::resurrect() |
---|
171 | { |
---|
172 | PRINTF(4)("Resurecting Object of type %s\n", this->getName()); |
---|
173 | if (unlikely(this->deadList == NULL)) |
---|
174 | { |
---|
175 | PRINTF(2)("The deadList of Class %s is empty, this may be either because it has not been filled yet, or the cache is to small.\n" \ |
---|
176 | "Fabricating a new %s", this->getName(), this->getName()); |
---|
177 | this->fabricate(); |
---|
178 | return this->resurrect(); |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | FastObjectMember* tmpC = deadList; |
---|
183 | this->deadList = this->deadList->next; |
---|
184 | |
---|
185 | tmpC->next = this->unusedContainers; |
---|
186 | this->unusedContainers = tmpC; |
---|
187 | |
---|
188 | return tmpC->objectPointer; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * gives back live to one Object. |
---|
194 | * @param classID the class From which to resurrect an Object. |
---|
195 | * @return the Object to resurrect, NULL if classID is not found. |
---|
196 | */ |
---|
197 | BaseObject* FastFactory::resurrect(ClassID classID) |
---|
198 | { |
---|
199 | FastFactory* tmpFac = FastFactory::getFirst(); |
---|
200 | |
---|
201 | while (tmpFac != NULL) |
---|
202 | { |
---|
203 | if (classID == tmpFac->storedClassID) |
---|
204 | return tmpFac->resurrect(); |
---|
205 | tmpFac = tmpFac->next; |
---|
206 | } |
---|
207 | return NULL; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection |
---|
212 | * @param object the Object to kill. |
---|
213 | * |
---|
214 | * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode. |
---|
215 | */ |
---|
216 | void FastFactory::kill(BaseObject* object) |
---|
217 | { |
---|
218 | FastObjectMember* tmpC; |
---|
219 | if (unlikely(this->unusedContainers == NULL)) |
---|
220 | { |
---|
221 | tmpC = new FastObjectMember; |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | tmpC = this->unusedContainers; |
---|
226 | this->unusedContainers = this->unusedContainers->next; |
---|
227 | } |
---|
228 | |
---|
229 | tmpC->next = this->deadList; |
---|
230 | tmpC->objectPointer = object; |
---|
231 | this->deadList = tmpC; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | void FastFactory::kill(BaseObject* object, bool searchForFastFactory) |
---|
236 | { |
---|
237 | if (likely(searchForFastFactory == true)) |
---|
238 | { |
---|
239 | FastFactory* tmpFac = FastFactory::first; |
---|
240 | while (tmpFac != NULL) |
---|
241 | { |
---|
242 | if (object->isA(tmpFac->storedClassID)) |
---|
243 | { |
---|
244 | tmpFac->kill(object); |
---|
245 | return; |
---|
246 | } |
---|
247 | tmpFac = tmpFac->next; |
---|
248 | } |
---|
249 | |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | /** |
---|
261 | * standard constructor |
---|
262 | */ |
---|
263 | ObjectManager::ObjectManager () |
---|
264 | { |
---|
265 | this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); |
---|
266 | this->setName("ObjectManager"); |
---|
267 | |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | /** |
---|
272 | * the singleton reference to this class |
---|
273 | */ |
---|
274 | ObjectManager* ObjectManager::singletonRef = NULL; |
---|
275 | |
---|
276 | /** |
---|
277 | * standard deconstructor |
---|
278 | */ |
---|
279 | ObjectManager::~ObjectManager () |
---|
280 | { |
---|
281 | ObjectManager::singletonRef = NULL; |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * outputs some simple debug information about the ObjectManage |
---|
286 | */ |
---|
287 | void ObjectManager::debug() const |
---|
288 | { |
---|
289 | PRINT(0)("\n==========================| ObjectManager::debug() |===\n"); |
---|
290 | /* PRINT(0)("= Number of registerable classes: %i\n", CL_NUMBER ); |
---|
291 | PRINT(0)("= Currently cached objects: \n"); |
---|
292 | for(int i = 0; i < CL_NUMBER; ++i) |
---|
293 | { |
---|
294 | if( this->managedObjectList[i] != NULL) |
---|
295 | PRINT(0)("= o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize()); |
---|
296 | else |
---|
297 | PRINT(0)("= o Class Nr. %i has cached 0 object(s)\n", i); |
---|
298 | }*/ |
---|
299 | PRINT(0)("=======================================================\n"); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|