Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now also the FastFactories get deleted at the end

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