[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. |
---|
[4979] | 17 | * |
---|
| 18 | * The big difference to the FastFactory-class is, that this one is used more for the purpose |
---|
| 19 | * of fast game-interaction than for loading. althought one can also load FastFactorized classes |
---|
| 20 | * it is not the main topic. |
---|
[4940] | 21 | */ |
---|
| 22 | |
---|
| 23 | #ifndef _FAST_FACTORY_H |
---|
| 24 | #define _FAST_FACTORY_H |
---|
| 25 | |
---|
| 26 | #include "base_object.h" |
---|
| 27 | /** |
---|
[4941] | 28 | * Creates a FastFactory to a Createable FastFactory. |
---|
[4940] | 29 | */ |
---|
[4941] | 30 | #define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \ |
---|
[5447] | 31 | FastFactory* global_##CLASS_NAME##_FastFactory = tFastFactory<CLASS_NAME>::getFastFactory(CLASS_ID, #CLASS_NAME) |
---|
| 32 | /** |
---|
| 33 | * Creates a FastFactory for a Class' static function named ClassName::fastFactory. |
---|
| 34 | * @param CLASS_NAME the name of the Class to create the fast-factory for. |
---|
[9716] | 35 | * @param CLASS_ID the ID of the class to create the fast-factory for @see "class_id_DEPRECATED.h" |
---|
[5447] | 36 | * |
---|
| 37 | * notice, that the Class to be called, must implement: |
---|
| 38 | * static FastFactory* fastFactory; |
---|
| 39 | */ |
---|
[9707] | 40 | #define CREATE_FAST_FACTORY_STATIC(CLASS_NAME) \ |
---|
[9757] | 41 | FastFactory* CLASS_NAME::fastFactory = tFastFactory<CLASS_NAME>::getFastFactory(CLASS_NAME::staticClassID(), #CLASS_NAME) |
---|
[4940] | 42 | |
---|
| 43 | //! A struct, that holds Lists of Objects of a certain type. |
---|
[4941] | 44 | typedef struct FastObjectMember |
---|
[9703] | 45 | { |
---|
| 46 | BaseObject* objectPointer; //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork) |
---|
[4940] | 47 | |
---|
[9703] | 48 | FastObjectMember* next; //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers) |
---|
| 49 | }; |
---|
[4940] | 50 | |
---|
| 51 | //! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler. |
---|
| 52 | /** |
---|
| 53 | * The ObjectManager (FastFactory) is designed, to automatically generate and remove |
---|
| 54 | * (without to much overhead) many instances of different classes. |
---|
| 55 | * |
---|
| 56 | * FastFactory is needed to glue all the tFastFactories together. |
---|
| 57 | * It is also the general class that implements the necessary functions |
---|
| 58 | * to generate, resurrect kill and stuff... |
---|
| 59 | */ |
---|
| 60 | class FastFactory : public BaseObject |
---|
[9703] | 61 | { |
---|
[9715] | 62 | ObjectListDeclaration(FastFactory); |
---|
[4940] | 63 | |
---|
[9703] | 64 | public: |
---|
| 65 | virtual ~FastFactory (); |
---|
| 66 | static void deleteAll(); |
---|
[4940] | 67 | |
---|
[9703] | 68 | // functions to push and pop elements of this class |
---|
| 69 | BaseObject* resurrect(); |
---|
[9715] | 70 | static BaseObject* resurrect(const ClassID& classID); |
---|
[9703] | 71 | void kill(BaseObject* object); |
---|
| 72 | static void kill(BaseObject* object, bool searchForFastFactory); |
---|
[4940] | 73 | |
---|
[9703] | 74 | void prepare(unsigned int count); |
---|
[4940] | 75 | |
---|
[9703] | 76 | static void flushAll(bool hardFLUSH = false); |
---|
| 77 | void flush(bool hardFLUSH = false); |
---|
[4940] | 78 | |
---|
[9703] | 79 | /** @returns the first FastFactory */ |
---|
| 80 | inline static FastFactory* getFirst() { return FastFactory::first; }; |
---|
[4940] | 81 | |
---|
[9715] | 82 | static FastFactory* searchFastFactory(const ClassID& classID); |
---|
[9703] | 83 | static FastFactory* searchFastFactory(const std::string& fastFactoryName); |
---|
[4947] | 84 | |
---|
[9715] | 85 | const ClassID& getStoredID() const { return this->storedClassID; }; |
---|
[4950] | 86 | |
---|
[9703] | 87 | protected: |
---|
[9715] | 88 | FastFactory (const ClassID& classID, const std::string& fastFactoryName = ""); |
---|
[4940] | 89 | |
---|
[9703] | 90 | /** sets the Next factory in the list @param nextFactory the next factory */ |
---|
| 91 | inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; }; |
---|
| 92 | /** @returns the next FastFactory */ |
---|
| 93 | FastFactory* getNext() const { return this->next; }; |
---|
[4940] | 94 | |
---|
[9703] | 95 | /** generates a new Object of the Class T */ |
---|
| 96 | virtual void fabricate() = 0; |
---|
[4940] | 97 | |
---|
[9703] | 98 | private: |
---|
| 99 | static void registerFastFactory(FastFactory* fastFactory); |
---|
[4940] | 100 | |
---|
[9703] | 101 | protected: |
---|
[9715] | 102 | ClassID storedClassID; //!< The classID of the specified class. |
---|
[9703] | 103 | unsigned int storedDeadObjects; //!< How many dead objects are stored in this class |
---|
[4940] | 104 | |
---|
[9703] | 105 | FastObjectMember* deadList; //!< A List of all stored dead Objects of this class. |
---|
| 106 | FastObjectMember* unusedContainers; //!< This is a List of unused containers, that will be reused by kill. |
---|
[4940] | 107 | |
---|
[9703] | 108 | private: |
---|
| 109 | static FastFactory* first; //!< A pointer to the first FastFactory. |
---|
[4940] | 110 | |
---|
[9703] | 111 | FastFactory* next; //!< pointer to the next FastFactory. |
---|
| 112 | }; |
---|
[4940] | 113 | |
---|
[4941] | 114 | |
---|
| 115 | |
---|
[4940] | 116 | /** |
---|
| 117 | * a FastFactory that is able to load any kind of Object from a ClassID |
---|
| 118 | * (this is a Functor) |
---|
| 119 | */ |
---|
| 120 | template<class T> |
---|
| 121 | class tFastFactory : public FastFactory |
---|
[9703] | 122 | { |
---|
| 123 | public: |
---|
[9715] | 124 | static tFastFactory<T>* getFastFactory(const ClassID& classID, const std::string& fastFactoryName); |
---|
[4940] | 125 | |
---|
[9703] | 126 | private: |
---|
[9715] | 127 | tFastFactory(const ClassID& classID, const std::string& fastFactoryName); |
---|
[4940] | 128 | |
---|
[9703] | 129 | virtual void fabricate(); |
---|
| 130 | }; |
---|
[4940] | 131 | |
---|
| 132 | /** |
---|
| 133 | * construnts a FastFactory with |
---|
| 134 | * @param fastFactoryName the name of the FastFactory |
---|
| 135 | * @param fastFactory the ID of the class |
---|
| 136 | * @todo (can this be written in another form??) |
---|
| 137 | */ |
---|
| 138 | template<class T> |
---|
[9715] | 139 | tFastFactory<T>::tFastFactory(const ClassID& classID, const std::string& fastFactoryName) |
---|
[4940] | 140 | : FastFactory(classID, fastFactoryName) |
---|
| 141 | {} |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * creates (if not existent) a Factory of Class T, and assigns some values to it |
---|
| 145 | * @param classID the ClassID to assign to this class |
---|
| 146 | * @param fastFactoryName the name to assign |
---|
| 147 | * @returns The FastFactory if existent a new Factory if not. |
---|
| 148 | */ |
---|
| 149 | template<class T> |
---|
[9715] | 150 | tFastFactory<T>* tFastFactory<T>::getFastFactory(const ClassID& classID, const std::string& fastFactoryName) |
---|
[4940] | 151 | { |
---|
| 152 | tFastFactory<T>* tmpFac = NULL; |
---|
| 153 | if (FastFactory::getFirst() != NULL) |
---|
[4950] | 154 | tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID)); |
---|
[4940] | 155 | |
---|
| 156 | if (tmpFac != NULL) |
---|
| 157 | return tmpFac; |
---|
| 158 | else |
---|
| 159 | return new tFastFactory<T>(classID, fastFactoryName); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * fabricates an Object of Class T, that corresponds to classID. |
---|
| 164 | */ |
---|
| 165 | template<class T> |
---|
| 166 | void tFastFactory<T>::fabricate() |
---|
| 167 | { |
---|
| 168 | FastObjectMember* tmpFirstDead = new FastObjectMember; |
---|
| 169 | tmpFirstDead->objectPointer = new T(); |
---|
| 170 | tmpFirstDead->next = this->deadList; |
---|
| 171 | ++this->storedDeadObjects; |
---|
| 172 | |
---|
| 173 | this->deadList = tmpFirstDead; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | #endif /* _FAST_FACTORY_H */ |
---|