Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4974 was 4969, checked in by bensch, 20 years ago

orxonox/trunk: ability to set a Slots Direction

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