Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Objects now get cleanly ereased.
This is a fix in the Weapon-class, that kills the Resurected Projectiles created for information-gathering

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