Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4937 was 4937, checked in by bensch, 20 years ago

orxonox/trunk: FastFactory: some more movement to the BaseClass. Now it is almost where i want it
doxygen tags will follow at 2 o'Clock in the morning …

File size: 5.9 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    void prepare(unsigned int count);
60    // retrival functions for fast Ineraction
61    //FastFactory* getFastFactory(ClassID classID);
62
63    static void flushAll(bool hardFLUSH = false);
64    void flush(bool hardFLUSH = false);
65
66    /** @returns the first FastFactory */
67    static FastFactory* getFirst() { return FastFactory::first; };
68
69  protected:
70    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
71
72    /** sets the Next factory in the list @param nextFactory the next factory */
73    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
74    /** @returns the next FastFactory */
75    FastFactory* getNext() const { return this->next; };
76
77//    virtual BaseObject* fabricate(ClassID classID) = NULL;
78
79    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = 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 *  a FastFactory that is able to load any kind of Object from a ClassID
99 * (this is a Functor)
100 */
101template<class T> class tFastFactory : public FastFactory
102{
103  public:
104    static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
105
106    T* resurect();
107  private:
108    tFastFactory(ClassID classID, const char* fastFactoryName);
109
110    virtual void fabricate();
111
112  private:
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 */
120template<class T>
121    tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
122  : FastFactory(classID, fastFactoryName)
123{
124}
125
126template<class T>
127    tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
128{
129  tFastFactory<T>* tmpFac = NULL;
130  if (FastFactory::getFirst() != NULL)
131    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName));
132
133  if (tmpFac != NULL)
134    return tmpFac;
135  else
136    return new tFastFactory<T>(classID, fastFactoryName);
137}
138
139
140template<class T>
141    void tFastFactory<T>::fabricate()
142{
143  FastObjectMember* tmpFirstDead = new FastObjectMember;
144  tmpFirstDead->objectPointer = new T();
145  tmpFirstDead->next = this->deadList;
146  ++this->storedDeadObjects;
147
148  this->deadList = tmpFirstDead;
149}
150
151template<class T>
152    T* tFastFactory<T>::resurect()
153{
154  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
155  if (unlikely(this->deadList == NULL))
156  {
157    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" \
158        "Fabricating a new %s", this->getName(), this->getName());
159    this->fabricate();
160    return resurect();
161  }
162  else
163  {
164    FastObjectMember* tmpC = deadList;
165    this->deadList = this->deadList->next;
166
167    tmpC->next = this->unusedContainers;
168    this->unusedContainers = tmpC;
169
170    return dynamic_cast<T*>(tmpC->objectPointer);
171  }
172}
173
174////////////////////
175// OBJECT MANAGER //
176////////////////////
177
178//! the object manager itself
179class ObjectManager : public BaseObject {
180
181 public:
182  virtual ~ObjectManager();
183  /** @returns a Pointer to the only object of this Class */
184  inline static ObjectManager* getInstance() { if (!singletonRef) singletonRef = new ObjectManager();  return singletonRef; };
185
186  void registerClass(ClassID classID);
187
188  BaseObject* resurect();
189  void kill(BaseObject* object);
190
191
192  void debug() const;
193
194 private:
195  ObjectManager();
196
197 private:
198  static ObjectManager*      singletonRef;          //!< The singleton reference to the only reference of this class
199
200};
201
202
203
204#endif /* _OBJECT_MANAGER_H */
Note: See TracBrowser for help on using the repository browser.