Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/util/fast_factory.h @ 5808

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

orxonox/trunk: some minor cleanup, of the mess i made with AutoMake-sh

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