Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/lib/lang/class_list.cc @ 5957

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

branches/powerups: merged the trunk to branches/powerup
merged with commandsvn merge ../trunk/ powerups/ -r5848:HEAD
conflicts resolved in favor of the trunk

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