Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: FastFactory: some more movement to the BaseClass. Now it is almost where i want it
doxygen tags will follow at 2 o'Clock in the morning …

File size: 5.2 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
27
28/**
29 *  constructor, sets everything to zero and define factoryName
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 *  clear the Q
53 */
54FastFactory::~FastFactory ()
55{
56  if (this == first)
57    this->first = NULL;
58
59  if (this->next)
60    delete this->next;
61}
62
63void FastFactory::registerFastFactory(FastFactory* fastFactory)
64{
65  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
66
67  if( FastFactory::first == NULL)
68    FastFactory::first = fastFactory;
69  else
70  {
71    FastFactory* tmpFac = FastFactory::first;
72    while( tmpFac->next != NULL)
73      tmpFac = tmpFac->next;
74    tmpFac->setNext(fastFactory);
75  }
76}
77
78
79/**
80 * searches for a FastFactory
81 * @param factoryName the Name of the Factory to search for (not used)
82 * @param classID the ClassID of the FastFactory to search for
83 * @returns true if found, false otherwise.
84 */
85FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName)
86{
87  if (FastFactory::first == NULL)
88    return NULL;
89   else
90   {
91     FastFactory* tmpFac = FastFactory::first;
92     while (tmpFac != NULL)
93     {
94       if (tmpFac->storedClassID == classID)
95         return tmpFac;
96       tmpFac = tmpFac->next;
97     }
98   }
99   return NULL;
100}
101
102/**
103 *
104 */
105void FastFactory::flushAll(bool hardFLUSH)
106{
107  FastFactory* tmpFac = FastFactory::first;
108  while (tmpFac != NULL)
109  {
110    tmpFac->flush(hardFLUSH);
111    tmpFac = tmpFac->next;
112  }
113}
114
115
116/**
117 * ereases all the remaining containers, without deleting the stored Objects inside of them.
118 */
119void FastFactory::flush(bool hardFLUSH)
120{
121  FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
122  while (tmpMember != NULL)
123  {
124    delMember = tmpMember;
125    tmpMember = tmpMember->next;
126    if (unlikely(hardFLUSH == true))
127      delete delMember->objectPointer;
128    delete delMember;
129  }
130  this->deadList = NULL;
131
132  tmpMember = this->unusedContainers;
133  while (tmpMember != NULL)
134  {
135    delMember = tmpMember;
136    tmpMember = tmpMember->next;
137    delete delMember;
138  }
139  this->unusedContainers = NULL;
140}
141
142
143
144void FastFactory::prepare(unsigned int count)
145{
146/*  if (this->storedDeadObjects + this->storedLivingObjects >= count)
147  {
148  PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
149}*/
150  for (int i = this->storedDeadObjects; i < count; i++)
151  {
152    this->fabricate();
153  }
154}
155
156
157BaseObject* FastFactory::resurect(ClassID classID)
158{
159  PRINTF(4)("Resurecting Object of type %s\n", this->getName());
160  if (unlikely(this->deadList == NULL))
161  {
162    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" \
163        "Fabricating a new %s", this->getName(), this->getName());
164    this->fabricate();
165    return this->resurect(classID);
166  }
167  else
168  {
169  FastObjectMember* tmpC = deadList;
170  this->deadList = this->deadList->next;
171
172  tmpC->next = this->unusedContainers;
173  this->unusedContainers = tmpC;
174
175  return tmpC->objectPointer;
176  }
177}
178
179
180
181void FastFactory::kill(ClassID classID, BaseObject* object)
182{
183  FastObjectMember* tmpC;
184  if (unlikely(this->unusedContainers == NULL))
185  {
186    tmpC = new FastObjectMember;
187  }
188  else
189  {
190    tmpC = this->unusedContainers;
191    this->unusedContainers = this->unusedContainers->next;
192  }
193
194  tmpC->next = this->deadList;
195  tmpC->objectPointer = object;
196}
197
198
199
200
201
202
203
204
205
206
207
208/**
209 *  standard constructor
210*/
211ObjectManager::ObjectManager ()
212{
213  this->setClassID(CL_OBJECT_MANAGER, "ObjectManager");
214  this->setName("ObjectManager");
215
216}
217
218
219/**
220 *  the singleton reference to this class
221*/
222ObjectManager* ObjectManager::singletonRef = NULL;
223
224/**
225 *  standard deconstructor
226*/
227ObjectManager::~ObjectManager ()
228{
229  ObjectManager::singletonRef = NULL;
230}
231
232/**
233 *  outputs some simple debug information about the ObjectManage
234*/
235void ObjectManager::debug() const
236{
237  PRINT(0)("\n==========================| ObjectManager::debug() |===\n");
238/* PRINT(0)("=  Number of registerable classes: %i\n", CL_NUMBER );
239 PRINT(0)("=  Currently cached objects: \n");
240 for(int i = 0; i < CL_NUMBER; ++i)
241   {
242      if( this->managedObjectList[i] != NULL)
243        PRINT(0)("=   o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize());
244      else
245        PRINT(0)("=   o Class Nr. %i has cached 0 object(s)\n", i);
246    }*/
247  PRINT(0)("=======================================================\n");
248}
249
250
Note: See TracBrowser for help on using the repository browser.