Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/util/fast_factory.cc @ 9703

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

orxonox/new_object_list: fast factory, ObjectManager, and Hud adapted

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