Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/garbage_collector.cc @ 3668

Last change on this file since 3668 was 3668, checked in by patrick, 19 years ago

orxonox/trunk: made list more performant and less vulnerable to segfaults. changed the benchmark function to only display list attributes for now. changed pnode to iterator

File size: 3.0 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
19
20#include "garbage_collector.h"
21
22#include "world.h"
23#include "world_entity.h"
24#include "null_parent.h"
25
26#include "list.h"
27
28using namespace std;
29
30
31GarbageCollector* GarbageCollector::singletonRef = 0;
32
33/**
34   \brief standard constructor
35   \todo this constructor is not jet implemented - do it
36*/
37GarbageCollector::GarbageCollector () 
38{
39   this->setClassName ("GarbageCollection");
40   this->time = 0;
41   this->delay = 3.0f; /* clean up all 5.0 seconds */
42}
43
44
45/**
46   \brief standard deconstructor
47
48*/
49GarbageCollector::~GarbageCollector () 
50{
51  // delete what has to be deleted here
52}
53
54
55/**
56   \brief this returns the singleton reference this this class
57   \returns singleton reference
58*/
59GarbageCollector* GarbageCollector::getInstance()
60{
61  if( singletonRef == NULL)
62    singletonRef = new GarbageCollector();
63  return singletonRef;
64}
65
66
67/**
68   \brief this sets the collection delay
69   \param delay
70   
71   after this delay, the garbage collector starts its work and begins to collect unused object
72   to delete them afterwards. only objects in the worldentity list from the world object are lookded
73   at.
74*/
75void GarbageCollector::setCollectionDelay(float delay)
76{
77  this->delay = delay;
78}
79
80
81/**
82   \brief this foreces a garbage collection
83
84   if this function is called, the gc tries to initiate the garbage collection routines. actually
85   this should always work.
86*/
87void GarbageCollector::forceCollection()
88{
89  /* just make the time slitely bigger than the delay */
90  this->time = this->delay + 1;
91  /* and update, to get rid of the unused objects */
92  this->update();
93}
94
95
96/**
97   \brief this ticks the GarbageCollector to give it the time pulse
98   \param the time passed since last tick
99
100   like every other tick function eg. worldentity
101*/
102void GarbageCollector::tick(float time)
103{
104  this->time += time;
105}
106
107
108/**
109   \brief this updated the gargabe collection, if the time is ready
110
111
112*/
113void GarbageCollector::update()
114{
115  if( this->time < this->delay)
116    return;
117  /* garbage collect */
118  PRINTF(3)("GarbageCollection is been done\n");
119  WorldInterface* wi = WorldInterface::getInstance();
120  tList<WorldEntity>* list = wi->getEntityList();
121
122  tIterator<WorldEntity>* iterator = list->getIterator();
123  WorldEntity* entity = iterator->nextElement();
124  while( entity != NULL) 
125    { 
126      if( entity->isFinalized())
127        {
128          PRINTF(1)("Finalizing object in list - not yet done realy\n");
129         
130          /* first remove out of entity list */
131          list->remove(entity);
132          /* second remove out of pnode tree */
133          //entity->remove();
134          //delete entity;
135        }
136      entity = iterator->nextElement();
137    }
138 
139  /* reset time to count again up to this->delay */
140  this->time = 0;
141}
Note: See TracBrowser for help on using the repository browser.