Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/object_manager.cc @ 4938

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

orxonox/trunk: doxygen tags.

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: Patrick Boenzli
13*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_OBJECT_MANAGER
16
17#include "object_manager.h"
18#include "garbage_collector.h"
19#include "list.h"
20
21#include "debug.h"
22
23using namespace std;
24
25/**
26 * 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 (ClassID classID, const char* fastFactoryName)
32{
33  this->setClassID(CL_FAST_FACTORY, "FastFactory");
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
59  if (this->next)
60    delete this->next;
61}
62
63/**
64 * registers a Factory to the List of known factories.
65 * @param fastFactory The factory to add
66 *
67 * needed, to step through all the FastFactories.
68 */
69void FastFactory::registerFastFactory(FastFactory* fastFactory)
70{
71  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
72
73  if( FastFactory::first == NULL)
74    FastFactory::first = fastFactory;
75  else
76  {
77    FastFactory* tmpFac = FastFactory::first;
78    while( tmpFac->next != NULL)
79      tmpFac = tmpFac->next;
80    tmpFac->setNext(fastFactory);
81  }
82}
83
84
85/**
86 * searches for a FastFactory
87 * @param factoryName the Name of the Factory to search for (not used)
88 * @param classID the ClassID of the FastFactory to search for
89 * @returns true if found, false otherwise.
90 */
91FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName)
92{
93  if (FastFactory::first == NULL)
94    return NULL;
95   else
96   {
97     FastFactory* tmpFac = FastFactory::first;
98     while (tmpFac != NULL)
99     {
100       if (tmpFac->storedClassID == classID)
101         return tmpFac;
102       tmpFac = tmpFac->next;
103     }
104   }
105   return NULL;
106}
107
108/**
109 * Removes all the stored Containers, and sets the Lists back to emptienes.
110 * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
111 */
112void FastFactory::flushAll(bool hardFLUSH)
113{
114  FastFactory* tmpFac = FastFactory::first;
115  while (tmpFac != NULL)
116  {
117    tmpFac->flush(hardFLUSH);
118    tmpFac = tmpFac->next;
119  }
120}
121
122
123/**
124 * ereases all the remaining containers, without deleting the stored Objects inside of them.
125 * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
126 */
127void FastFactory::flush(bool hardFLUSH)
128{
129  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
130  while (tmpMember != NULL)
131  {
132    delMember = tmpMember;
133    tmpMember = tmpMember->next;
134    if (unlikely(hardFLUSH == true))
135      delete delMember->objectPointer;
136    delete delMember;
137  }
138  this->deadList = NULL;
139
140  tmpMember = this->unusedContainers;
141  while (tmpMember != NULL)
142  {
143    delMember = tmpMember;
144    tmpMember = tmpMember->next;
145    delete delMember;
146  }
147  this->unusedContainers = NULL;
148}
149
150/**
151 * generates count new Object of the Corresponding class. (precaching)
152 * @param count How many instances of the class should be generated.
153 */
154void FastFactory::prepare(unsigned int count)
155{
156/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
157  {
158  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
159}*/
160  for (int i = this->storedDeadObjects; i < count; i++)
161  {
162    this->fabricate();
163  }
164}
165
166/**
167 * gives back live to one Object.
168 * @return the Object to resurrect.
169 */
170BaseObject* FastFactory::resurrect()
171{
172  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
173  if (unlikely(this->deadList == NULL))
174  {
175    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" \
176        "Fabricating a new %s", this->getName(), this->getName());
177    this->fabricate();
178    return this->resurrect();
179  }
180  else
181  {
182  FastObjectMember* tmpC = deadList;
183  this->deadList = this->deadList->next;
184
185  tmpC->next = this->unusedContainers;
186  this->unusedContainers = tmpC;
187
188  return tmpC->objectPointer;
189  }
190}
191
192/**
193 * gives back live to one Object.
194 * @param classID the class From which to resurrect an Object.
195 * @return the Object to resurrect, NULL if classID is not found.
196 */
197BaseObject* FastFactory::resurrect(ClassID classID)
198{
199  FastFactory* tmpFac = FastFactory::getFirst();
200
201  while (tmpFac != NULL)
202  {
203    if (classID == tmpFac->storedClassID)
204      return tmpFac->resurrect();
205    tmpFac = tmpFac->next;
206  }
207  return NULL;
208}
209
210/**
211 * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
212 * @param object the Object to kill.
213 */
214void FastFactory::kill(BaseObject* object)
215{
216  FastObjectMember* tmpC;
217  if (unlikely(this->unusedContainers == NULL))
218  {
219    tmpC = new FastObjectMember;
220  }
221  else
222  {
223    tmpC = this->unusedContainers;
224    this->unusedContainers = this->unusedContainers->next;
225  }
226
227  tmpC->next = this->deadList;
228  tmpC->objectPointer = object;
229  this->deadList = tmpC;
230}
231
232
233void FastFactory::kill(BaseObject* object, ClassID classID)
234{
235
236
237}
238
239
240
241
242
243
244
245
246/**
247 *  standard constructor
248*/
249ObjectManager::ObjectManager ()
250{
251  this->setClassID(CL_OBJECT_MANAGER, "ObjectManager");
252  this->setName("ObjectManager");
253
254}
255
256
257/**
258 *  the singleton reference to this class
259*/
260ObjectManager* ObjectManager::singletonRef = NULL;
261
262/**
263 *  standard deconstructor
264*/
265ObjectManager::~ObjectManager ()
266{
267  ObjectManager::singletonRef = NULL;
268}
269
270/**
271 *  outputs some simple debug information about the ObjectManage
272*/
273void ObjectManager::debug() const
274{
275  PRINT(0)("\n==========================| ObjectManager::debug() |===\n");
276/* PRINT(0)("=  Number of registerable classes: %i\n", CL_NUMBER );
277 PRINT(0)("=  Currently cached objects: \n");
278 for(int i = 0; i < CL_NUMBER; ++i)
279   {
280      if( this->managedObjectList[i] != NULL)
281        PRINT(0)("=   o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize());
282      else
283        PRINT(0)("=   o Class Nr. %i has cached 0 object(s)\n", i);
284    }*/
285  PRINT(0)("=======================================================\n");
286}
287
288
Note: See TracBrowser for help on using the repository browser.