/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_OBJECT_MANAGER #include "object_manager.h" #include "garbage_collector.h" #include "list.h" #include "debug.h" using namespace std; /** * standard constructor */ ObjectManager::ObjectManager () { this->setClassID(CL_OBJECT_MANAGER, "ObjectManager"); this->setName("ObjectManager"); this->managedObjectList = new tList*[CL_NUMBER]; for(int i = 0; i < CL_NUMBER; ++i) { this->managedObjectList[i] = NULL; } } /** * the singleton reference to this class */ ObjectManager* ObjectManager::singletonRef = NULL; /** * standard deconstructor */ ObjectManager::~ObjectManager () { ObjectManager::singletonRef = NULL; } /** * adds an element to the list of dead objects * @param index: The type of object to add * @param object: pointer to the object at hand */ void ObjectManager::addToDeadList(int index, BaseObject* object) { if( likely(this->managedObjectList[index] != NULL)) this->managedObjectList[index]->add(object); else PRINTF(0)(" Critical: unable to add object to the list nr. %i: no list initialized - ignoring\n", index); } /** * resurects an object * @param index: the type of resource to load * @param number: how many of them @todo if it is unable to get an object from the deadList, it should create it */ BaseObject* ObjectManager::getFromDeadList(int index, int number) { if( likely(this->managedObjectList[index] != NULL)) { BaseObject* obj = this->managedObjectList[index]->firstElement(); this->managedObjectList[index]->remove(obj); if( unlikely(obj == NULL)) { PRINTF(0)("Critical: there was no object anymore in the dead list! This could result in Segfaults\n"); } return obj; } else PRINTF(0)(" Critical: unable to get object from the list nr. %i: no elements initialized - ignoring\n", index); return NULL; } /** * outputs some simple debug information about the ObjectManage */ void ObjectManager::debug() const { PRINT(0)("\n==========================| ObjectManager::debug() |===\n"); PRINT(0)("= Number of registerable classes: %i\n", CL_NUMBER ); PRINT(0)("= Currently cached objects: \n"); for(int i = 0; i < CL_NUMBER; ++i) { if( this->managedObjectList[i] != NULL) PRINT(0)("= o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize()); else PRINT(0)("= o Class Nr. %i has cached 0 object(s)\n", i); } PRINT(0)("=======================================================\n"); } /** * constructor set everything to zero and define factoryName */ FastObject::FastObject (const char* fastObjectName, ClassID classID) { this->setClassID(CL_FACTORY, "FastObject"); this->setName(fastObjectName); this->storedClassID = classID; this->next = NULL; FastObject::registerFastObject(this); } /** a reference to the First FastObject */ FastObject* FastObject::first = NULL; /** * destructor clear the Q */ FastObject::~FastObject () { // printf("%s\n", this->factoryName); // FastObject* tmpDel = this->next; // this->next = NULL; if (this->next) delete this->next; } /** * add a FastObject to the FastObject Queue * @param factory a FastObject to be registered */ void FastObject::registerFastObject( FastObject* factory) { PRINTF(4)("Registered FastObject for '%s'\n", factory->getName()); if( FastObject::first == NULL) FastObject::first = factory; else { FastObject* tmpFac = FastObject::first; while( tmpFac->next != NULL) { tmpFac = tmpFac->next; } tmpFac->setNext(factory); } }