Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6278 was 6278, checked in by bensch, 18 years ago

trunk: christmas commit…. fixed baseObject

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