Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: move the Pollution of the ShellCommand to some other File

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