Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/fast_factory.cc @ 10344

Last change on this file since 10344 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 6.7 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{
159  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
160  while (tmpMember != NULL)
161  {
162    delMember = tmpMember;
163    tmpMember = tmpMember->next;
164    if (hardFLUSH)
165      delete delMember->objectPointer;
166    delete delMember;
167  }
168  this->deadList = NULL;
169
170  tmpMember = this->unusedContainers;
171  while (tmpMember != NULL)
172  {
173    delMember = tmpMember;
174    tmpMember = tmpMember->next;
175    delete delMember;
176  }
177  this->unusedContainers = NULL;
178}
179
180/**
181 * generates count new Object of the Corresponding class. (precaching)
182 * @param count How many instances of the class should be generated.
183 */
184void FastFactory::prepare(unsigned int count)
185{
186/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
187  {
188  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassCName());
189}*/
190  for (unsigned int i = this->storedDeadObjects; i < count; i++)
191  {
192    this->fabricate();
193  }
194}
195
196/**
197 * gives back live to one Object.
198 * @return the Object to resurrect.
199 */
200BaseObject* FastFactory::resurrect()
201{
202  PRINTF(4)("Resurecting Object of type %s\n", this->getCName());
203  if (unlikely(this->deadList == NULL))
204  {
205    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" \
206        "  Developer: try increasing the count with FastFactory::prepare(contHigher than actual)\n" \
207        "    Fabricating a new %s\n", this->getCName(), this->getCName());
208    this->fabricate();
209    return this->resurrect();
210  }
211  else
212  {
213    FastObjectMember* tmpC = this->deadList;
214    this->deadList = this->deadList->next;
215
216    tmpC->next = this->unusedContainers;
217    this->unusedContainers = tmpC;
218
219    return tmpC->objectPointer;
220  }
221}
222
223/**
224 * gives back live to one Object.
225 * @param classID the class From which to resurrect an Object.
226 * @return the Object to resurrect, NULL if classID is not found.
227 */
228BaseObject* FastFactory::resurrect(const ClassID& classID)
229{
230  FastFactory* tmpFac = FastFactory::getFirst();
231
232  while (tmpFac != NULL)
233  {
234    if (classID == tmpFac->storedClassID)
235      return tmpFac->resurrect();
236    tmpFac = tmpFac->next;
237  }
238  return NULL;
239}
240
241/**
242 * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
243 * @param object the Object to kill.
244 *
245 * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode.
246 */
247void FastFactory::kill(BaseObject* object)
248{
249  FastObjectMember* tmpC;
250  if (unlikely(this->unusedContainers == NULL))
251  {
252    tmpC = new FastObjectMember;
253  }
254  else
255  {
256    tmpC = this->unusedContainers;
257    this->unusedContainers = this->unusedContainers->next;
258  }
259
260  tmpC->next = this->deadList;
261  tmpC->objectPointer = object;
262  this->deadList = tmpC;
263}
264
265
266void FastFactory::kill(BaseObject* object, bool searchForFastFactory)
267{
268  if (likely(searchForFastFactory == true))
269  {
270    FastFactory* tmpFac = FastFactory::first;
271    while (tmpFac != NULL)
272    {
273      if (object->isA(tmpFac->storedClassID))
274      {
275        tmpFac->kill(object);
276        return;
277      }
278      tmpFac = tmpFac->next;
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.