Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: FastFactory: gets flushed now → devMail is withdrawn now.
Also reimplemented much stuff, so it now works most out of the BaseClass. → still some minor stuff to fix.
GarbageCollector, i will come to you soon …

File size: 6.4 KB
Line 
1/*!
2    \file object_manager.h
3  *  this manager will ceep track of the objects  in the world
4
5    This is specially designed to:
6    - Give an interface to the world data
7    - separate the world data from the world build,update,draw process
8    - recycle deleted objects: specific for Projectils since there is a lot of world entity creation/deletion (and this needs a lot of time)
9    - control the garbage collector
10
11    TO ADD SUPPORT FOR A CLASS do the following steps:
12    1. include the hader file : #include "class_id.h"
13    2. add the class to the type enum classID {}; in class_id.h
14    3. define a function void mCache( ClassName ) in class ObjectManager
15
16*/
17
18
19#ifndef _OBJECT_MANAGER_H
20#define _OBJECT_MANAGER_H
21
22#include "base_object.h"
23#include "class_id.h"
24
25template<class T> class tList;
26class GarbageCollector;
27
28
29/**
30 * Creates a FastFactory to a Loadable FastFactory.
31 */
32//#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \
33    //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
34
35//! A struct, that holds Lists of Objects of a certain type.
36struct FastObjectMember
37{
38  BaseObject*               objectPointer;
39
40  FastObjectMember*         next;
41};
42
43//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
44/**
45 * FastFactory is needed to glue all the tFastFactory<T>'s together.
46 * Furthermore one can retrieve the corresponding tFastFactory over
47 * tFastFactory<T>* = FastFacroty::getFastFactory(ID);
48 */
49class FastFactory : public BaseObject {
50
51  public:
52    virtual ~FastFactory ();
53
54    // functions to push and pop elements of this class
55    BaseObject* resurect(ClassID classID);
56    void kill(ClassID classID, BaseObject* object);
57
58    virtual void fabricate() = NULL;
59    // retrival functions for fast Ineraction
60    //FastFactory* getFastFactory(ClassID classID);
61
62    static void flushAll(bool hardFLUSH = false);
63    void flush(bool hardFLUSH = false);
64
65    /** @returns the first FastFactory */
66    static FastFactory* getFirst() { return FastFactory::first; };
67
68  protected:
69    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
70
71    /** sets the Next factory in the list @param nextFactory the next factory */
72    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
73    /** @returns the next FastFactory */
74    FastFactory* getNext() const { return this->next; };
75
76//    virtual BaseObject* fabricate(ClassID classID) = NULL;
77
78    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
79
80  private:
81    static void registerFastFactory(FastFactory* fastFactory);
82
83  protected:
84    ClassID               storedClassID;        //!< The classID of the specified class.
85    unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
86
87    FastObjectMember*     deadList;             //!< A List of all stored dead Objects of this class.
88    FastObjectMember*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
89
90  private:
91    static FastFactory*   first;                //!< A pointer to the first FastFactory.
92
93    FastFactory*          next;                 //!< pointer to the next FastFactory.
94};
95
96/**
97 *  a FastFactory that is able to load any kind of Object from a ClassID
98 * (this is a Functor)
99 */
100template<class T> class tFastFactory : public FastFactory
101{
102  public:
103    static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
104
105    void prepare(unsigned int count);
106
107    T* resurect();
108  private:
109    tFastFactory(ClassID classID, const char* fastFactoryName);
110
111    virtual void fabricate();
112
113  private:
114};
115
116/**
117 * construnts a FastFactory with
118 * @param fastFactoryName the name of the FastFactory
119 * @param fastFactory the ID of the class
120 */
121template<class T>
122    tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
123  : FastFactory(classID, fastFactoryName)
124{
125  PRINTF(5)("Class: %s loadable as a FastFactory\n", this->getName());
126
127  this->deadList = NULL;
128  this->unusedContainers = NULL;
129}
130
131template<class T>
132    tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
133{
134  tFastFactory<T>* tmpFac = NULL;
135  if (FastFactory::getFirst() != NULL)
136    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName));
137
138  if (tmpFac != NULL)
139    return tmpFac;
140  else
141    return new tFastFactory<T>(classID, fastFactoryName);
142}
143
144
145template<class T>
146    void tFastFactory<T>::fabricate()
147{
148  FastObjectMember* tmpFirstDead = new FastObjectMember;
149  tmpFirstDead->objectPointer = new T();
150  tmpFirstDead->next = this->deadList;
151  ++this->storedDeadObjects;
152
153  this->deadList = tmpFirstDead;
154}
155
156template<class T>
157    void tFastFactory<T>::prepare(unsigned int count)
158{
159/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
160  {
161    PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
162  }*/
163  for (int i = this->storedDeadObjects; i < count; i++)
164  {
165    this->fabricate();
166  }
167}
168
169template<class T>
170    T* tFastFactory<T>::resurect()
171{
172  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
173  if (unlikely(this->deadList == NULL))
174  {
175    PRINTF(2)("The deadList of Class %s is empty, this may be either because it has not been filled yet, or the cache is to small.\n" \
176        "Fabricating a new %s", this->getName(), this->getName());
177    this->fabricate();
178    return resurect();
179  }
180  else
181  {
182    FastObjectMember* tmpC = deadList;
183    this->deadList = this->deadList->next;
184
185    tmpC->next = this->unusedContainers;
186    this->unusedContainers = tmpC;
187
188    return dynamic_cast<T*>(tmpC->objectPointer);
189  }
190}
191
192////////////////////
193// OBJECT MANAGER //
194////////////////////
195
196//! the object manager itself
197class ObjectManager : public BaseObject {
198
199 public:
200  virtual ~ObjectManager();
201  /** @returns a Pointer to the only object of this Class */
202  inline static ObjectManager* getInstance() { if (!singletonRef) singletonRef = new ObjectManager();  return singletonRef; };
203
204  void registerClass(ClassID classID);
205
206  BaseObject* resurect();
207  void kill(BaseObject* object);
208
209
210  void debug() const;
211
212 private:
213  ObjectManager();
214
215 private:
216  static ObjectManager*      singletonRef;          //!< The singleton reference to the only reference of this class
217
218};
219
220
221
222#endif /* _OBJECT_MANAGER_H */
Note: See TracBrowser for help on using the repository browser.