[4940] | 1 | /*! |
---|
| 2 | * @file fast_factory.h |
---|
| 3 | * The ObjectManager (FastFactory) is designed, to automatically generate and remove |
---|
| 4 | * (without to much overhead) many instances of different classes. |
---|
| 5 | * |
---|
| 6 | * It is especially usefull for objects, that come only for a short time into existence, |
---|
| 7 | * and get killed after a small amount of time (like shots). |
---|
| 8 | * |
---|
| 9 | * The Creation of an Object is usually done in the Weapon Class, where one subscribes |
---|
| 10 | * a Projectile with: |
---|
| 11 | * this->bulletFactory = tFastFactory<TestBullet>::getFastFactory(CL_TEST_BULLET, "TestBullet"); |
---|
| 12 | * (this might change over time). |
---|
| 13 | * Then you can at loading time initialize an amount of the class with something like: |
---|
| 14 | * this->bulletFactory->prepare(100); // creates 100 entities of TestBullet (dead ones) |
---|
| 15 | * afterwards one can just retrieve an Object form the Class with |
---|
| 16 | * this->bulletFactory->resurrect(); // this returns a BaseObject an Object of the class. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #ifndef _FAST_FACTORY_H |
---|
| 21 | #define _FAST_FACTORY_H |
---|
| 22 | |
---|
| 23 | #include "base_object.h" |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | // FORWARD DECLARATION // |
---|
| 27 | class GarbageCollector; |
---|
| 28 | template<class T> |
---|
| 29 | class tList; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Creates a FastFactory to a Loadable FastFactory. |
---|
| 33 | */ |
---|
| 34 | //#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \ |
---|
| 35 | //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID) |
---|
| 36 | |
---|
| 37 | //! A struct, that holds Lists of Objects of a certain type. |
---|
| 38 | struct FastObjectMember |
---|
| 39 | { |
---|
| 40 | BaseObject* objectPointer; //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork) |
---|
| 41 | |
---|
| 42 | FastObjectMember* next; //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers) |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | //! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler. |
---|
| 46 | /** |
---|
| 47 | * The ObjectManager (FastFactory) is designed, to automatically generate and remove |
---|
| 48 | * (without to much overhead) many instances of different classes. |
---|
| 49 | * |
---|
| 50 | * FastFactory is needed to glue all the tFastFactories together. |
---|
| 51 | * It is also the general class that implements the necessary functions |
---|
| 52 | * to generate, resurrect kill and stuff... |
---|
| 53 | */ |
---|
| 54 | class FastFactory : public BaseObject |
---|
| 55 | { |
---|
| 56 | |
---|
| 57 | public: |
---|
| 58 | virtual ~FastFactory (); |
---|
| 59 | |
---|
| 60 | // functions to push and pop elements of this class |
---|
| 61 | BaseObject* resurrect(); |
---|
| 62 | static BaseObject* resurrect(ClassID classID); |
---|
| 63 | void kill(BaseObject* object); |
---|
| 64 | static void kill(BaseObject* object, bool searchForFastFactory); |
---|
| 65 | |
---|
| 66 | void prepare(unsigned int count); |
---|
| 67 | // retrival functions for fast Ineraction |
---|
| 68 | //FastFactory* getFastFactory(ClassID classID); |
---|
| 69 | |
---|
| 70 | static void flushAll(bool hardFLUSH = false); |
---|
| 71 | void flush(bool hardFLUSH = false); |
---|
| 72 | |
---|
| 73 | /** @returns the first FastFactory */ |
---|
| 74 | static FastFactory* getFirst() |
---|
| 75 | { |
---|
| 76 | return FastFactory::first; |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | protected: |
---|
| 80 | FastFactory (ClassID classID, const char* fastFactoryName = NULL); |
---|
| 81 | |
---|
| 82 | /** sets the Next factory in the list @param nextFactory the next factory */ |
---|
| 83 | inline void setNext( FastFactory* nextFastFactory) |
---|
| 84 | { |
---|
| 85 | this->next = nextFastFactory; |
---|
| 86 | }; |
---|
| 87 | /** @returns the next FastFactory */ |
---|
| 88 | FastFactory* getNext() const |
---|
| 89 | { |
---|
| 90 | return this->next; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | /** generates a new Object of the Class T */ |
---|
| 94 | virtual void fabricate() = NULL; |
---|
| 95 | static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL); |
---|
| 96 | |
---|
| 97 | private: |
---|
| 98 | static void registerFastFactory(FastFactory* fastFactory); |
---|
| 99 | |
---|
| 100 | protected: |
---|
| 101 | ClassID storedClassID; //!< The classID of the specified class. |
---|
| 102 | unsigned int storedDeadObjects; //!< How many dead objects are stored in this class |
---|
| 103 | |
---|
| 104 | FastObjectMember* deadList; //!< A List of all stored dead Objects of this class. |
---|
| 105 | FastObjectMember* unusedContainers; //!< This is a List of unused containers, that will be reused by kill. |
---|
| 106 | |
---|
| 107 | private: |
---|
| 108 | static FastFactory* first; //!< A pointer to the first FastFactory. |
---|
| 109 | |
---|
| 110 | FastFactory* next; //!< pointer to the next FastFactory. |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * a FastFactory that is able to load any kind of Object from a ClassID |
---|
| 115 | * (this is a Functor) |
---|
| 116 | */ |
---|
| 117 | template<class T> |
---|
| 118 | class tFastFactory : public FastFactory |
---|
| 119 | { |
---|
| 120 | public: |
---|
| 121 | static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL); |
---|
| 122 | |
---|
| 123 | private: |
---|
| 124 | tFastFactory(ClassID classID, const char* fastFactoryName); |
---|
| 125 | |
---|
| 126 | virtual void fabricate(); |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * construnts a FastFactory with |
---|
| 131 | * @param fastFactoryName the name of the FastFactory |
---|
| 132 | * @param fastFactory the ID of the class |
---|
| 133 | * @todo (can this be written in another form??) |
---|
| 134 | */ |
---|
| 135 | template<class T> |
---|
| 136 | tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName) |
---|
| 137 | : FastFactory(classID, fastFactoryName) |
---|
| 138 | {} |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * creates (if not existent) a Factory of Class T, and assigns some values to it |
---|
| 142 | * @param classID the ClassID to assign to this class |
---|
| 143 | * @param fastFactoryName the name to assign |
---|
| 144 | * @returns The FastFactory if existent a new Factory if not. |
---|
| 145 | */ |
---|
| 146 | template<class T> |
---|
| 147 | tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName) |
---|
| 148 | { |
---|
| 149 | tFastFactory<T>* tmpFac = NULL; |
---|
| 150 | if (FastFactory::getFirst() != NULL) |
---|
| 151 | tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName)); |
---|
| 152 | |
---|
| 153 | if (tmpFac != NULL) |
---|
| 154 | return tmpFac; |
---|
| 155 | else |
---|
| 156 | return new tFastFactory<T>(classID, fastFactoryName); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * fabricates an Object of Class T, that corresponds to classID. |
---|
| 162 | */ |
---|
| 163 | template<class T> |
---|
| 164 | void tFastFactory<T>::fabricate() |
---|
| 165 | { |
---|
| 166 | FastObjectMember* tmpFirstDead = new FastObjectMember; |
---|
| 167 | tmpFirstDead->objectPointer = new T(); |
---|
| 168 | tmpFirstDead->next = this->deadList; |
---|
| 169 | ++this->storedDeadObjects; |
---|
| 170 | |
---|
| 171 | this->deadList = tmpFirstDead; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | #endif /* _FAST_FACTORY_H */ |
---|