| [4940] | 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 | */ | 
|---|
 | 14 |  | 
|---|
| [5750] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD | 
|---|
| [4940] | 16 |  | 
|---|
 | 17 | #include "fast_factory.h" | 
|---|
| [5041] | 18 |  | 
|---|
| [4940] | 19 | #include "compiler.h" | 
|---|
 | 20 | #include "debug.h" | 
|---|
| [5041] | 21 | #include <string.h> | 
|---|
| [4940] | 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 |  */ | 
|---|
| [7221] | 31 | FastFactory::FastFactory (ClassID classID, const std::string& fastFactoryName) | 
|---|
| [4940] | 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; | 
|---|
| [4980] | 58 |   this->flush(); | 
|---|
| [4940] | 59 |  | 
|---|
 | 60 |   if (this->next) | 
|---|
 | 61 |     delete this->next; | 
|---|
 | 62 | } | 
|---|
 | 63 |  | 
|---|
 | 64 | /** | 
|---|
| [4980] | 65 |  * deletes all FastFactories | 
|---|
 | 66 |  */ | 
|---|
 | 67 | void FastFactory::deleteAll() | 
|---|
 | 68 | { | 
|---|
 | 69 |   if (FastFactory::first) | 
|---|
 | 70 |     delete FastFactory::first; | 
|---|
 | 71 | } | 
|---|
 | 72 |  | 
|---|
 | 73 | /** | 
|---|
| [4940] | 74 |  * registers a Factory to the List of known factories. | 
|---|
 | 75 |  * @param fastFactory The factory to add | 
|---|
 | 76 |  * | 
|---|
 | 77 |  * needed, to step through all the FastFactories. | 
|---|
 | 78 |  */ | 
|---|
 | 79 | void FastFactory::registerFastFactory(FastFactory* fastFactory) | 
|---|
 | 80 | { | 
|---|
 | 81 |   PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName()); | 
|---|
 | 82 |  | 
|---|
 | 83 |   if( FastFactory::first == NULL) | 
|---|
 | 84 |     FastFactory::first = fastFactory; | 
|---|
 | 85 |   else | 
|---|
 | 86 |   { | 
|---|
 | 87 |     FastFactory* tmpFac = FastFactory::first; | 
|---|
 | 88 |     while( tmpFac->next != NULL) | 
|---|
 | 89 |       tmpFac = tmpFac->next; | 
|---|
 | 90 |     tmpFac->setNext(fastFactory); | 
|---|
 | 91 |   } | 
|---|
 | 92 | } | 
|---|
 | 93 |  | 
|---|
 | 94 | /** | 
|---|
 | 95 |  * searches for a FastFactory | 
|---|
 | 96 |  * @param classID the ClassID of the FastFactory to search for | 
|---|
 | 97 |  * @returns true if found, false otherwise. | 
|---|
 | 98 |  */ | 
|---|
| [4950] | 99 | FastFactory* FastFactory::searchFastFactory(ClassID classID) | 
|---|
| [4940] | 100 | { | 
|---|
 | 101 |   if (FastFactory::first == NULL) | 
|---|
 | 102 |     return NULL; | 
|---|
 | 103 |   else | 
|---|
 | 104 |   { | 
|---|
 | 105 |     FastFactory* tmpFac = FastFactory::first; | 
|---|
 | 106 |     while (tmpFac != NULL) | 
|---|
 | 107 |     { | 
|---|
 | 108 |       if (tmpFac->storedClassID == classID) | 
|---|
 | 109 |         return tmpFac; | 
|---|
 | 110 |       tmpFac = tmpFac->next; | 
|---|
 | 111 |     } | 
|---|
 | 112 |   } | 
|---|
 | 113 |   return NULL; | 
|---|
 | 114 | } | 
|---|
 | 115 |  | 
|---|
 | 116 | /** | 
|---|
| [4950] | 117 |  * searches for a FastFactory | 
|---|
 | 118 |  * @param classID the ClassID of the FastFactory to search for | 
|---|
 | 119 |  * @returns true if found, false otherwise. | 
|---|
 | 120 |  */ | 
