Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/fast_factory.h @ 6883

Last change on this file since 6883 was 5447, checked in by bensch, 19 years ago

orxonox/trunk: Explosions on Collision (—HACK—)
also made the FastFactory faster, not turning through the GarbageCollector, as it is not necessary. FastFactory also implements a Static Memeber subscriptor-Macro now
last but not least: new Functions in The ParticleEngine, and some revisited

File size: 6.3 KB
RevLine 
[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.
35 * @param CLASS_ID the ID of the class to create the fast-factory for @see "class_id.h"
36 *
37 * notice, that the Class to be called, must implement:
38 * static FastFactory*         fastFactory;
39 */
40#define CREATE_FAST_FACTORY_STATIC(CLASS_NAME, CLASS_ID) \
41  FastFactory* CLASS_NAME::fastFactory = tFastFactory<CLASS_NAME>::getFastFactory(CLASS_ID, #CLASS_NAME)
[4940]42
43//! A struct, that holds Lists of Objects of a certain type.
[4941]44typedef struct FastObjectMember
[4940]45  {
46    BaseObject*          objectPointer;      //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork)
47
48    FastObjectMember*    next;               //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers)
49  };
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 */
60class FastFactory : public BaseObject
61  {
62
63  public:
64    virtual ~FastFactory ();
[4980]65    static void deleteAll();
[4940]66
67    // functions to push and pop elements of this class
68    BaseObject* resurrect();
69    static BaseObject* resurrect(ClassID classID);
70    void kill(BaseObject* object);
71    static void kill(BaseObject* object, bool searchForFastFactory);
72
73    void prepare(unsigned int count);
74
75    static void flushAll(bool hardFLUSH = false);
76    void flush(bool hardFLUSH = false);
77
78    /** @returns the first FastFactory */
[4947]79    inline static FastFactory* getFirst() { return FastFactory::first; };
[4940]80
[4950]81    static FastFactory* searchFastFactory(ClassID classID);
82    static FastFactory* searchFastFactory(const char* fastFactoryName);
[4947]83
[4950]84    ClassID getStoredID() const { return this->storedClassID; };
85
[4940]86  protected:
87    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
88
89    /** sets the Next factory in the list @param nextFactory the next factory */
[4947]90    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
[4940]91    /** @returns the next FastFactory */
[4947]92    FastFactory* getNext() const { return this->next; };
[4940]93
94    /** generates a new Object of the Class T */
[5279]95    virtual void fabricate() = 0;
[4940]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
[4941]113
114
[4940]115/**
116 *  a FastFactory that is able to load any kind of Object from a ClassID
117 * (this is a Functor)
118 */
119template<class T>
120class tFastFactory : public FastFactory
121  {
122  public:
123    static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
124
125  private:
126    tFastFactory(ClassID classID, const char* fastFactoryName);
127
128    virtual void fabricate();
129  };
130
131/**
132 * construnts a FastFactory with
133 * @param fastFactoryName the name of the FastFactory
134 * @param fastFactory the ID of the class
135 * @todo (can this be written in another form??)
136 */
137template<class T>
138tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
139    : FastFactory(classID, fastFactoryName)
140{}
141
142/**
143 * creates (if not existent) a Factory of Class T, and assigns some values to it
144 * @param classID the ClassID to assign to this class
145 * @param fastFactoryName the name to assign
146 * @returns The FastFactory if existent a new Factory if not.
147 */
148template<class T>
149tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
150{
151  tFastFactory<T>* tmpFac = NULL;
152  if (FastFactory::getFirst() != NULL)
[4950]153    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID));
[4940]154
155  if (tmpFac != NULL)
156    return tmpFac;
157  else
158    return new tFastFactory<T>(classID, fastFactoryName);
159}
160
161/**
162 * fabricates an Object of Class T, that corresponds to classID.
163 */
164template<class T>
165void tFastFactory<T>::fabricate()
166{
167  FastObjectMember* tmpFirstDead = new FastObjectMember;
168  tmpFirstDead->objectPointer = new T();
169  tmpFirstDead->next = this->deadList;
170  ++this->storedDeadObjects;
171
172  this->deadList = tmpFirstDead;
173}
174
175#endif /* _FAST_FACTORY_H */
Note: See TracBrowser for help on using the repository browser.