Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4782 was 4782, checked in by bensch, 19 years ago

orxonox/trunk: orxonox-events registered, solved an error in ClassList

File size: 5.9 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
[4752]21#include "list.h"
[4747]22#include "compiler.h"
23#include "debug.h"
[4748]24#include <string.h>
25#include <math.h>
[4747]26
[4743]27using namespace std;
28
29
30/**
[4748]31   \brief Creates a new ClassList
[4743]32*/
[4747]33ClassList::ClassList(const long& classID, const char* className)
[4743]34{
[4747]35  this->next = NULL;
36  this->className = className;
37  this->classID = classID;
[4752]38  this->objectList = new tList<BaseObject>;
[4748]39
40  ++ClassList::classCount;
[4743]41}
42
43
44/**
45   \brief standard deconstructor
46*/
47ClassList::~ClassList ()
48{
[4782]49  delete this->objectList;
[4748]50  --ClassList::classCount;
[4743]51}
[4747]52
[4748]53//! the first class that is registered
[4747]54ClassList*  ClassList::first = NULL;
[4748]55
56//! the Count of classes
[4747]57unsigned int ClassList::classCount = 0;
58
[4748]59/**
60 * Adds a new Object to the ClassList (and if necessary a new Class)
61 * @param objectPointer Pointer to the Object at hand
62 * @param classID ID of the Given ObjectType \see ClassID
63 * @param className name of the Class to add
64 */
[4747]65void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className)
66{
67  ClassList* regClass;
[4782]68  PRINTF(5)("subscribe a %s\n", className );
[4747]69
70  if(ClassList::first == NULL)
71    ClassList::first = regClass = new ClassList(classID, className);
72  else
73  {
74    ClassList* tmp = ClassList::first;
75    while (likely(tmp != NULL))
76    {
77      if (tmp->classID == classID)
78      {
79        regClass = tmp;
80        break;
81      }
82
[4782]83      if (unlikely(tmp->next == NULL))
84      {
[4747]85        tmp->next = regClass = new ClassList(classID, className);
[4782]86        break;
87      }
[4747]88      tmp = tmp->next;
89    }
90  }
91
[4752]92  regClass->objectList->add(objectPointer);
[4747]93}
94
[4748]95/**
96 * removes an Object from a the ClassList
97 * @param objectPointer the Object to delete from the List
98 */
[4747]99void ClassList::removeFromClassList(BaseObject* objectPointer)
100{
101  ClassList* tmp = ClassList::first;
102  while (likely(tmp != NULL))
103  {
104    if (objectPointer->isA(tmp->classID))
105    {
[4752]106      tmp->objectList->remove(objectPointer);
[4747]107    }
108
109    tmp = tmp->next;
110  }
111}
112
[4748]113/**
[4754]114 * checks if the BaseObject* object exists.
[4761]115 * @param name the name of the BaseObject to look for
116 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
117 * @return true, if the Object Exists in the specified ClassID, false otherwise
118 * @todo: speed this up!!
119 */
120BaseObject* ClassList::getObject(const char* name, long classID)
121{
122  if(unlikely(ClassList::first == NULL) || name == NULL)
123    return NULL;
124  else
125  {
126    ClassList* tmp = ClassList::first;
127    while (likely(tmp != NULL))
128    {
[4778]129      if (tmp->classID == classID || classID == CL_NULL)
[4761]130      {
131        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
132        BaseObject* enumBO = iterator->nextElement();
133        const char* tmpName;
134        while (enumBO != NULL)
135        {
136          tmpName = enumBO->getName();
137          if (tmpName && !strcmp(tmpName, name))
138          {
139            delete iterator;
140            return enumBO;
141          }
142          enumBO = iterator->nextElement();
143        }
144        delete iterator;
145        break;
146      }
147      tmp = tmp->next;
148    }
149  }
150  return NULL;
151}
152
153
154/**
155 * checks if the BaseObject* object exists.
[4754]156 * @param object the Pointer to a BaseObject to check if it exists
157 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
158 * @return true, if the Object Exists in the specified ClassID, false otherwise
159 * @todo: speed this up!!
160 */
161bool ClassList::exists(BaseObject* object, long classID)
162{
163  if(unlikely(ClassList::first == NULL))
164    return false;
165  else
166  {
167    ClassList* tmp = ClassList::first;
168    while (likely(tmp != NULL))
169    {
[4778]170      if (tmp->classID == classID || classID == CL_NULL)
[4754]171      {
172        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
173        BaseObject* enumBO = iterator->nextElement();
[4761]174        while (enumBO != NULL)
[4754]175        {
176          if (enumBO == object)
[4761]177          {
178            delete iterator;
[4754]179            return true;
[4761]180          }
[4754]181          enumBO = iterator->nextElement();
182        }
183        delete iterator;
184        break;
185      }
186      tmp = tmp->next;
187    }
188  }
189  return false;
190}
191
192/**
[4748]193 * Print out some very nice debug information
194 */
[4752]195    void ClassList::debug(unsigned int debugLevel)
[4747]196{
[4752]197  if (debugLevel > 2)
198    debugLevel = 2;
199  PRINT(0)("==========================\n");
200  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
201  PRINT(0)("==========================\n");
[4760]202  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
[4747]203  ClassList* tmp = ClassList::first;
[4748]204  char niceString[100];
205  unsigned int lenCount = 0;
206
[4747]207  while (likely(tmp != NULL))
208  {
[4752]209    if (debugLevel >= 1 || tmp->objectList->getSize() > 0)
210    {
211      lenCount = 1;
212      while (pow(10,lenCount) <= tmp->objectList->getSize())
213        ++lenCount;
214      for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
215        (niceString[i]) = ' ';
216      niceString[30-strlen(tmp->className) - lenCount] = '\0';
[4747]217
[4758]218      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
[4748]219
[4752]220      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
221      {
[4758]222        PRINT(0)("|  Listing Instances:\n");
[4752]223        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
224        BaseObject* enumBO = iterator->nextElement();
225        while (enumBO)
226        {
[4758]227          PRINT(0)("|   (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO);
[4752]228          enumBO = iterator->nextElement();
229        }
230        delete iterator;
231      }
232    }
[4747]233    tmp = tmp->next;
234  }
[4758]235  PRINT(0)("=======================CL=\n");
[4747]236}
Note: See TracBrowser for help on using the repository browser.