Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new garbage-collection-algorithm works, but the entities are not correctly setup for re-entering the scene.

File size: 6.0 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
21using namespace std;
22
23/**
24 * Initializes a FastFactory
25 * @param classID the ClassID this Class belongs to (the top-most)
26 * @param fastFactoryName the Name of the ObjectClass-handled here
27 * @return a new FastFactory
28 */
29FastFactory::FastFactory (ClassID classID, const char* fastFactoryName)
30{
31  this->setClassID(CL_FAST_FACTORY, "FastFactory");
32  this->setName(fastFactoryName);
33
34  this->storedClassID = classID;
35  this->next = NULL;
36
37  this->deadList = NULL;
38  this->unusedContainers = NULL;
39
40  this->storedDeadObjects = 0;
41
42  FastFactory::registerFastFactory(this);
43}
44
45/** a reference to the First FastFactory */
46FastFactory* FastFactory::first = NULL;
47
48/**
49 *  destructor
50 * deletes all the Instances of the FastFactory.
51 */
52FastFactory::~FastFactory ()
53{
54  if (this == first)
55    this->first = NULL;
56
57  if (this->next)
58    delete this->next;
59}
60
61/**
62 * registers a Factory to the List of known factories.
63 * @param fastFactory The factory to add
64 *
65 * needed, to step through all the FastFactories.
66 */
67void FastFactory::registerFastFactory(FastFactory* fastFactory)
68{
69  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
70
71  if( FastFactory::first == NULL)
72    FastFactory::first = fastFactory;
73  else
74  {
75    FastFactory* tmpFac = FastFactory::first;
76    while( tmpFac->next != NULL)
77      tmpFac = tmpFac->next;
78    tmpFac->setNext(fastFactory);
79  }
80}
81
82
83/**
84 * searches for a FastFactory
85 * @param factoryName the Name of the Factory to search for (not used)
86 * @param classID the ClassID of the FastFactory to search for
87 * @returns true if found, false otherwise.
88 */
89FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName)
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 * Removes all the stored Containers, and sets the Lists back to emptienes.
108 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
109 */
110void FastFactory::flushAll(bool hardFLUSH)
111{
112  FastFactory* tmpFac = FastFactory::first;
113  while (tmpFac != NULL)
114  {
115    tmpFac->flush(hardFLUSH);
116    tmpFac = tmpFac->next;
117  }
118}
119
120
121/**
122 * ereases all the remaining containers, without deleting the stored Objects inside of them.
123 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
124 */
125void FastFactory::flush(bool hardFLUSH)
126{
127  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
128  while (tmpMember != NULL)
129  {
130    delMember = tmpMember;
131    tmpMember = tmpMember->next;
132    if (unlikely(hardFLUSH == true))
133      delete delMember->objectPointer;
134    delete delMember;
135  }
136  this->deadList = NULL;
137
138  tmpMember = this->unusedContainers;
139  while (tmpMember != NULL)
140  {
141    delMember = tmpMember;
142    tmpMember = tmpMember->next;
143    delete delMember;
144  }
145  this->unusedContainers = NULL;
146}
147
148/**
149 * generates count new Object of the Corresponding class. (precaching)
150 * @param count How many instances of the class should be generated.
151 */
152void FastFactory::prepare(unsigned int count)
153{
154/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
155  {
156  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
157}*/
158  for (int i = this->storedDeadObjects; i < count; i++)
159  {
160    this->fabricate();
161  }
162}
163
164/**
165 * gives back live to one Object.
166 * @return the Object to resurrect.
167 */
168BaseObject* FastFactory::resurrect()
169{
170  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
171  if (unlikely(this->deadList == NULL))
172  {
173    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" \
174        "Fabricating a new %s", this->getName(), this->getName());
175    this->fabricate();
176    return this->resurrect();
177  }
178  else
179  {
180    FastObjectMember* tmpC = this->deadList;
181    this->deadList = this->deadList->next;
182
183    tmpC->next = this->unusedContainers;
184    this->unusedContainers = tmpC;
185
186    return tmpC->objectPointer;
187  }
188}
189
190/**
191 * gives back live to one Object.
192 * @param classID the class From which to resurrect an Object.
193 * @return the Object to resurrect, NULL if classID is not found.
194 */
195BaseObject* FastFactory::resurrect(ClassID classID)
196{
197  FastFactory* tmpFac = FastFactory::getFirst();
198
199  while (tmpFac != NULL)
200  {
201    if (classID == tmpFac->storedClassID)
202      return tmpFac->resurrect();
203    tmpFac = tmpFac->next;
204  }
205  return NULL;
206}
207
208/**
209 * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
210 * @param object the Object to kill.
211 *
212 * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode.
213 */
214void FastFactory::kill(BaseObject* object)
215{
216  FastObjectMember* tmpC;
217  if (unlikely(this->unusedContainers == NULL))
218  {
219    tmpC = new FastObjectMember;
220  }
221  else
222  {
223    tmpC = this->unusedContainers;
224    this->unusedContainers = this->unusedContainers->next;
225  }
226
227  tmpC->next = this->deadList;
228  tmpC->objectPointer = object;
229  this->deadList = tmpC;
230}
231
232
233void FastFactory::kill(BaseObject* object, bool searchForFastFactory)
234{
235  if (likely(searchForFastFactory == true))
236  {
237    FastFactory* tmpFac = FastFactory::first;
238    while (tmpFac != NULL)
239    {
240      if (object->isA(tmpFac->storedClassID))
241      {
242        tmpFac->kill(object);
243        return;
244      }
245      tmpFac = tmpFac->next;
246    }
247
248  }
249}
Note: See TracBrowser for help on using the repository browser.