[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 | |
---|
[4933] | 39 | this->storedDeadObjects = 0; |
---|
| 40 | |
---|
[4932] | 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 | |
---|
[4933] | 60 | void FastFactory::registerFastFactory(FastFactory* fastFactory) |
---|
[4932] | 61 | { |
---|
[4933] | 62 | PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName()); |
---|
[4932] | 63 | |
---|
| 64 | if( FastFactory::first == NULL) |
---|
[4933] | 65 | FastFactory::first = fastFactory; |
---|
[4932] | 66 | else |
---|
| 67 | { |
---|
| 68 | FastFactory* tmpFac = FastFactory::first; |
---|
| 69 | while( tmpFac->next != NULL) |
---|
| 70 | tmpFac = tmpFac->next; |
---|
[4933] | 71 | tmpFac->setNext(fastFactory); |
---|
[4932] | 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[4933] | 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 | } |
---|
[4932] | 98 | |
---|
[4936] | 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 | } |
---|
[4932] | 111 | |
---|
| 112 | |
---|
[4936] | 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; |
---|
[4932] | 128 | |
---|
[4936] | 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 | } |
---|
[4932] | 138 | |
---|
| 139 | |
---|
| 140 | |
---|
[4936] | 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; |
---|
[4932] | 155 | |
---|
[4936] | 156 | tmpC->next = this->unusedContainers; |
---|
| 157 | this->unusedContainers = tmpC; |
---|
[4932] | 158 | |
---|
[4936] | 159 | return tmpC->objectPointer; |
---|
| 160 | } |
---|
| 161 | } |
---|
[4932] | 162 | |
---|
| 163 | |
---|
[4936] | 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 | |
---|
[4932] | 192 | /** |
---|
[4836] | 193 | * standard constructor |
---|
[4245] | 194 | */ |
---|
[4592] | 195 | ObjectManager::ObjectManager () |
---|
[4245] | 196 | { |
---|
[4320] | 197 | this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); |
---|
[4597] | 198 | this->setName("ObjectManager"); |
---|
[4592] | 199 | |
---|
[4245] | 200 | } |
---|
| 201 | |
---|
[4322] | 202 | |
---|
[4245] | 203 | /** |
---|
[4836] | 204 | * the singleton reference to this class |
---|
[4245] | 205 | */ |
---|
| 206 | ObjectManager* ObjectManager::singletonRef = NULL; |
---|
| 207 | |
---|
| 208 | /** |
---|
[4836] | 209 | * standard deconstructor |
---|
[4245] | 210 | */ |
---|
[4592] | 211 | ObjectManager::~ObjectManager () |
---|
[4245] | 212 | { |
---|
| 213 | ObjectManager::singletonRef = NULL; |
---|
[4285] | 214 | } |
---|
[4245] | 215 | |
---|
[4485] | 216 | /** |
---|
[4836] | 217 | * outputs some simple debug information about the ObjectManage |
---|
[4485] | 218 | */ |
---|
[4746] | 219 | void ObjectManager::debug() const |
---|
[4287] | 220 | { |
---|
| 221 | PRINT(0)("\n==========================| ObjectManager::debug() |===\n"); |
---|
[4932] | 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 | { |
---|
[4318] | 226 | if( this->managedObjectList[i] != NULL) |
---|
[4592] | 227 | PRINT(0)("= o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize()); |
---|
[4287] | 228 | else |
---|
[4592] | 229 | PRINT(0)("= o Class Nr. %i has cached 0 object(s)\n", i); |
---|
[4932] | 230 | }*/ |
---|
[4287] | 231 | PRINT(0)("=======================================================\n"); |
---|
| 232 | } |
---|
[4930] | 233 | |
---|
[4931] | 234 | |
---|