Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/object_manager.h @ 4939

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

orxonox/trunk: now the garbage collector should be ready to store the Objects

File size: 6.3 KB
Line 
1/*!
2 * @file object_manager.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 _OBJECT_MANAGER_H
21#define _OBJECT_MANAGER_H
22
23#include "base_object.h"
24
25
26// FORWARD DECLARATION //
27class GarbageCollector;
28template<class T> class tList;
29
30/**
31 * Creates a FastFactory to a Loadable FastFactory.
32 */
33//#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \
34    //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
35
36//! A struct, that holds Lists of Objects of a certain type.
37struct FastObjectMember
38{
39  BaseObject*          objectPointer;      //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork)
40
41  FastObjectMember*    next;               //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers)
42};
43
44//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
45/**
46 * The ObjectManager (FastFactory) is designed, to automatically generate and remove
47 * (without to much overhead) many instances of different classes.
48 *
49 * FastFactory is needed to glue all the tFastFactory<T>'s together.
50 * It is also the general class that implements the necessary functions
51 * to generate, resurrect kill and stuff...
52 */
53class FastFactory : public BaseObject {
54
55  public:
56    virtual ~FastFactory ();
57
58    // functions to push and pop elements of this class
59    BaseObject* resurrect();
60    static BaseObject* resurrect(ClassID classID);
61    void kill(BaseObject* object);
62    static void kill(BaseObject* object, bool searchForFastFactory);
63
64    void prepare(unsigned int count);
65    // retrival functions for fast Ineraction
66    //FastFactory* getFastFactory(ClassID classID);
67
68    static void flushAll(bool hardFLUSH = false);
69    void flush(bool hardFLUSH = false);
70
71    /** @returns the first FastFactory */
72    static FastFactory* getFirst() { return FastFactory::first; };
73
74  protected:
75    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
76
77    /** sets the Next factory in the list @param nextFactory the next factory */
78    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
79    /** @returns the next FastFactory */
80    FastFactory* getNext() const { return this->next; };
81
82    /** generates a new Object of the Class T */
83    virtual void fabricate() = NULL;
84    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
85
86  private:
87    static void registerFastFactory(FastFactory* fastFactory);
88
89  protected:
90    ClassID               storedClassID;        //!< The classID of the specified class.
91    unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
92
93    FastObjectMember*     deadList;             //!< A List of all stored dead Objects of this class.
94    FastObjectMember*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
95
96  private:
97    static FastFactory*   first;                //!< A pointer to the first FastFactory.
98
99    FastFactory*          next;                 //!< pointer to the next FastFactory.
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> class 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>
124    tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
125  : FastFactory(classID, fastFactoryName)
126{
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>
136    tFastFactory<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, fastFactoryName));
141
142  if (tmpFac != NULL)
143    return tmpFac;
144  else
145    return new tFastFactory<T>(classID, fastFactoryName);
146}
147
148
149/**
150 * fabricates an Object of Class T, that corresponds to classID.
151 */
152template<class T>
153    void tFastFactory<T>::fabricate()
154{
155  FastObjectMember* tmpFirstDead = new FastObjectMember;
156  tmpFirstDead->objectPointer = new T();
157  tmpFirstDead->next = this->deadList;
158  ++this->storedDeadObjects;
159
160  this->deadList = tmpFirstDead;
161}
162
163
164
165
166
167
168
169
170
171////////////////////
172// OBJECT MANAGER //
173////////////////////
174
175//! the object manager itself
176class ObjectManager : public BaseObject {
177
178 public:
179  virtual ~ObjectManager();
180  /** @returns a Pointer to the only object of this Class */
181  inline static ObjectManager* getInstance() { if (!singletonRef) singletonRef = new ObjectManager();  return singletonRef; };
182
183  void registerClass(ClassID classID);
184
185  BaseObject* resurrect();
186  void kill(BaseObject* object);
187
188
189  void debug() const;
190
191 private:
192  ObjectManager();
193
194 private:
195  static ObjectManager*      singletonRef;          //!< The singleton reference to the only reference of this class
196
197};
198
199
200
201#endif /* _OBJECT_MANAGER_H */
Note: See TracBrowser for help on using the repository browser.