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