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
RevLine 
[4743]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:
[4750]12   main-programmer: Benjamin Grauer
[4743]13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "class_list.h"
[4747]19#include "base_object.h"
[4743]20
[4752]21#include "list.h"
[4747]22#include "compiler.h"
23#include "debug.h"
[4748]24#include <string.h>
25#include <math.h>
[4747]26
[4743]27using namespace std;
28
29
30/**
[4748]31   \brief Creates a new ClassList
[4743]32*/
[4747]33ClassList::ClassList(const long& classID, const char* className)
[4743]34{
[4747]35  this->next = NULL;
36  this->className = className;
37  this->classID = classID;
[4752]38  this->objectList = new tList<BaseObject>;
[4748]39
40  ++ClassList::classCount;
[4743]41}
42
43
44/**
45   \brief standard deconstructor
46*/
47ClassList::~ClassList ()
48{
[4748]49  --ClassList::classCount;
[4743]50}
[4747]51
[4748]52//! the first class that is registered
[4747]53ClassList*  ClassList::first = NULL;
[4748]54
55//! the Count of classes
[4747]56unsigned int ClassList::classCount = 0;
57
[4748]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 */
[4747]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
[4752]87  regClass->objectList->add(objectPointer);
[4747]88}
89
[4748]90/**
91 * removes an Object from a the ClassList
92 * @param objectPointer the Object to delete from the List
93 */
[4747]94void ClassList::removeFromClassList(BaseObject* objectPointer)
95{
96  ClassList* tmp = ClassList::first;
97  while (likely(tmp != NULL))
98  {
99    if (objectPointer->isA(tmp->classID))
100    {
[4752]101      tmp->objectList->remove(objectPointer);
[4747]102    }
103
104    tmp = tmp->next;
105  }
106}
107
[4748]108/**
[4754]109 * checks if the BaseObject* object exists.
[4761]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    {
[4778]124      if (tmp->classID == classID || classID == CL_NULL)
[4761]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.
[4754]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    {
[4778]165      if (tmp->classID == classID || classID == CL_NULL)
[4754]166      {
167        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
168        BaseObject* enumBO = iterator->nextElement();
[4761]169        while (enumBO != NULL)
[4754]170        {
171          if (enumBO == object)
[4761]172          {
173            delete iterator;
[4754]174            return true;
[4761]175          }
[4754]176          enumBO = iterator->nextElement();
177        }
178        delete iterator;
179        break;
180      }
181      tmp = tmp->next;
182    }
183  }
184  return false;
185}
186
187/**
[4748]188 * Print out some very nice debug information
189 */
[4752]190    void ClassList::debug(unsigned int debugLevel)
[4747]191{
[4752]192  if (debugLevel > 2)
193    debugLevel = 2;
194  PRINT(0)("==========================\n");
195  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
196  PRINT(0)("==========================\n");
[4760]197  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
[4747]198  ClassList* tmp = ClassList::first;
[4748]199  char niceString[100];
200  unsigned int lenCount = 0;
201
[4747]202  while (likely(tmp != NULL))
203  {
[4752]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';
[4747]212
[4758]213      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
[4748]214
[4752]215      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
216      {
[4758]217        PRINT(0)("|  Listing Instances:\n");
[4752]218        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
219        BaseObject* enumBO = iterator->nextElement();
220        while (enumBO)
221        {
[4758]222          PRINT(0)("|   (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO);
[4752]223          enumBO = iterator->nextElement();
224        }
225        delete iterator;
226      }
227    }
[4747]228    tmp = tmp->next;
229  }
[4758]230  PRINT(0)("=======================CL=\n");
[4747]231}
Note: See TracBrowser for help on using the repository browser.