|---|
| [7221] | 121 | FastFactory* FastFactory::searchFastFactory(const std::string& fastFactoryName) | 
|---|
| [4950] | 122 | { | 
|---|
 | 123 |   if (FastFactory::first == NULL) | 
|---|
 | 124 |     return NULL; | 
|---|
 | 125 |   else | 
|---|
 | 126 |   { | 
|---|
 | 127 |     FastFactory* tmpFac = FastFactory::first; | 
|---|
 | 128 |     while (tmpFac != NULL) | 
|---|
 | 129 |     { | 
|---|
| [7221] | 130 |       if (fastFactoryName == tmpFac->getName()) | 
|---|
| [4950] | 131 |         return tmpFac; | 
|---|
 | 132 |       tmpFac = tmpFac->next; | 
|---|
 | 133 |     } | 
|---|
 | 134 |   } | 
|---|
 | 135 |   return NULL; | 
|---|
 | 136 | } | 
|---|
 | 137 |  | 
|---|
 | 138 | /** | 
|---|
| [4940] | 139 |  * Removes all the stored Containers, and sets the Lists back to emptienes. | 
|---|
 | 140 |  * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !! | 
|---|
 | 141 |  */ | 
|---|
 | 142 | void FastFactory::flushAll(bool hardFLUSH) | 
|---|
 | 143 | { | 
|---|
 | 144 |   FastFactory* tmpFac = FastFactory::first; | 
|---|
 | 145 |   while (tmpFac != NULL) | 
|---|
 | 146 |   { | 
|---|
| [5041] | 147 |     PRINTF(4)("DELETEING ALL OF %s\n",tmpFac->getName()); | 
|---|
| [4940] | 148 |     tmpFac->flush(hardFLUSH); | 
|---|
 | 149 |     tmpFac = tmpFac->next; | 
|---|
 | 150 |   } | 
|---|
 | 151 | } | 
|---|
 | 152 |  | 
|---|
 | 153 | /** | 
|---|
 | 154 |  * ereases all the remaining containers, without deleting the stored Objects inside of them. | 
|---|
 | 155 |  * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !! | 
|---|
 | 156 |  */ | 
|---|
 | 157 | void FastFactory::flush(bool hardFLUSH) | 
|---|
 | 158 | { | 
|---|
 | 159 |   FastObjectMember* tmpMember = this->deadList, *delMember = NULL; | 
|---|
 | 160 |   while (tmpMember != NULL) | 
|---|
 | 161 |   { | 
|---|
 | 162 |     delMember = tmpMember; | 
|---|
 | 163 |     tmpMember = tmpMember->next; | 
|---|
| [4979] | 164 |     if (hardFLUSH) | 
|---|
 | 165 |     { | 
|---|
| [4940] | 166 |       delete delMember->objectPointer; | 
|---|
| [4979] | 167 |     } | 
|---|
| [4940] | 168 |     delete delMember; | 
|---|
 | 169 |   } | 
|---|
 | 170 |   this->deadList = NULL; | 
|---|
 | 171 |  | 
|---|
 | 172 |   tmpMember = this->unusedContainers; | 
|---|
 | 173 |   while (tmpMember != NULL) | 
|---|
 | 174 |   { | 
|---|
 | 175 |     delMember = tmpMember; | 
|---|
 | 176 |     tmpMember = tmpMember->next; | 
|---|
 | 177 |     delete delMember; | 
|---|
 | 178 |   } | 
|---|
 | 179 |   this->unusedContainers = NULL; | 
|---|
 | 180 | } | 
|---|
 | 181 |  | 
|---|
 | 182 | /** | 
|---|
 | 183 |  * generates count new Object of the Corresponding class. (precaching) | 
|---|
 | 184 |  * @param count How many instances of the class should be generated. | 
|---|
 | 185 |  */ | 
|---|
 | 186 | void FastFactory::prepare(unsigned int count) | 
|---|
 | 187 | { | 
|---|
 | 188 | /*  if (this->storedDeadObjects + this->storedLivingObjects >= count) | 
|---|
 | 189 |   { | 
|---|
 | 190 |   PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName()); | 
|---|
 | 191 | }*/ | 
|---|
| [8350] | 192 |   for (unsigned int i = this->storedDeadObjects; i < count; i++) | 
|---|
| [4940] | 193 |   { | 
|---|
 | 194 |     this->fabricate(); | 
|---|
 | 195 |   } | 
|---|
 | 196 | } | 
|---|
 | 197 |  | 
|---|
 | 198 | /** | 
|---|
 | 199 |  * gives back live to one Object. | 
|---|
 | 200 |  * @return the Object to resurrect. | 
|---|
 | 201 |  */ | 
|---|
 | 202 | BaseObject* FastFactory::resurrect() | 
