Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merge

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