Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/fast_factory.h @ 9869

Last change on this file since 9869 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 6.4 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  FastFactory* global_##CLASS_NAME##_FastFactory = tFastFactory<CLASS_NAME>::getFastFactory(CLASS_ID, #CLASS_NAME)
32/**
33 * Creates a FastFactory for a Class' static function named ClassName::fastFactory.
34 * @param CLASS_NAME the name of the Class to create the fast-factory for.
35 * @param CLASS_ID the ID of the class to create the fast-factory for @see "class_id_DEPRECATED.h"
36 *
37 * notice, that the Class to be called, must implement:
38 * static FastFactory*         fastFactory;
39 */
40#define CREATE_FAST_FACTORY_STATIC(CLASS_NAME) \
41  FastFactory* CLASS_NAME::fastFactory = tFastFactory<CLASS_NAME>::getFastFactory(CLASS_NAME::staticClassID(), #CLASS_NAME)
42
43//! A struct, that holds Lists of Objects of a certain type.
44typedef struct FastObjectMember
45{
46  BaseObject*          objectPointer;      //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork)
47
48  FastObjectMember*    next;               //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers)
49};
50
51//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
52/**
53 * The ObjectManager (FastFactory) is designed, to automatically generate and remove
54 * (without to much overhead) many instances of different classes.
55 *
56 * FastFactory is needed to glue all the tFastFactories together.
57 * It is also the general class that implements the necessary functions
58 * to generate, resurrect kill and stuff...
59 */
60class FastFactory : public BaseObject
61{
62  ObjectListDeclaration(FastFactory);
63
64public:
65  virtual ~FastFactory ();
66  static void deleteAll();
67
68  // functions to push and pop elements of this class
69  BaseObject* resurrect();
70  static BaseObject* resurrect(const ClassID& classID);
71  void kill(BaseObject* object);
72  static void kill(BaseObject* object, bool searchForFastFactory);
73
74  void prepare(unsigned int count);
75
76  static void flushAll(bool hardFLUSH = false);
77  void flush(bool hardFLUSH = false);
78
79  /** @returns the first FastFactory */
80  inline static FastFactory* getFirst() { return FastFactory::first; };
81
82  static FastFactory* searchFastFactory(const ClassID& classID);
83  static FastFactory* searchFastFactory(const std::string& fastFactoryName);
84
85  const ClassID& getStoredID() const { return this->storedClassID; };
86
87protected:
88  FastFactory (const ClassID& classID, const std::string& fastFactoryName = "");
89
90  /** sets the Next factory in the list @param nextFactory the next factory */
91  inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
92  /** @returns the next FastFactory */
93  FastFactory* getNext() const { return this->next; };
94
95  /** generates a new Object of the Class T */
96  virtual void fabricate() = 0;
97
98private:
99  static void registerFastFactory(FastFactory* fastFactory);
100
101protected:
102  ClassID            storedClassID;        //!< The classID of the specified class.
103  unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
104
105  FastObjectMember*     deadList;             //!< A List of all stored dead Objects of this class.
106  FastObjectMember*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
107
108private:
109  static FastFactory*   first;                //!< A pointer to the first FastFactory.
110
111  FastFactory*          next;                 //!< pointer to the next FastFactory.
112};
113
114
115
116/**
117 *  a FastFactory that is able to load any kind of Object from a ClassID
118 * (this is a Functor)
119 */
120template<class T>
121class tFastFactory : public FastFactory
122{
123public:
124  static tFastFactory<T>* getFastFactory(const ClassID& classID, const std::string& fastFactoryName);
125
126private:
127  tFastFactory(const ClassID& classID, const std::string& fastFactoryName);
128
129  virtual void fabricate();
130};
131
132/**
133 * construnts a FastFactory with
134 * @param fastFactoryName the name of the FastFactory
135 * @param fastFactory the ID of the class
136 * @todo (can this be written in another form??)
137 */
138template<class T>
139tFastFactory<T>::tFastFactory(const ClassID& classID, const std::string& fastFactoryName)
140    : FastFactory(classID, fastFactoryName)
141{}
142
143/**
144 * creates (if not existent) a Factory of Class T, and assigns some values to it
145 * @param classID the ClassID to assign to this class
146 * @param fastFactoryName the name to assign
147 * @returns The FastFactory if existent a new Factory if not.
148 */
149template<class T>
150tFastFactory<T>* tFastFactory<T>::getFastFactory(const ClassID& classID, const std::string& fastFactoryName)
151{
152  tFastFactory<T>* tmpFac = NULL;
153  if (FastFactory::getFirst() != NULL)
154    tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID));
155
156  if (tmpFac != NULL)
157    return tmpFac;
158  else
159    return new tFastFactory<T>(classID, fastFactoryName);
160}
161
162/**
163 * fabricates an Object of Class T, that corresponds to classID.
164 */
165template<class T>
166void tFastFactory<T>::fabricate()
167{
168  FastObjectMember* tmpFirstDead = new FastObjectMember;
169  tmpFirstDead->objectPointer = new T();
170  tmpFirstDead->next = this->deadList;
171  ++this->storedDeadObjects;
172
173  this->deadList = tmpFirstDead;
174}
175
176#endif /* _FAST_FACTORY_H */
Note: See TracBrowser for help on using the repository browser.