|---|
 | 203 | { | 
|---|
 | 204 |   PRINTF(4)("Resurecting Object of type %s\n", this->getName()); | 
|---|
 | 205 |   if (unlikely(this->deadList == NULL)) | 
|---|
 | 206 |   { | 
|---|
| [5041] | 207 |     PRINTF(3)("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" \ | 
|---|
 | 208 |         "  Developer: try increasing the count with FastFactory::prepare(contHigher than actual)\n" \ | 
|---|
 | 209 |         "    Fabricating a new %s\n", this->getName(), this->getName()); | 
|---|
| [4940] | 210 |     this->fabricate(); | 
|---|
 | 211 |     return this->resurrect(); | 
|---|
 | 212 |   } | 
|---|
 | 213 |   else | 
|---|
 | 214 |   { | 
|---|
| [4941] | 215 |     FastObjectMember* tmpC = this->deadList; | 
|---|
| [4940] | 216 |     this->deadList = this->deadList->next; | 
|---|
 | 217 |  | 
|---|
 | 218 |     tmpC->next = this->unusedContainers; | 
|---|
 | 219 |     this->unusedContainers = tmpC; | 
|---|
 | 220 |  | 
|---|
 | 221 |     return tmpC->objectPointer; | 
|---|
 | 222 |   } | 
|---|
 | 223 | } | 
|---|
 | 224 |  | 
|---|
 | 225 | /** | 
|---|
 | 226 |  * gives back live to one Object. | 
|---|
 | 227 |  * @param classID the class From which to resurrect an Object. | 
|---|
 | 228 |  * @return the Object to resurrect, NULL if classID is not found. | 
|---|
 | 229 |  */ | 
|---|
 | 230 | BaseObject* FastFactory::resurrect(ClassID classID) | 
|---|
 | 231 | { | 
|---|
 | 232 |   FastFactory* tmpFac = FastFactory::getFirst(); | 
|---|
 | 233 |  | 
|---|
 | 234 |   while (tmpFac != NULL) | 
|---|
 | 235 |   { | 
|---|
 | 236 |     if (classID == tmpFac->storedClassID) | 
|---|
 | 237 |       return tmpFac->resurrect(); | 
|---|
 | 238 |     tmpFac = tmpFac->next; | 
|---|
 | 239 |   } | 
|---|
 | 240 |   return NULL; | 
|---|
 | 241 | } | 
|---|
 | 242 |  | 
|---|
 | 243 | /** | 
|---|
 | 244 |  * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection | 
|---|
 | 245 |  * @param object the Object to kill. | 
|---|
 | 246 |  * | 
|---|
 | 247 |  * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode. | 
|---|
 | 248 |  */ | 
|---|
 | 249 | void FastFactory::kill(BaseObject* object) | 
|---|
 | 250 | { | 
|---|
 | 251 |   FastObjectMember* tmpC; | 
|---|
 | 252 |   if (unlikely(this->unusedContainers == NULL)) | 
|---|
 | 253 |   { | 
|---|
 | 254 |     tmpC = new FastObjectMember; | 
|---|
 | 255 |   } | 
|---|
 | 256 |   else | 
|---|
 | 257 |   { | 
|---|
 | 258 |     tmpC = this->unusedContainers; | 
|---|
 | 259 |     this->unusedContainers = this->unusedContainers->next; | 
|---|
 | 260 |   } | 
|---|
 | 261 |  | 
|---|
 | 262 |   tmpC->next = this->deadList; | 
|---|
 | 263 |   tmpC->objectPointer = object; | 
|---|
 | 264 |   this->deadList = tmpC; | 
|---|
 | 265 | } | 
|---|
 | 266 |  | 
|---|
 | 267 |  | 
|---|
 | 268 | void FastFactory::kill(BaseObject* object, bool searchForFastFactory) | 
|---|
 | 269 | { | 
|---|
 | 270 |   if (likely(searchForFastFactory == true)) | 
|---|
 | 271 |   { | 
|---|
 | 272 |     FastFactory* tmpFac = FastFactory::first; | 
|---|
 | 273 |     while (tmpFac != NULL) | 
|---|
 | 274 |     { | 
|---|
 | 275 |       if (object->isA(tmpFac->storedClassID)) | 
|---|
 | 276 |       { | 
|---|
 | 277 |         tmpFac->kill(object); | 
|---|
 | 278 |         return; | 
|---|
 | 279 |       } | 
|---|
 | 280 |       tmpFac = tmpFac->next; | 
|---|
 | 281 |     } | 
|---|
 | 282 |   } | 
|---|
 | 283 | } | 
|---|