Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: performance update.

File size: 10.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 "compiler.h"
22#include "debug.h"
23#include <string.h>
24#include <math.h>
25#include <algorithm>
26#include "shell_command.h"
27
28using namespace std;
29
30SHELL_COMMAND_STATIC(debug, ClassList, ClassList::debugS)
31    ->describe("Shows all registered classes, if param1: is a valid ClassName only values of this class are shown. param2: how much output")
32    ->defaultValues(2, NULL, 1);
33
34/**
35 *  Creates a new ClassList
36*/
37ClassList::ClassList(const long& classID, const char* className)
38{
39  this->className = className;
40  this->classID = classID;
41}
42
43/**
44 *  standard deconstructor
45*/
46ClassList::~ClassList ()
47{
48//   ClassList::classList->clear());
49}
50
51//! a List of all known Classes.
52std::list<ClassList>* ClassList::classList = NULL;
53
54//! a List of all strings of all classes, that have registered so far.
55std::list<const char*> ClassList::classNames;
56
57/**
58 * Adds a new Object to the ClassList (and if necessary a new Class)
59 * @param objectPointer Pointer to the Object at hand
60 * @param classID ID of the Given ObjectType \see ClassID
61 * @param className name of the Class to add
62 *
63 * !! FIRST YOU HAVE TO CALL THIS FUNCTION ONCE
64 * !! Before unsing the ClassList, as it creates the ClassLits
65 */
66void ClassList::addToClassList(BaseObject* objectPointer, ClassID classID, const char* className)
67{
68  if (unlikely(classList == NULL))
69    ClassList::classList = new list<ClassList>();
70
71  PRINTF(5)("subscribe a '%s'\n", className );
72
73  ClassList* regClass = ClassList::getClassList(classID);
74  if (regClass != NULL)
75    regClass->objectList.push_back(objectPointer);
76  else
77  {
78    ClassList::classList->push_back(ClassList(classID, className));
79    ClassList::classList->back().objectList.push_back(objectPointer);
80  }
81}
82
83/**
84 * removes an Object from a the ClassList
85 * @param objectPointer the Object to delete from the List
86 */
87void ClassList::removeFromClassList(BaseObject* objectPointer)
88{
89  list<ClassList>::iterator cl;
90  for(cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
91  {
92    if (objectPointer->isA((*cl).classID))
93    {
94      std::list<BaseObject*>::iterator bo;
95      for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++)
96      {
97        if ((*bo) == objectPointer)
98        {
99          (*cl).objectList.erase(bo);
100          break;
101        }
102      }
103    }
104  }
105}
106
107/**
108 * grabs the names of all Classes, and injects it into a List of const chars
109 * @return the generated List
110 *
111 * This function first looks, if the List has been changed (by the ListSize)
112 * befor it changes anything.
113 */
114const std::list<const char*>* ClassList::getClassNames()
115{
116  if (ClassList::classNames.size() != ClassList::classList->size())
117  {
118      ClassList::classNames.clear();
119
120      list<ClassList>::const_iterator cl;
121      for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
122        ClassList::classNames.push_back((*cl).className);
123  }
124
125  return &ClassList::classNames;
126}
127
128/**
129 * searches for classID and returns the list of Entities
130 * @param classID the ID of the class to get the list from
131 * @return the List accessed by classID, or NULL if not found
132 */
133std::list<BaseObject*>* ClassList::getList(ClassID classID)
134{
135  ClassList* fl;
136  return ((fl = ClassList::getClassList(classID)) != NULL)?
137       &(fl->objectList) : NULL;
138
139/*
140  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), classID);
141  return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/
142
143/*  for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++)
144  {
145    if ((*classIT) == classID )
146      return &(*classIT).objectList;
147  }
148  return NULL;*/
149}
150
151/**
152 * searches for className and returns the list of Entities
153 * @param className the name of the class to get the list from
154 * @return the List accessed by classID, or NULL if not found
155 */
156std::list<BaseObject*>* ClassList::getList(const char* className)
157{
158  ClassList* fl;
159  return ((fl = ClassList::getClassList(className)) != NULL)?
160      &(fl->objectList) : NULL;
161
162  /*
163  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className);
164  return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/
165
166
167/*  for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++)
168  {
169    if ((*classIT) == className )
170      return &(*classIT).objectList;
171  }
172  return NULL;*/
173}
174
175
176ClassList* ClassList::getClassList(ClassID classID)
177{
178  std::list<ClassList>::iterator classIT = find (ClassList::classList->begin(), ClassList::classList->end(), classID);
179  return (likely(classIT != classList->end()))? &(*classIT) : NULL;
180}
181
182
183ClassList* ClassList::getClassList(const char* className)
184{
185  if (className == NULL)
186    return NULL;
187  std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className);
188  return (likely(classIT != classList->end()))? &(*classIT) : NULL;
189}
190
191
192/**
193 * checks if the BaseObject* object exists.
194 * @param objectName the name of the BaseObject to look for
195 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
196 * @return true, if the Object Exists in the specified ClassID, false otherwise
197 * @todo: speed this up!!
198 */
199BaseObject* ClassList::getObject(const char* objectName, ClassID classID)
200{
201  if (classID != CL_NULL)
202  {
203    ClassList* cl = ClassList::getClassList(classID);
204    if (cl != NULL)
205    {
206      std::list<BaseObject*>::iterator bo;
207      for (bo = cl->objectList.begin(); bo != cl->objectList.end(); bo++)
208        if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName))
209          return (*bo);
210    }
211  }
212  else
213  {
214    list<ClassList>::iterator cl;
215    for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
216    {
217      std::list<BaseObject*>::iterator bo;
218      for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++)
219        if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName))
220          return (*bo);
221    }
222  }
223  return NULL;
224}
225
226
227/**
228 * checks if the BaseObject* object exists.
229 * @param object the Pointer to a BaseObject to check if it exists
230 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
231 * @return true, if the Object Exists in the specified ClassID, false otherwise
232 * @todo: speed this up!!
233 */
234bool ClassList::exists(const BaseObject* object, ClassID classID)
235{
236  if (classID != CL_NULL)
237  {
238    ClassList* cl = ClassList::getClassList(classID);
239    if (cl != NULL)
240    {
241      std::list<BaseObject*>::const_iterator bo = find (cl->objectList.begin(), cl->objectList.end(), object);
242      return (bo != cl->objectList.end());
243    }
244  }
245  else
246  {
247    list<ClassList>::iterator cl;
248    for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
249    {
250      std::list<BaseObject*>::const_iterator bo = find ((*cl).objectList.begin(), (*cl).objectList.end(), object);
251      if (bo != (*cl).objectList.end())
252        return true;
253    }
254  }
255  return false;
256}
257
258/**
259 * prints out a string of all the types this Object matches
260 * @param object a Pointer to the object to analyze
261 */
262void ClassList::whatIs(const BaseObject* object)
263{
264  list<ClassList>::iterator cl;
265  for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
266    if (object->isA((*cl).classID))
267  {
268    PRINT(0)("=%s=-", (*cl).className);
269  }
270}
271
272/**
273 * converts a ClassID into a string
274 * @param classID the ClassID to search for
275 * @return a String containing the name of the Class, NULL if the Class was not found
276 */
277const char* ClassList::IDToString(ClassID classID)
278{
279  ClassList* cl = ClassList::getClassList(classID);
280  return (cl != NULL) ? cl->className : NULL;
281}
282
283/**
284 * converts a String into a ClassID
285 * @param className the name of the class to search for
286 * @return the ClassID. CL_NULL, if the class was not found.
287 */
288long ClassList::StringToID(const char* className)
289{
290  ClassList* cl = ClassList::getClassList(className);
291  return (cl != NULL) ? cl->classID : CL_NULL;
292}
293
294/**
295 * checks if this ClassList is named className
296 * @param className the Name to check this ClassList's ClassName against
297 * @returns true on match, false otherwise
298 */
299bool ClassList::operator==(const char* className)
300{
301  if (likely( className != NULL && this->className != NULL))
302    return (!strcmp(this->className, className));
303  else
304    return false;
305}
306
307
308
309/**
310 * Print out some very nice debug information
311 * @param debugLevel the level of verbosity
312 * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed)
313 */
314void ClassList::debug(unsigned int debugLevel, long classID)
315{
316  if (debugLevel > 3)
317    debugLevel = 3;
318  PRINT(0)("==========================\n");
319  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
320  PRINT(0)("==========================\n");
321  PRINT(0)("| knows %d Classes\n|\n", ClassList::classList->size());
322  char niceString[100];
323  unsigned int lenCount = 0;
324
325  list<ClassList>::iterator cl;
326  for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
327  {
328    if ((debugLevel >= 1 || (*cl).objectList.size() > 0 ) &&
329         (classID == CL_NULL || unlikely (classID == (*cl).classID)))
330    {
331      lenCount = 1;
332      while (pow(10, lenCount) <= (*cl).objectList.size())
333        ++lenCount;
334      for (int i=0; i < 30-strlen((*cl).className) - lenCount; i++)
335        (niceString[i]) = ' ';
336      niceString[30-strlen((*cl).className) - lenCount] = '\0';
337
338      PRINT(0)("| CLASS %s:%s %d\n", (*cl).className, niceString, (*cl).objectList.size());
339
340      if (debugLevel >=2 && (*cl).objectList.size() > 0)
341      {
342        PRINT(0)("|  Listing Instances:\n");
343        list<BaseObject*>::const_iterator bo;
344        for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++)
345        {
346          PRINT(0)("|   (class %s): NAME(%s)->%p ", (*bo)->getClassName(), (*bo)->getName(), (*bo));
347          if (debugLevel == 3)
348            ClassList::whatIs(*bo);
349          PRINT(0)("\n");
350        }
351      }
352    }
353  }
354  PRINT(0)("=======================CL=\n");
355}
356
357/**
358 * Print out some very nice debug information
359 * @param debugLevel the level of verbosity
360 * @param className the class that should be displayed.
361 * @see ClassList::debug
362 */
363void ClassList::debugS(const char* className, unsigned int debugLevel)
364{
365  ClassList::debug(debugLevel, ClassList::StringToID(className));
366}
Note: See TracBrowser for help on using the repository browser.