Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: whatIs implemented in ClassList is better than in BaseObject

File size: 6.9 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 "list.h"
22#include "compiler.h"
23#include "debug.h"
24#include <string.h>
25#include <math.h>
26
27using namespace std;
28
29
30/**
31 *  Creates a new ClassList
32*/
33ClassList::ClassList(const long& classID, const char* className)
34{
35  this->next = NULL;
36  this->className = className;
37  this->classID = classID;
38  this->objectList = new tList<BaseObject>;
39
40  ++ClassList::classCount;
41}
42
43
44/**
45 *  standard deconstructor
46*/
47ClassList::~ClassList ()
48{
49  delete this->objectList;
50  --ClassList::classCount;
51}
52
53//! the first class that is registered
54ClassList*  ClassList::first = NULL;
55
56//! the Count of classes
57unsigned int ClassList::classCount = 0;
58
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 */
65void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className)
66{
67  ClassList* regClass;
68  PRINTF(5)("subscribe a %s\n", className );
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
83      if (unlikely(tmp->next == NULL))
84      {
85        tmp->next = regClass = new ClassList(classID, className);
86        break;
87      }
88      tmp = tmp->next;
89    }
90  }
91  regClass->objectList->add(objectPointer);
92}
93
94/**
95 * removes an Object from a the ClassList
96 * @param objectPointer the Object to delete from the List
97 */
98void ClassList::removeFromClassList(BaseObject* objectPointer)
99{
100  ClassList* tmp = ClassList::first;
101  while (likely(tmp != NULL))
102  {
103    if (objectPointer->isA(tmp->classID))
104    {
105      tmp->objectList->remove(objectPointer);
106    }
107    tmp = tmp->next;
108  }
109}
110
111tList<BaseObject>* ClassList::getList(long classID)
112{
113  if(unlikely(ClassList::first == NULL))
114    return NULL;
115  else
116  {
117    ClassList* tmpCL = ClassList::first;
118    while (likely(tmpCL != NULL))
119    {
120      if (unlikely(tmpCL->classID == classID))
121        return tmpCL->objectList;
122      tmpCL = tmpCL->next;
123    }
124  }
125  return NULL;
126}
127
128/**
129 * checks if the BaseObject* object exists.
130 * @param name the name of the BaseObject to look for
131 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
132 * @return true, if the Object Exists in the specified ClassID, false otherwise
133 * @todo: speed this up!!
134 */
135BaseObject* ClassList::getObject(const char* name, long classID)
136{
137  if(unlikely(ClassList::first == NULL) || name == NULL)
138    return NULL;
139  else
140  {
141    ClassList* tmp = ClassList::first;
142    while (likely(tmp != NULL))
143    {
144      if (tmp->classID == classID || classID == CL_NULL)
145      {
146        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
147        BaseObject* enumBO = iterator->nextElement();
148        const char* tmpName;
149        while (enumBO != NULL)
150        {
151          tmpName = enumBO->getName();
152          if (tmpName && !strcmp(tmpName, name))
153          {
154            delete iterator;
155            return enumBO;
156          }
157          enumBO = iterator->nextElement();
158        }
159        delete iterator;
160        break;
161      }
162      tmp = tmp->next;
163    }
164  }
165  return NULL;
166}
167
168
169/**
170 * checks if the BaseObject* object exists.
171 * @param object the Pointer to a BaseObject to check if it exists
172 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
173 * @return true, if the Object Exists in the specified ClassID, false otherwise
174 * @todo: speed this up!!
175 */
176bool ClassList::exists(const BaseObject* object, long classID)
177{
178  if(unlikely(ClassList::first == NULL))
179    return false;
180  else
181  {
182    ClassList* tmp = ClassList::first;
183    while (likely(tmp != NULL))
184    {
185      if (tmp->classID == classID || classID == CL_NULL)
186      {
187        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
188        BaseObject* enumBO = iterator->nextElement();
189        while (enumBO != NULL)
190        {
191          if (enumBO == object)
192          {
193            delete iterator;
194            return true;
195          }
196          enumBO = iterator->nextElement();
197        }
198        delete iterator;
199        break;
200      }
201      tmp = tmp->next;
202    }
203  }
204  return false;
205}
206
207
208/**
209 * prints out a string of all the types this Object matches
210 * @param object a Pointer to the object to analyze
211 */
212void ClassList::whatIs(const BaseObject* object)
213{
214  ClassList* tmp = ClassList::first;
215  while (likely(tmp != NULL))
216  {
217    if (object->isA(tmp->classID))
218    {
219      PRINT(0)("=%s=-", tmp->className);
220    }
221    tmp = tmp->next;
222  }
223}
224
225/**
226 * Print out some very nice debug information
227 * @param debugLevel the level of verbosity
228 * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed)
229 */
230void ClassList::debug(unsigned int debugLevel, long classID)
231{
232  if (debugLevel > 3)
233    debugLevel = 3;
234  PRINT(0)("==========================\n");
235  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
236  PRINT(0)("==========================\n");
237  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
238  ClassList* tmp = ClassList::first;
239  char niceString[100];
240  unsigned int lenCount = 0;
241
242  while (likely(tmp != NULL))
243  {
244    if ((debugLevel >= 1 || tmp->objectList->getSize() > 0 ) &&
245         (classID == CL_NULL || unlikely (classID == tmp->classID)))
246    {
247      lenCount = 1;
248      while (pow(10,lenCount) <= tmp->objectList->getSize())
249        ++lenCount;
250      for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
251        (niceString[i]) = ' ';
252      niceString[30-strlen(tmp->className) - lenCount] = '\0';
253
254      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
255
256      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
257      {
258        PRINT(0)("|  Listing Instances:\n");
259        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
260        BaseObject* enumBO = iterator->nextElement();
261        while (enumBO)
262        {
263          PRINT(0)("|   (class %s): NAME(%s)->%p ", enumBO->getClassName(), enumBO->getName(), enumBO);
264          if (debugLevel == 3)
265            ClassList::whatIs(enumBO);
266          PRINT(0)("\n");
267          enumBO = iterator->nextElement();
268        }
269        delete iterator;
270      }
271    }
272    tmp = tmp->next;
273  }
274  PRINT(0)("=======================CL=\n");
275}
Note: See TracBrowser for help on using the repository browser.