[4591] | 1 | /*! |
---|
[4245] | 2 | \file object_manager.h |
---|
[4836] | 3 | * this manager will ceep track of the objects in the world |
---|
[4591] | 4 | |
---|
[4245] | 5 | This is specially designed to: |
---|
| 6 | - Give an interface to the world data |
---|
| 7 | - separate the world data from the world build,update,draw process |
---|
| 8 | - recycle deleted objects: specific for Projectils since there is a lot of world entity creation/deletion (and this needs a lot of time) |
---|
| 9 | - control the garbage collector |
---|
[4313] | 10 | |
---|
| 11 | TO ADD SUPPORT FOR A CLASS do the following steps: |
---|
[4930] | 12 | 1. include the hader file : #include "class_id.h" |
---|
[4744] | 13 | 2. add the class to the type enum classID {}; in class_id.h |
---|
[4313] | 14 | 3. define a function void mCache( ClassName ) in class ObjectManager |
---|
| 15 | |
---|
[4245] | 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | #ifndef _OBJECT_MANAGER_H |
---|
| 20 | #define _OBJECT_MANAGER_H |
---|
| 21 | |
---|
| 22 | #include "base_object.h" |
---|
[4931] | 23 | #include "class_id.h" |
---|
[4245] | 24 | |
---|
[4930] | 25 | template<class T> class tList; |
---|
[4288] | 26 | class GarbageCollector; |
---|
[4285] | 27 | |
---|
[4245] | 28 | |
---|
[4932] | 29 | /** |
---|
| 30 | * Creates a FastFactory to a Loadable FastFactory. |
---|
| 31 | */ |
---|
[4933] | 32 | //#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \ |
---|
| 33 | //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) |
---|
[4591] | 34 | |
---|
[4932] | 35 | //! A struct, that holds Lists of Objects of a certain type. |
---|
[4935] | 36 | struct FastObjectMember |
---|
[4932] | 37 | { |
---|
[4935] | 38 | BaseObject* objectPointer; |
---|
[4699] | 39 | |
---|
[4935] | 40 | FastObjectMember* next; |
---|
[4932] | 41 | }; |
---|
[4245] | 42 | |
---|
[4932] | 43 | //! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler. |
---|
[4933] | 44 | /** |
---|
| 45 | * FastFactory is needed to glue all the tFastFactory<T>'s together. |
---|
| 46 | * Furthermore one can retrieve the corresponding tFastFactory over |
---|
| 47 | * tFastFactory<T>* = FastFacroty::getFastFactory(ID); |
---|
| 48 | */ |
---|
[4932] | 49 | class FastFactory : public BaseObject { |
---|
[4930] | 50 | |
---|
[4932] | 51 | public: |
---|
| 52 | virtual ~FastFactory (); |
---|
[4930] | 53 | |
---|
[4933] | 54 | // functions to push and pop elements of this class |
---|
[4932] | 55 | BaseObject* resurect(ClassID classID); |
---|
| 56 | void kill(ClassID classID, BaseObject* object); |
---|
[4311] | 57 | |
---|
[4936] | 58 | virtual void fabricate() = NULL; |
---|
[4937] | 59 | void prepare(unsigned int count); |
---|
[4933] | 60 | // retrival functions for fast Ineraction |
---|
| 61 | //FastFactory* getFastFactory(ClassID classID); |
---|
[4245] | 62 | |
---|
[4936] | 63 | static void flushAll(bool hardFLUSH = false); |
---|
| 64 | void flush(bool hardFLUSH = false); |
---|
| 65 | |
---|
[4932] | 66 | /** @returns the first FastFactory */ |
---|
| 67 | static FastFactory* getFirst() { return FastFactory::first; }; |
---|
[4245] | 68 | |
---|
[4932] | 69 | protected: |
---|
[4934] | 70 | FastFactory (ClassID classID, const char* fastFactoryName = NULL); |
---|
| 71 | |
---|
[4930] | 72 | /** sets the Next factory in the list @param nextFactory the next factory */ |
---|
[4932] | 73 | inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; }; |
---|
| 74 | /** @returns the next FastFactory */ |
---|
| 75 | FastFactory* getNext() const { return this->next; }; |
---|
[4930] | 76 | |
---|
[4934] | 77 | // virtual BaseObject* fabricate(ClassID classID) = NULL; |
---|
[4933] | 78 | |
---|
[4934] | 79 | static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL); |
---|
| 80 | |
---|
[4933] | 81 | private: |
---|
| 82 | static void registerFastFactory(FastFactory* fastFactory); |
---|
| 83 | |
---|
[4931] | 84 | protected: |
---|
[4932] | 85 | ClassID storedClassID; //!< The classID of the specified class. |
---|
[4933] | 86 | unsigned int storedDeadObjects; //!< How many dead objects are stored in this class |
---|
[4931] | 87 | |
---|
[4935] | 88 | FastObjectMember* deadList; //!< A List of all stored dead Objects of this class. |
---|
| 89 | FastObjectMember* unusedContainers; //!< This is a List of unused containers, that will be reused by kill. |
---|
| 90 | |
---|
[4930] | 91 | private: |
---|
[4932] | 92 | static FastFactory* first; //!< A pointer to the first FastFactory. |
---|
| 93 | |
---|
| 94 | FastFactory* next; //!< pointer to the next FastFactory. |
---|
[4930] | 95 | }; |
---|
| 96 | |
---|
| 97 | /** |
---|
[4932] | 98 | * a FastFactory that is able to load any kind of Object from a ClassID |
---|
[4931] | 99 | * (this is a Functor) |
---|
[4930] | 100 | */ |
---|
[4932] | 101 | template<class T> class tFastFactory : public FastFactory |
---|
[4930] | 102 | { |
---|
| 103 | public: |
---|
[4934] | 104 | static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL); |
---|
[4930] | 105 | |
---|
[4933] | 106 | T* resurect(); |
---|
[4930] | 107 | private: |
---|
[4933] | 108 | tFastFactory(ClassID classID, const char* fastFactoryName); |
---|
| 109 | |
---|
[4936] | 110 | virtual void fabricate(); |
---|
[4933] | 111 | |
---|
| 112 | private: |
---|
[4930] | 113 | }; |
---|
| 114 | |
---|
| 115 | /** |
---|
[4932] | 116 | * construnts a FastFactory with |
---|
| 117 | * @param fastFactoryName the name of the FastFactory |
---|
| 118 | * @param fastFactory the ID of the class |
---|
[4930] | 119 | */ |
---|
| 120 | template<class T> |
---|
[4933] | 121 | tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName) |
---|
| 122 | : FastFactory(classID, fastFactoryName) |
---|
[4930] | 123 | { |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | template<class T> |
---|
[4934] | 127 | tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName) |
---|
[4930] | 128 | { |
---|
[4933] | 129 | tFastFactory<T>* tmpFac = NULL; |
---|
| 130 | if (FastFactory::getFirst() != NULL) |
---|
[4934] | 131 | tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName)); |
---|
[4933] | 132 | |
---|
| 133 | if (tmpFac != NULL) |
---|
| 134 | return tmpFac; |
---|
[4932] | 135 | else |
---|
[4934] | 136 | return new tFastFactory<T>(classID, fastFactoryName); |
---|
[4930] | 137 | } |
---|
| 138 | |
---|
[4932] | 139 | |
---|
[4933] | 140 | template<class T> |
---|
[4936] | 141 | void tFastFactory<T>::fabricate() |
---|
[4933] | 142 | { |
---|
[4935] | 143 | FastObjectMember* tmpFirstDead = new FastObjectMember; |
---|
[4936] | 144 | tmpFirstDead->objectPointer = new T(); |
---|
[4933] | 145 | tmpFirstDead->next = this->deadList; |
---|
| 146 | ++this->storedDeadObjects; |
---|
[4932] | 147 | |
---|
[4933] | 148 | this->deadList = tmpFirstDead; |
---|
| 149 | } |
---|
[4932] | 150 | |
---|
[4933] | 151 | template<class T> |
---|
| 152 | T* tFastFactory<T>::resurect() |
---|
| 153 | { |
---|
[4934] | 154 | PRINTF(4)("Resurecting Object of type %s\n", this->getName()); |
---|
[4933] | 155 | if (unlikely(this->deadList == NULL)) |
---|
| 156 | { |
---|
| 157 | 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" \ |
---|
| 158 | "Fabricating a new %s", this->getName(), this->getName()); |
---|
[4936] | 159 | this->fabricate(); |
---|
| 160 | return resurect(); |
---|
[4933] | 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
[4935] | 164 | FastObjectMember* tmpC = deadList; |
---|
[4933] | 165 | this->deadList = this->deadList->next; |
---|
| 166 | |
---|
| 167 | tmpC->next = this->unusedContainers; |
---|
[4934] | 168 | this->unusedContainers = tmpC; |
---|
[4933] | 169 | |
---|
[4935] | 170 | return dynamic_cast<T*>(tmpC->objectPointer); |
---|
[4933] | 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[4932] | 174 | //////////////////// |
---|
| 175 | // OBJECT MANAGER // |
---|
| 176 | //////////////////// |
---|
| 177 | |
---|
| 178 | //! the object manager itself |
---|
| 179 | class ObjectManager : public BaseObject { |
---|
| 180 | |
---|
| 181 | public: |
---|
| 182 | virtual ~ObjectManager(); |
---|
| 183 | /** @returns a Pointer to the only object of this Class */ |
---|
| 184 | inline static ObjectManager* getInstance() { if (!singletonRef) singletonRef = new ObjectManager(); return singletonRef; }; |
---|
| 185 | |
---|
| 186 | void registerClass(ClassID classID); |
---|
| 187 | |
---|
| 188 | BaseObject* resurect(); |
---|
| 189 | void kill(BaseObject* object); |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | void debug() const; |
---|
| 193 | |
---|
| 194 | private: |
---|
| 195 | ObjectManager(); |
---|
| 196 | |
---|
| 197 | private: |
---|
| 198 | static ObjectManager* singletonRef; //!< The singleton reference to the only reference of this class |
---|
| 199 | |
---|
| 200 | }; |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | |
---|
[4245] | 204 | #endif /* _OBJECT_MANAGER_H */ |
---|