Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/lang/class_list.cc @ 4778

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

orxonox/trunk: not Saving the BaseObjects anymore, now the ClassList is 10 times faster
it used 7% of the process-time of orxonox (kprof-analysis) now it only takes .7%, i hope this is good enough

File size: 5.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   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "class_list.h"
19#include "base_object.h"
20
21#include "list.h"
22#include "compiler.h"
23#include "debug.h"
24#include <string.h>
25#include <math.h>
26
27using namespace std;
28
29
30/**
31   \brief Creates a new ClassList
32*/
33ClassList::ClassList(const long& classID, const char* className)
34{
35  this->next = NULL;
36  this->className = className;
37  this->classID = classID;
38  this->objectList = new tList<BaseObject>;
39
40  ++ClassList::classCount;
41}
42
43
44/**
45   \brief standard deconstructor
46*/
47ClassList::~ClassList ()
48{
49  --ClassList::classCount;
50}
51
52//! the first class that is registered
53ClassList*  ClassList::first = NULL;
54
55//! the Count of classes
56unsigned int ClassList::classCount = 0;
57
58/**
59 * Adds a new Object to the ClassList (and if necessary a new Class)
60 * @param objectPointer Pointer to the Object at hand
61 * @param classID ID of the Given ObjectType \see ClassID
62 * @param className name of the Class to add
63 */
64void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className)
65{
66  ClassList* regClass;
67
68  if(ClassList::first == NULL)
69    ClassList::first = regClass = new ClassList(classID, className);
70  else
71  {
72    ClassList* tmp = ClassList::first;
73    while (likely(tmp != NULL))
74    {
75      if (tmp->classID == classID)
76      {
77        regClass = tmp;
78        break;
79      }
80
81      if (tmp->next == NULL)
82        tmp->next = regClass = new ClassList(classID, className);
83      tmp = tmp->next;
84    }
85  }
86
87  regClass->objectList->add(objectPointer);
88}
89
90/**
91 * removes an Object from a the ClassList
92 * @param objectPointer the Object to delete from the List
93 */
94void ClassList::removeFromClassList(BaseObject* objectPointer)
95{
96  ClassList* tmp = ClassList::first;
97  while (likely(tmp != NULL))
98  {
99    if (objectPointer->isA(tmp->classID))
100    {
101      tmp->objectList->remove(objectPointer);
102    }
103
104    tmp = tmp->next;
105  }
106}
107
108/**
109 * checks if the BaseObject* object exists.
110 * @param name the name of the BaseObject to look for
111 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
112 * @return true, if the Object Exists in the specified ClassID, false otherwise
113 * @todo: speed this up!!
114 */
115BaseObject* ClassList::getObject(const char* name, long classID)
116{
117  if(unlikely(ClassList::first == NULL) || name == NULL)
118    return NULL;
119  else
120  {
121    ClassList* tmp = ClassList::first;
122    while (likely(tmp != NULL))
123    {
124      if (tmp->classID == classID || classID == CL_NULL)
125      {
126        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
127        BaseObject* enumBO = iterator->nextElement();
128        const char* tmpName;
129        while (enumBO != NULL)
130        {
131          tmpName = enumBO->getName();
132          if (tmpName && !strcmp(tmpName, name))
133          {
134            delete iterator;
135            return enumBO;
136          }
137          enumBO = iterator->nextElement();
138        }
139        delete iterator;
140        break;
141      }
142      tmp = tmp->next;
143    }
144  }
145  return NULL;
146}
147
148
149/**
150 * checks if the BaseObject* object exists.
151 * @param object the Pointer to a BaseObject to check if it exists
152 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
153 * @return true, if the Object Exists in the specified ClassID, false otherwise
154 * @todo: speed this up!!
155 */
156bool ClassList::exists(BaseObject* object, long classID)
157{
158  if(unlikely(ClassList::first == NULL))
159    return false;
160  else
161  {
162    ClassList* tmp = ClassList::first;
163    while (likely(tmp != NULL))
164    {
165      if (tmp->classID == classID || classID == CL_NULL)
166      {
167        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
168        BaseObject* enumBO = iterator->nextElement();
169        while (enumBO != NULL)
170        {
171          if (enumBO == object)
172          {
173            delete iterator;
174            return true;
175          }
176          enumBO = iterator->nextElement();
177        }
178        delete iterator;
179        break;
180      }
181      tmp = tmp->next;
182    }
183  }
184  return false;
185}
186
187/**
188 * Print out some very nice debug information
189 */
190    void ClassList::debug(unsigned int debugLevel)
191{
192  if (debugLevel > 2)
193    debugLevel = 2;
194  PRINT(0)("==========================\n");
195  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
196  PRINT(0)("==========================\n");
197  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
198  ClassList* tmp = ClassList::first;
199  char niceString[100];
200  unsigned int lenCount = 0;
201
202  while (likely(tmp != NULL))
203  {
204    if (debugLevel >= 1 || tmp->objectList->getSize() > 0)
205    {
206      lenCount = 1;
207      while (pow(10,lenCount) <= tmp->objectList->getSize())
208        ++lenCount;
209      for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
210        (niceString[i]) = ' ';
211      niceString[30-strlen(tmp->className) - lenCount] = '\0';
212
213      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
214
215      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
216      {
217        PRINT(0)("|  Listing Instances:\n");
218        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
219        BaseObject* enumBO = iterator->nextElement();
220        while (enumBO)
221        {
222          PRINT(0)("|   (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO);
223          enumBO = iterator->nextElement();
224        }
225        delete iterator;
226      }
227    }
228    tmp = tmp->next;
229  }
230  PRINT(0)("=======================CL=\n");
231}
Note: See TracBrowser for help on using the repository browser.