Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/lib/util/loading/fast_factory.cc

Last change on this file was 10368, checked in by patrick, 18 years ago

merged the branche playability into the trunk

File size: 6.8 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_LOAD
16
17#include "fast_factory.h"
18
19#include "compiler.h"
20#include "debug.h"
21
22
23ObjectListDefinition(FastFactory);
24
25/**
26 * @brief Initializes a FastFactory
27 * @param classID the ClassID this Class belongs to (the top-most)
28 * @param fastFactoryName the Name of the ObjectClass-handled here
29 * @return a new FastFactory
30 */
31FastFactory::FastFactory (const ClassID& classID, const std::string& fastFactoryName)
32{
33  this->registerObject(this, FastFactory::_objectList);
34  this->setName(fastFactoryName);
35
36  this->storedClassID = classID;
37  this->next = NULL;
38
39  this->deadList = NULL;
40  this->unusedContainers = NULL;
41
42  this->storedDeadObjects = 0;
43
44  FastFactory::registerFastFactory(this);
45}
46
47/** a reference to the First FastFactory */
48FastFactory* FastFactory::first = NULL;
49
50/**
51 *  destructor
52 * deletes all the Instances of the FastFactory.
53 */
54FastFactory::~FastFactory ()
55{
56  if (this == first)
57    this->first = NULL;
58  this->flush();
59
60  if (this->next)
61    delete this->next;
62}
63
64/**
65 * deletes all FastFactories
66 */
67void FastFactory::deleteAll()
68{
69  if (FastFactory::first)
70    delete FastFactory::first;
71}
72
73/**
74 * registers a Factory to the List of known factories.
75 * @param fastFactory The factory to add
76 *
77 * needed, to step through all the FastFactories.
78 */
79void FastFactory::registerFastFactory(FastFactory* fastFactory)
80{
81  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getCName());
82
83  if( FastFactory::first == NULL)
84    FastFactory::first = fastFactory;
85  else
86  {
87    FastFactory* tmpFac = FastFactory::first;
88    while( tmpFac->next != NULL)
89      tmpFac = tmpFac->next;
90    tmpFac->setNext(fastFactory);
91  }
92}
93
94/**
95 * searches for a FastFactory
96 * @param classID the ClassID of the FastFactory to search for
97 * @returns true if found, false otherwise.
98 */
99FastFactory* FastFactory::searchFastFactory(const ClassID& classID)
100{
101  if (FastFactory::first == NULL)
102    return NULL;
103  else
104  {
105    FastFactory* tmpFac = FastFactory::first;
106    while (tmpFac != NULL)
107    {
108      if (tmpFac->storedClassID == classID)
109        return tmpFac;
110      tmpFac = tmpFac->next;
111    }
112  }
113  return NULL;
114}
115
116/**
117 * searches for a FastFactory
118 * @param classID the ClassID of the FastFactory to search for
119 * @returns true if found, false otherwise.
120 */
121FastFactory* FastFactory::searchFastFactory(const std::string& fastFactoryName)
122{
123  if (FastFactory::first == NULL)
124    return NULL;
125  else
126  {
127    FastFactory* tmpFac = FastFactory::first;
128    while (tmpFac != NULL)
129    {
130      if (fastFactoryName == tmpFac->getCName())
131        return tmpFac;
132      tmpFac = tmpFac->next;
133    }
134  }
135  return NULL;
136}
137
138/**
139 * Removes all the stored Containers, and sets the Lists back to emptienes.
140 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
141 */
142void FastFactory::flushAll(bool hardFLUSH)
143{
144  FastFactory* tmpFac = FastFactory::first;
145  while (tmpFac != NULL)
146  {
147    PRINTF(4)("DELETEING ALL OF %s\n",tmpFac->getCName());
148    tmpFac->flush(hardFLUSH);
149    tmpFac = tmpFac->next;
150  }
151}
152
153/**
154 * ereases all the remaining containers, without deleting the stored Objects inside of them.
155 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
156 */
157void FastFactory::flush(bool hardFLUSH)
158{
159hardFLUSH = false; // HACK
160  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
161  while (tmpMember != NULL)
162  {
163    delMember = tmpMember;
164    tmpMember = tmpMember->next;
165    if (hardFLUSH)
166      delete delMember->objectPointer;
167    delete delMember;
168  }
169  this->deadList = NULL;
170
171  tmpMember = this->unusedContainers;
172  while (tmpMember != NULL)
173  {
174    delMember = tmpMember;
175    tmpMember = tmpMember->next;
176    delete delMember;
177  }
178  this->unusedContainers = NULL;
179}
180
181/**
182 * generates count new Object of the Corresponding class. (precaching)
183 * @param count How many instances of the class should be generated.
184 */
185void FastFactory::prepare(unsigned int count)
186{
187/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
188  {
189  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassCName());
190}*/
191  for (unsigned int i = this->storedDeadObjects; i < count; i++)
192  {
193    this->fabricate();
194  }
195}
196
197/**
198 * gives back live to one Object.
199 * @return the Object to resurrect.
200 */
201BaseObject* FastFactory::resurrect()
202{
203  PRINTF(4)("Resurecting Object of type %s\n", this->getCName());
204  if (unlikely(this->deadList == NULL))
205  {
206    PRINTF(3)("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" \
207        "  Developer: try increasing the count with FastFactory::prepare(contHigher than actual)\n" \
208        "    Fabricating a new %s\n", this->getCName(), this->getCName());
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(const 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.