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