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 | |
---|
27 | |
---|
28 | /** |
---|
29 | * constructor, sets everything to zero and define factoryName |
---|
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->storedDeadObjects = 0; |
---|
40 | |
---|
41 | FastFactory::registerFastFactory(this); |
---|
42 | } |
---|
43 | |
---|
44 | /** a reference to the First FastFactory */ |
---|
45 | FastFactory* FastFactory::first = NULL; |
---|
46 | |
---|
47 | /** |
---|
48 | * destructor |
---|
49 | * clear the Q |
---|
50 | */ |
---|
51 | FastFactory::~FastFactory () |
---|
52 | { |
---|
53 | if (this == first) |
---|
54 | this->first = NULL; |
---|
55 | |
---|
56 | if (this->next) |
---|
57 | delete this->next; |
---|
58 | } |
---|
59 | |
---|
60 | void FastFactory::registerFastFactory(FastFactory* fastFactory) |
---|
61 | { |
---|
62 | PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName()); |
---|
63 | |
---|
64 | if( FastFactory::first == NULL) |
---|
65 | FastFactory::first = fastFactory; |
---|
66 | else |
---|
67 | { |
---|
68 | FastFactory* tmpFac = FastFactory::first; |
---|
69 | while( tmpFac->next != NULL) |
---|
70 | tmpFac = tmpFac->next; |
---|
71 | tmpFac->setNext(fastFactory); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | * searches for a FastFactory |
---|
78 | * @param factoryName the Name of the Factory to search for (not used) |
---|
79 | * @param classID the ClassID of the FastFactory to search for |
---|
80 | * @returns true if found, false otherwise. |
---|
81 | */ |
---|
82 | FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName) |
---|
83 | { |
---|
84 | if (FastFactory::first == NULL) |
---|
85 | return NULL; |
---|
86 | else |
---|
87 | { |
---|
88 | FastFactory* tmpFac = FastFactory::first; |
---|
89 | while (tmpFac != NULL) |
---|
90 | { |
---|
91 | if (tmpFac->storedClassID == classID) |
---|
92 | return tmpFac; |
---|
93 | tmpFac = tmpFac->next; |
---|
94 | } |
---|
95 | } |
---|
96 | return NULL; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * |
---|
101 | */ |
---|
102 | void FastFactory::flushAll(bool hardFLUSH) |
---|
103 | { |
---|
104 | FastFactory* tmpFac = FastFactory::first; |
---|
105 | while (tmpFac != NULL) |
---|
106 | { |
---|
107 | tmpFac->flush(hardFLUSH); |
---|
108 | tmpFac = tmpFac->next; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | * ereases all the remaining containers, without deleting the stored Objects inside of them. |
---|
115 | */ |
---|
116 | void FastFactory::flush(bool hardFLUSH) |
---|
117 | { |
---|
118 | FastObjectMember* tmpMember = this->deadList, *delMember = NULL; |
---|
119 | while (tmpMember != NULL) |
---|
120 | { |
---|
121 | delMember = tmpMember; |
---|
122 | tmpMember = tmpMember->next; |
---|
123 | if (unlikely(hardFLUSH == true)) |
---|
124 | delete delMember->objectPointer; |
---|
125 | delete delMember; |
---|
126 | } |
---|
127 | this->deadList = NULL; |
---|
128 | |
---|
129 | tmpMember = this->unusedContainers; |
---|
130 | while (tmpMember != NULL) |
---|
131 | { |
---|
132 | delMember = tmpMember; |
---|
133 | tmpMember = tmpMember->next; |
---|
134 | delete delMember; |
---|
135 | } |
---|
136 | this->unusedContainers = NULL; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | BaseObject* FastFactory::resurect(ClassID classID) |
---|
142 | { |
---|
143 | PRINTF(4)("Resurecting Object of type %s\n", this->getName()); |
---|
144 | if (unlikely(this->deadList == NULL)) |
---|
145 | { |
---|
146 | 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" \ |
---|
147 | "Fabricating a new %s", this->getName(), this->getName()); |
---|
148 | this->fabricate(); |
---|
149 | return this->resurect(classID); |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | FastObjectMember* tmpC = deadList; |
---|
154 | this->deadList = this->deadList->next; |
---|
155 | |
---|
156 | tmpC->next = this->unusedContainers; |
---|
157 | this->unusedContainers = tmpC; |
---|
158 | |
---|
159 | return tmpC->objectPointer; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | void FastFactory::kill(ClassID classID, BaseObject* object) |
---|
166 | { |
---|
167 | FastObjectMember* tmpC; |
---|
168 | if (unlikely(this->unusedContainers == NULL)) |
---|
169 | { |
---|
170 | tmpC = new FastObjectMember; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | tmpC = this->unusedContainers; |
---|
175 | this->unusedContainers = this->unusedContainers->next; |
---|
176 | } |
---|
177 | |
---|
178 | tmpC->next = this->deadList; |
---|
179 | tmpC->objectPointer = object; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | * standard constructor |
---|
194 | */ |
---|
195 | ObjectManager::ObjectManager () |
---|
196 | { |
---|
197 | this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); |
---|
198 | this->setName("ObjectManager"); |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | /** |
---|
204 | * the singleton reference to this class |
---|
205 | */ |
---|
206 | ObjectManager* ObjectManager::singletonRef = NULL; |
---|
207 | |
---|
208 | /** |
---|
209 | * standard deconstructor |
---|
210 | */ |
---|
211 | ObjectManager::~ObjectManager () |
---|
212 | { |
---|
213 | ObjectManager::singletonRef = NULL; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * outputs some simple debug information about the ObjectManage |
---|
218 | */ |
---|
219 | void ObjectManager::debug() const |
---|
220 | { |
---|
221 | PRINT(0)("\n==========================| ObjectManager::debug() |===\n"); |
---|
222 | /* PRINT(0)("= Number of registerable classes: %i\n", CL_NUMBER ); |
---|
223 | PRINT(0)("= Currently cached objects: \n"); |
---|
224 | for(int i = 0; i < CL_NUMBER; ++i) |
---|
225 | { |
---|
226 | if( this->managedObjectList[i] != NULL) |
---|
227 | PRINT(0)("= o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize()); |
---|
228 | else |
---|
229 | PRINT(0)("= o Class Nr. %i has cached 0 object(s)\n", i); |
---|
230 | }*/ |
---|
231 | PRINT(0)("=======================================================\n"); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|