Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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