| [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 | |
|---|
| 26 | /** |
|---|
| [4836] | 27 | * standard constructor |
|---|
| [4245] | 28 | */ |
|---|
| [4592] | 29 | ObjectManager::ObjectManager () |
|---|
| [4245] | 30 | { |
|---|
| [4320] | 31 | this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); |
|---|
| [4597] | 32 | this->setName("ObjectManager"); |
|---|
| [4592] | 33 | |
|---|
| [4286] | 34 | this->managedObjectList = new tList<BaseObject>*[CL_NUMBER]; |
|---|
| [4318] | 35 | for(int i = 0; i < CL_NUMBER; ++i) |
|---|
| 36 | { |
|---|
| 37 | this->managedObjectList[i] = NULL; |
|---|
| 38 | } |
|---|
| [4245] | 39 | } |
|---|
| 40 | |
|---|
| [4322] | 41 | |
|---|
| [4245] | 42 | /** |
|---|
| [4836] | 43 | * the singleton reference to this class |
|---|
| [4245] | 44 | */ |
|---|
| 45 | ObjectManager* ObjectManager::singletonRef = NULL; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| [4836] | 48 | * standard deconstructor |
|---|
| [4245] | 49 | */ |
|---|
| [4592] | 50 | ObjectManager::~ObjectManager () |
|---|
| [4245] | 51 | { |
|---|
| 52 | ObjectManager::singletonRef = NULL; |
|---|
| [4285] | 53 | } |
|---|
| [4245] | 54 | |
|---|
| [4485] | 55 | /** |
|---|
| [4836] | 56 | * adds an element to the list of dead objects |
|---|
| 57 | * @param index: The type of object to add |
|---|
| 58 | * @param object: pointer to the object at hand |
|---|
| [4485] | 59 | */ |
|---|
| [4322] | 60 | void ObjectManager::addToDeadList(int index, BaseObject* object) |
|---|
| [4288] | 61 | { |
|---|
| 62 | if( likely(this->managedObjectList[index] != NULL)) |
|---|
| 63 | this->managedObjectList[index]->add(object); |
|---|
| 64 | else |
|---|
| [4324] | 65 | PRINTF(0)(" Critical: unable to add object to the list nr. %i: no list initialized - ignoring\n", index); |
|---|
| [4288] | 66 | } |
|---|
| [4285] | 67 | |
|---|
| [4485] | 68 | /** |
|---|
| [4836] | 69 | * resurects an object |
|---|
| 70 | * @param index: the type of resource to load |
|---|
| 71 | * @param number: how many of them |
|---|
| [4285] | 72 | |
|---|
| [4836] | 73 | @todo if it is unable to get an object from the deadList, it should create it |
|---|
| [4485] | 74 | */ |
|---|
| [4322] | 75 | BaseObject* ObjectManager::getFromDeadList(int index, int number) |
|---|
| [4289] | 76 | { |
|---|
| 77 | if( likely(this->managedObjectList[index] != NULL)) |
|---|
| 78 | { |
|---|
| 79 | BaseObject* obj = this->managedObjectList[index]->firstElement(); |
|---|
| 80 | this->managedObjectList[index]->remove(obj); |
|---|
| [4324] | 81 | if( unlikely(obj == NULL)) |
|---|
| [4592] | 82 | { |
|---|
| 83 | PRINTF(0)("Critical: there was no object anymore in the dead list! This could result in Segfaults\n"); |
|---|
| 84 | } |
|---|
| [4289] | 85 | return obj; |
|---|
| 86 | } |
|---|
| 87 | else |
|---|
| [4324] | 88 | PRINTF(0)(" Critical: unable to get object from the list nr. %i: no elements initialized - ignoring\n", index); |
|---|
| 89 | return NULL; |
|---|
| [4289] | 90 | } |
|---|
| [4287] | 91 | |
|---|
| [4485] | 92 | /** |
|---|
| [4836] | 93 | * outputs some simple debug information about the ObjectManage |
|---|
| [4485] | 94 | */ |
|---|
| [4746] | 95 | void ObjectManager::debug() const |
|---|
| [4287] | 96 | { |
|---|
| 97 | PRINT(0)("\n==========================| ObjectManager::debug() |===\n"); |
|---|
| [4592] | 98 | PRINT(0)("= Number of registerable classes: %i\n", CL_NUMBER ); |
|---|
| [4287] | 99 | PRINT(0)("= Currently cached objects: \n"); |
|---|
| 100 | for(int i = 0; i < CL_NUMBER; ++i) |
|---|
| 101 | { |
|---|
| [4318] | 102 | if( this->managedObjectList[i] != NULL) |
|---|
| [4592] | 103 | PRINT(0)("= o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize()); |
|---|
| [4287] | 104 | else |
|---|
| [4592] | 105 | PRINT(0)("= o Class Nr. %i has cached 0 object(s)\n", i); |
|---|
| [4287] | 106 | } |
|---|
| 107 | PRINT(0)("=======================================================\n"); |
|---|
| 108 | } |
|---|
| [4930] | 109 | |
|---|
| [4931] | 110 | |
|---|
| 111 | |
|---|
| [4930] | 112 | /** |
|---|
| 113 | * constructor |
|---|
| 114 | |
|---|
| 115 | set everything to zero and define factoryName |
|---|
| 116 | */ |
|---|
| 117 | FastObject::FastObject (const char* fastObjectName, ClassID classID) |
|---|
| 118 | { |
|---|
| 119 | this->setClassID(CL_FACTORY, "FastObject"); |
|---|
| 120 | this->setName(fastObjectName); |
|---|
| 121 | |
|---|
| 122 | this->storedClassID = classID; |
|---|
| 123 | this->next = NULL; |
|---|
| 124 | |
|---|
| 125 | FastObject::registerFastObject(this); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** a reference to the First FastObject */ |
|---|
| 129 | FastObject* FastObject::first = NULL; |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * destructor |
|---|
| 133 | |
|---|
| 134 | clear the Q |
|---|
| 135 | */ |
|---|
| 136 | FastObject::~FastObject () |
|---|
| 137 | { |
|---|
| 138 | // printf("%s\n", this->factoryName); |
|---|
| 139 | // FastObject* tmpDel = this->next; |
|---|
| 140 | // this->next = NULL; |
|---|
| 141 | if (this->next) |
|---|
| 142 | delete this->next; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | * add a FastObject to the FastObject Queue |
|---|
| 147 | * @param factory a FastObject to be registered |
|---|
| 148 | */ |
|---|
| 149 | void FastObject::registerFastObject( FastObject* factory) |
|---|
| 150 | { |
|---|
| 151 | PRINTF(4)("Registered FastObject for '%s'\n", factory->getName()); |
|---|
| 152 | |
|---|
| 153 | if( FastObject::first == NULL) |
|---|
| 154 | FastObject::first = factory; |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | FastObject* tmpFac = FastObject::first; |
|---|
| 158 | while( tmpFac->next != NULL) |
|---|
| 159 | { |
|---|
| 160 | tmpFac = tmpFac->next; |
|---|
| 161 | } |
|---|
| 162 | tmpFac->setNext(factory); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|