Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/fast_factory.cc @ 4951

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

orxonox/trunk: the new WeaponManager is online :)

File size: 6.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_OBJECT_MANAGER
16
17#include "fast_factory.h"
18#include "compiler.h"
19#include "debug.h"
20#include "string.h"
21
22using namespace std;
23
24/**
25 * Initializes a FastFactory
26 * @param classID the ClassID this Class belongs to (the top-most)
27 * @param fastFactoryName the Name of the ObjectClass-handled here
28 * @return a new FastFactory
29 */
30FastFactory::FastFactory (ClassID classID, const char* fastFactoryName)
31{
32  this->setClassID(CL_FAST_FACTORY, "FastFactory");
33  this->setName(fastFactoryName);
34
35  this->storedClassID = classID;
36  this->next = NULL;
37
38  this->deadList = NULL;
39  this->unusedContainers = NULL;
40
41  this->storedDeadObjects = 0;
42
43  FastFactory::registerFastFactory(this);
44}
45
46/** a reference to the First FastFactory */
47FastFactory* FastFactory::first = NULL;
48
49/**
50 *  destructor
51 * deletes all the Instances of the FastFactory.
52 */
53FastFactory::~FastFactory ()
54{
55  if (this == first)
56    this->first = NULL;
57
58  if (this->next)
59    delete this->next;
60}
61
62/**
63 * registers a Factory to the List of known factories.
64 * @param fastFactory The factory to add
65 *
66 * needed, to step through all the FastFactories.
67 */
68void FastFactory::registerFastFactory(FastFactory* fastFactory)
69{
70  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
71
72  if( FastFactory::first == NULL)
73    FastFactory::first = fastFactory;
74  else
75  {
76    FastFactory* tmpFac = FastFactory::first;
77    while( tmpFac->next != NULL)
78      tmpFac = tmpFac->next;
79    tmpFac->setNext(fastFactory);
80  }
81}
82
83
84/**
85 * searches for a FastFactory
86 * @param classID the ClassID of the FastFactory to search for
87 * @returns true if found, false otherwise.
88 */
89FastFactory* FastFactory::searchFastFactory(ClassID classID)
90{
91  if (FastFactory::first == NULL)
92    return NULL;
93  else
94  {
95    FastFactory* tmpFac = FastFactory::first;
96    while (tmpFac != NULL)
97    {
98      if (tmpFac->storedClassID == classID)
99        return tmpFac;
100      tmpFac = tmpFac->next;
101    }
102  }
103  return NULL;
104}
105
106/**
107 * searches for a FastFactory
108 * @param classID the ClassID of the FastFactory to search for
109 * @returns true if found, false otherwise.
110 */
111FastFactory* FastFactory::searchFastFactory(const char* fastFactoryName)
112{
113  if (FastFactory::first == NULL)
114    return NULL;
115  else
116  {
117    FastFactory* tmpFac = FastFactory::first;
118    while (tmpFac != NULL)
119    {
120      if (strcmp(tmpFac->getName(), fastFactoryName))
121        return tmpFac;
122      tmpFac = tmpFac->next;
123    }
124  }
125  return NULL;
126}
127
128/**
129 * Removes all the stored Containers, and sets the Lists back to emptienes.
130 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
131 */
132void FastFactory::flushAll(bool hardFLUSH)
133{
134  FastFactory* tmpFac = FastFactory::first;
135  while (tmpFac != NULL)
136  {
137    tmpFac->flush(hardFLUSH);
138    tmpFac = tmpFac->next;
139  }
140}
141
142
143/**
144 * ereases all the remaining containers, without deleting the stored Objects inside of them.
145 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
146 */
147void FastFactory::flush(bool hardFLUSH)
148{
149  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
150  while (tmpMember != NULL)
151  {
152    delMember = tmpMember;
153    tmpMember = tmpMember->next;
154    if (unlikely(hardFLUSH == true))
155      delete delMember->objectPointer;
156    delete delMember;
157  }
158  this->deadList = NULL;
159
160  tmpMember = this->unusedContainers;
161  while (tmpMember != NULL)
162  {
163    delMember = tmpMember;
164    tmpMember = tmpMember->next;
165    delete delMember;
166  }
167  this->unusedContainers = NULL;
168}
169
170/**
171 * generates count new Object of the Corresponding class. (precaching)
172 * @param count How many instances of the class should be generated.
173 */
174void FastFactory::prepare(unsigned int count)
175{
176/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
177  {
178  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
179}*/
180  for (int i = this->storedDeadObjects; i < count; i++)
181  {
182    this->fabricate();
183  }
184}
185
186/**
187 * gives back live to one Object.
188 * @return the Object to resurrect.
189 */
190BaseObject* FastFactory::resurrect()
191{
192  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
193  if (unlikely(this->deadList == NULL))
194  {
195    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" \
196        "Fabricating a new %s\n", this->getName(), this->getName());
197    this->fabricate();
198    return this->resurrect();
199  }
200  else
201  {
202    FastObjectMember* tmpC = this->deadList;
203    this->deadList = this->deadList->next;
204
205    tmpC->next = this->unusedContainers;
206    this->unusedContainers = tmpC;
207
208    return tmpC->objectPointer;
209  }
210}
211
212/**
213 * gives back live to one Object.
214 * @param classID the class From which to resurrect an Object.
215 * @return the Object to resurrect, NULL if classID is not found.
216 */
217BaseObject* FastFactory::resurrect(ClassID classID)
218{
219  FastFactory* tmpFac = FastFactory::getFirst();
220
221  while (tmpFac != NULL)
222  {
223    if (classID == tmpFac->storedClassID)
224      return tmpFac->resurrect();
225    tmpFac = tmpFac->next;
226  }
227  return NULL;
228}
229
230/**
231 * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
232 * @param object the Object to kill.
233 *
234 * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode.
235 */
236void FastFactory::kill(BaseObject* object)
237{
238  FastObjectMember* tmpC;
239  if (unlikely(this->unusedContainers == NULL))
240  {
241    tmpC = new FastObjectMember;
242  }
243  else
244  {
245    tmpC = this->unusedContainers;
246    this->unusedContainers = this->unusedContainers->next;
247  }
248
249  tmpC->next = this->deadList;
250  tmpC->objectPointer = object;
251  this->deadList = tmpC;
252}
253
254
255void FastFactory::kill(BaseObject* object, bool searchForFastFactory)
256{
257  if (likely(searchForFastFactory == true))
258  {
259    FastFactory* tmpFac = FastFactory::first;
260    while (tmpFac != NULL)
261    {
262      if (object->isA(tmpFac->storedClassID))
263      {
264        tmpFac->kill(object);
265        return;
266      }
267      tmpFac = tmpFac->next;
268    }
269
270  }
271}
Note: See TracBrowser for help on using the repository browser.