Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Objects now get cleanly ereased.
This is a fix in the Weapon-class, that kills the Resurected Projectiles created for information-gathering

File size: 6.5 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 * searches for a FastFactory
85 * @param classID the ClassID of the FastFactory to search for
86 * @returns true if found, false otherwise.
87 */
88FastFactory* FastFactory::searchFastFactory(ClassID classID)
89{
90  if (FastFactory::first == NULL)
91    return NULL;
92  else
93  {
94    FastFactory* tmpFac = FastFactory::first;
95    while (tmpFac != NULL)
96    {
97      if (tmpFac->storedClassID == classID)
98        return tmpFac;
99      tmpFac = tmpFac->next;
100    }
101  }
102  return NULL;
103}
104
105/**
106 * searches for a FastFactory
107 * @param classID the ClassID of the FastFactory to search for
108 * @returns true if found, false otherwise.
109 */
110FastFactory* FastFactory::searchFastFactory(const char* fastFactoryName)
111{
112  if (FastFactory::first == NULL)
113    return NULL;
114  else
115  {
116    FastFactory* tmpFac = FastFactory::first;
117    while (tmpFac != NULL)
118    {
119      if (strcmp(tmpFac->getName(), fastFactoryName))
120        return tmpFac;
121      tmpFac = tmpFac->next;
122    }
123  }
124  return NULL;
125}
126
127/**
128 * Removes all the stored Containers, and sets the Lists back to emptienes.
129 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
130 */
131void FastFactory::flushAll(bool hardFLUSH)
132{
133  FastFactory* tmpFac = FastFactory::first;
134  while (tmpFac != NULL)
135  {
136    printf("DELETEING ALL OF %s\n",tmpFac->getName());
137    tmpFac->flush(hardFLUSH);
138    tmpFac = tmpFac->next;
139  }
140}
141
142/**
143 * ereases all the remaining containers, without deleting the stored Objects inside of them.
144 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
145 */
146void FastFactory::flush(bool hardFLUSH)
147{
148  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
149  while (tmpMember != NULL)
150  {
151    delMember = tmpMember;
152    tmpMember = tmpMember->next;
153    if (hardFLUSH)
154    {
155      PRINTF(1)("COOL\n");
156      delete delMember->objectPointer;
157    }
158    delete delMember;
159  }
160  this->deadList = NULL;
161
162  tmpMember = this->unusedContainers;
163  while (tmpMember != NULL)
164  {
165    delMember = tmpMember;
166    tmpMember = tmpMember->next;
167    delete delMember;
168  }
169  this->unusedContainers = NULL;
170}
171
172/**
173 * generates count new Object of the Corresponding class. (precaching)
174 * @param count How many instances of the class should be generated.
175 */
176void FastFactory::prepare(unsigned int count)
177{
178/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
179  {
180  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
181}*/
182  for (int i = this->storedDeadObjects; i < count; i++)
183  {
184    this->fabricate();
185  }
186}
187
188/**
189 * gives back live to one Object.
190 * @return the Object to resurrect.
191 */
192BaseObject* FastFactory::resurrect()
193{
194  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
195  if (unlikely(this->deadList == NULL))
196  {
197    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" \
198        "Fabricating a new %s\n", this->getName(), this->getName());
199    this->fabricate();
200    return this->resurrect();
201  }
202  else
203  {
204    FastObjectMember* tmpC = this->deadList;
205    this->deadList = this->deadList->next;
206
207    tmpC->next = this->unusedContainers;
208    this->unusedContainers = tmpC;
209
210    return tmpC->objectPointer;
211  }
212}
213
214/**
215 * gives back live to one Object.
216 * @param classID the class From which to resurrect an Object.
217 * @return the Object to resurrect, NULL if classID is not found.
218 */
219BaseObject* FastFactory::resurrect(ClassID classID)
220{
221  FastFactory* tmpFac = FastFactory::getFirst();
222
223  while (tmpFac != NULL)
224  {
225    if (classID == tmpFac->storedClassID)
226      return tmpFac->resurrect();
227    tmpFac = tmpFac->next;
228  }
229  return NULL;
230}
231
232/**
233 * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
234 * @param object the Object to kill.
235 *
236 * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode.
237 */
238void FastFactory::kill(BaseObject* object)
239{
240  FastObjectMember* tmpC;
241  if (unlikely(this->unusedContainers == NULL))
242  {
243    tmpC = new FastObjectMember;
244  }
245  else
246  {
247    tmpC = this->unusedContainers;
248    this->unusedContainers = this->unusedContainers->next;
249  }
250
251  tmpC->next = this->deadList;
252  tmpC->objectPointer = object;
253  this->deadList = tmpC;
254}
255
256
257void FastFactory::kill(BaseObject* object, bool searchForFastFactory)
258{
259  if (likely(searchForFastFactory == true))
260  {
261    FastFactory* tmpFac = FastFactory::first;
262    while (tmpFac != NULL)
263    {
264      if (object->isA(tmpFac->storedClassID))
265      {
266        tmpFac->kill(object);
267        return;
268      }
269      tmpFac = tmpFac->next;
270    }
271  }
272}
Note: See TracBrowser for help on using the repository browser.