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, 20 years ago

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

File size: 6.3 KB
RevLine 
[4591]1/*!
[4938]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.
[4245]17*/
18
19
20#ifndef _OBJECT_MANAGER_H
21#define _OBJECT_MANAGER_H
22
23#include "base_object.h"
24
[4938]25
26// FORWARD DECLARATION //
27class GarbageCollector;
[4930]28template<class T> class tList;
[4285]29
[4932]30/**
31 * Creates a FastFactory to a Loadable FastFactory.
32 */
[4933]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)
[4591]35
[4932]36//! A struct, that holds Lists of Objects of a certain type.
[4935]37struct FastObjectMember
[4932]38{
[4938]39  BaseObject*          objectPointer;      //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork)
[4699]40
[4938]41  FastObjectMember*    next;               //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers)
[4932]42};
[4245]43
[4932]44//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
[4933]45/**
[4938]46 * The ObjectManager (FastFactory) is designed, to automatically generate and remove
47 * (without to much overhead) many instances of different classes.
48 *
[4933]49 * FastFactory is needed to glue all the tFastFactory<T>'s together.
[4938]50 * It is also the general class that implements the necessary functions
51 * to generate, resurrect kill and stuff...
[4933]52 */
[4932]53class FastFactory : public BaseObject {
[4930]54
[4932]55  public:
56    virtual ~FastFactory ();
[4930]57
[4933]58    // functions to push and pop elements of this class
[4938]59    BaseObject* resurrect();
60    static BaseObject* resurrect(ClassID classID);
61    void kill(BaseObject* object);
[4939]62    static void kill(BaseObject* object, bool searchForFastFactory);
[4311]63
[4937]64    void prepare(unsigned int count);
[4933]65    // retrival functions for fast Ineraction
66    //FastFactory* getFastFactory(ClassID classID);
[4245]67
[4936]68    static void flushAll(bool hardFLUSH = false);
69    void flush(bool hardFLUSH = false);
70
[4932]71    /** @returns the first FastFactory */
72    static FastFactory* getFirst() { return FastFactory::first; };
[4245]73
[4932]74  protected:
[4934]75    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
76
[4930]77    /** sets the Next factory in the list @param nextFactory the next factory */
[4932]78    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
79    /** @returns the next FastFactory */
80    FastFactory* getNext() const { return this->next; };
[4930]81
[4939]82    /** generates a new Object of the Class T */
[4938]83    virtual void fabricate() = NULL;
[4934]84    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
85
[4933]86  private:
87    static void registerFastFactory(FastFactory* fastFactory);
88
[4931]89  protected:
[4932]90    ClassID               storedClassID;        //!< The classID of the specified class.
[4933]91    unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
[4931]92
[4935]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
[4930]96  private:
[4932]97    static FastFactory*   first;                //!< A pointer to the first FastFactory.
98
99    FastFactory*          next;                 //!< pointer to the next FastFactory.
[4930]100};
101
102/**
[4932]103 *  a FastFactory that is able to load any kind of Object from a ClassID
[4931]104 * (this is a Functor)
[4930]105 */
[4932]106template<class T> class tFastFactory : public FastFactory
[4930]107{
108  public:
[4934]109    static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
[4930]110
111  private:
[4933]112    tFastFactory(ClassID classID, const char* fastFactoryName);
113
[4936]114    virtual void fabricate();
[4930]115};
116
117/**
[4932]118 * construnts a FastFactory with
119 * @param fastFactoryName the name of the FastFactory
120 * @param fastFactory the ID of the class
[4939]121 * @todo (can this be written in another form??)
[4930]122 */
123template<class T>
[4933]124    tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
125  : FastFactory(classID, fastFactoryName)
[4930]126{
127}
128
[4939]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 */
[4930]135template<class T>
[4934]136    tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
[4930]137{
[4933]138  tFastFactory<T>* tmpFac = NULL;
139  if (FastFactory::getFirst() != NULL)
[4934]140    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName));
[4933]141
142  if (tmpFac != NULL)
143    return tmpFac;
[4932]144  else
[4934]145    return new tFastFactory<T>(classID, fastFactoryName);
[4930]146}
147
[4932]148
[4939]149/**
150 * fabricates an Object of Class T, that corresponds to classID.
151 */
[4933]152template<class T>
[4936]153    void tFastFactory<T>::fabricate()
[4933]154{
[4935]155  FastObjectMember* tmpFirstDead = new FastObjectMember;
[4936]156  tmpFirstDead->objectPointer = new T();
[4933]157  tmpFirstDead->next = this->deadList;
158  ++this->storedDeadObjects;
[4932]159
[4933]160  this->deadList = tmpFirstDead;
161}
[4932]162
[4933]163
164
165
[4938]166
167
168
169
170
[4932]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
[4938]185  BaseObject* resurrect();
[4932]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
[4245]201#endif /* _OBJECT_MANAGER_H */
Note: See TracBrowser for help on using the repository browser.