Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ProjectileFactory now handled in Weapon-class

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