Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/fast_factory.cc @ 9808

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

fixed a copy bug… wow… this took 2 hours again

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#include <string.h>
22
23
24ObjectListDefinition(FastFactory);
25
26/**
27 * @brief Initializes a FastFactory
28 * @param classID the ClassID this Class belongs to (the top-most)
29 * @param fastFactoryName the Name of the ObjectClass-handled here
30 * @return a new FastFactory
31 */
32FastFactory::FastFactory (const ClassID& classID, const std::string& fastFactoryName)
33{
34  this->registerObject(this, FastFactory::_objectList);
35  this->setName(fastFactoryName);
36
37  this->storedClassID = classID;
38  this->next = NULL;
39
40  this->deadList = NULL;
41  this->unusedContainers = NULL;
42
43  this->storedDeadObjects = 0;
44
45  FastFactory::registerFastFactory(this);
46}
47
48/** a reference to the First FastFactory */
49FastFactory* FastFactory::first = NULL;
50
51/**
52 *  destructor
53 * deletes all the Instances of the FastFactory.
54 */
55FastFactory::~FastFactory ()
56{
57  if (this == first)
58    this->first = NULL;
59  this->flush();
60
61  if (this->next)
62    delete this->next;
63}
64
65/**
66 * deletes all FastFactories
67 */
68void FastFactory::deleteAll()
69{
70  if (FastFactory::first)
71    delete FastFactory::first;
72}
73
74/**
75 * registers a Factory to the List of known factories.
76 * @param fastFactory The factory to add
77 *
78 * needed, to step through all the FastFactories.
79 */
80void FastFactory::registerFastFactory(FastFactory* fastFactory)
81{
82  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getCName());
83
84  if( FastFactory::first == NULL)
85    FastFactory::first = fastFactory;
86  else
87  {
88    FastFactory* tmpFac = FastFactory::first;
89    while( tmpFac->next != NULL)
90      tmpFac = tmpFac->next;
91    tmpFac->setNext(fastFactory);
92  }
93}
94
95/**
96 * searches for a FastFactory
97 * @param classID the ClassID of the FastFactory to search for
98 * @returns true if found, false otherwise.
99 */
100FastFactory* FastFactory::searchFastFactory(const ClassID& classID)
101{
102  if (FastFactory::first == NULL)
103    return NULL;
104  else
105  {
106    FastFactory* tmpFac = FastFactory::first;
107    while (tmpFac != NULL)
108    {
109      if (tmpFac->storedClassID == classID)
110        return tmpFac;
111      tmpFac = tmpFac->next;
112    }
113  }
114  return NULL;
115}
116
117/**
118 * searches for a FastFactory
119 * @param classID the ClassID of the FastFactory to search for
120 * @returns true if found, false otherwise.
121 */
122FastFactory* FastFactory::searchFastFactory(const std::string& fastFactoryName)
123{
124  if (FastFactory::first == NULL)
125    return NULL;
126  else
127  {
128    FastFactory* tmpFac = FastFactory::first;
129    while (tmpFac != NULL)
130    {
131      if (fastFactoryName == tmpFac->getCName())
132        return tmpFac;
133      tmpFac = tmpFac->next;
134    }
135  }
136  return NULL;
137}
138
139/**
140 * Removes all the stored Containers, and sets the Lists back to emptienes.
141 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
142 */
143void FastFactory::flushAll(bool hardFLUSH)
144{
145  FastFactory* tmpFac = FastFactory::first;
146  while (tmpFac != NULL)
147  {
148    PRINTF(4)("DELETEING ALL OF %s\n",tmpFac->getCName());
149    tmpFac->flush(hardFLUSH);
150    tmpFac = tmpFac->next;
151  }
152}
153
154/**
155 * ereases all the remaining containers, without deleting the stored Objects inside of them.
156 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
157 */
158void FastFactory::flush(bool hardFLUSH)
159{
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.