Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more loadability functionality in Weapon and FastFactory

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