Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new garbage-collection-algorithm works, but the entities are not correctly setup for re-entering the scene.

File size: 5.5 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    static FastFactory* getFirst()
67    {
68      return FastFactory::first;
69    };
70
71  protected:
72    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
73
74    /** sets the Next factory in the list @param nextFactory the next factory */
75    inline void setNext( FastFactory* nextFastFactory)
76    {
77      this->next = nextFastFactory;
78    };
79    /** @returns the next FastFactory */
80    FastFactory* getNext() const
81      {
82        return this->next;
83      };
84
85    /** generates a new Object of the Class T */
86    virtual void fabricate() = NULL;
87    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
88
89  private:
90    static void registerFastFactory(FastFactory* fastFactory);
91
92  protected:
93    ClassID               storedClassID;        //!< The classID of the specified class.
94    unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
95
96    FastObjectMember*     deadList;             //!< A List of all stored dead Objects of this class.
97    FastObjectMember*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
98
99  private:
100    static FastFactory*   first;                //!< A pointer to the first FastFactory.
101
102    FastFactory*          next;                 //!< pointer to the next FastFactory.
103  };
104
105
106
107/**
108 *  a FastFactory that is able to load any kind of Object from a ClassID
109 * (this is a Functor)
110 */
111template<class T>
112class tFastFactory : public FastFactory
113  {
114  public:
115    static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
116
117  private:
118    tFastFactory(ClassID classID, const char* fastFactoryName);
119
120    virtual void fabricate();
121  };
122
123/**
124 * construnts a FastFactory with
125 * @param fastFactoryName the name of the FastFactory
126 * @param fastFactory the ID of the class
127 * @todo (can this be written in another form??)
128 */
129template<class T>
130tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
131    : FastFactory(classID, fastFactoryName)
132{}
133
134/**
135 * creates (if not existent) a Factory of Class T, and assigns some values to it
136 * @param classID the ClassID to assign to this class
137 * @param fastFactoryName the name to assign
138 * @returns The FastFactory if existent a new Factory if not.
139 */
140template<class T>
141tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
142{
143  tFastFactory<T>* tmpFac = NULL;
144  if (FastFactory::getFirst() != NULL)
145    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName));
146
147  if (tmpFac != NULL)
148    return tmpFac;
149  else
150    return new tFastFactory<T>(classID, fastFactoryName);
151}
152
153/**
154 * fabricates an Object of Class T, that corresponds to classID.
155 */
156template<class T>
157void tFastFactory<T>::fabricate()
158{
159  FastObjectMember* tmpFirstDead = new FastObjectMember;
160  tmpFirstDead->objectPointer = new T();
161  tmpFirstDead->next = this->deadList;
162  ++this->storedDeadObjects;
163
164  this->deadList = tmpFirstDead;
165}
166
167#endif /* _FAST_FACTORY_H */
Note: See TracBrowser for help on using the repository browser.