Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: class-autocompletion is nice now

File size: 7.7 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
111/**
112 * searches for classID and returns the list of Entities
113 * @param classID the ID of the class to get the list from
114 * @return the List accessed by classID, or NULL if not found
115 */
116tList<BaseObject>* ClassList::getList(long classID)
117{
118  if(unlikely(ClassList::first == NULL))
119    return NULL;
120  else
121  {
122    ClassList* tmpCL = ClassList::first;
123    while (likely(tmpCL != NULL))
124    {
125      if (unlikely(tmpCL->classID == classID))
126        return tmpCL->objectList;
127      tmpCL = tmpCL->next;
128    }
129  }
130  return NULL;
131}
132
133/**
134 * searches for className and returns the list of Entities
135 * @param className the name of the class to get the list from
136 * @return the List accessed by classID, or NULL if not found
137 */tList<BaseObject>* ClassList::getList(const char* className)
138{
139  if(unlikely(ClassList::first == NULL))
140    return NULL;
141  else
142  {
143    ClassList* tmpCL = ClassList::first;
144    while (likely(tmpCL != NULL))
145    {
146      if (unlikely(!strcmp(tmpCL->className, className)))
147        return tmpCL->objectList;
148      tmpCL = tmpCL->next;
149    }
150  }
151  return NULL;
152}
153
154/**
155 * checks if the BaseObject* object exists.
156 * @param name the name of the BaseObject to look for
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 */
161BaseObject* ClassList::getObject(const char* name, long classID)
162{
163  if(unlikely(ClassList::first == NULL) || name == NULL)
164    return NULL;
165  else
166  {
167    ClassList* tmp = ClassList::first;
168    while (likely(tmp != NULL))
169    {
170      if (tmp->classID == classID || classID == CL_NULL)
171      {
172        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
173        BaseObject* enumBO = iterator->nextElement();
174        const char* tmpName;
175        while (enumBO != NULL)
176        {
177          tmpName = enumBO->getName();
178          if (tmpName && !strcmp(tmpName, name))
179          {
180            delete iterator;
181            return enumBO;
182          }
183          enumBO = iterator->nextElement();
184        }
185        delete iterator;
186        break;
187      }
188      tmp = tmp->next;
189    }
190  }
191  return NULL;
192}
193
194
195/**
196 * checks if the BaseObject* object exists.
197 * @param object the Pointer to a BaseObject to check if it exists
198 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
199 * @return true, if the Object Exists in the specified ClassID, false otherwise
200 * @todo: speed this up!!
201 */
202bool ClassList::exists(const BaseObject* object, long classID)
203{
204  if(unlikely(ClassList::first == NULL))
205    return false;
206  else
207  {
208    ClassList* tmp = ClassList::first;
209    while (likely(tmp != NULL))
210    {
211      if (tmp->classID == classID || classID == CL_NULL)
212      {
213        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
214        BaseObject* enumBO = iterator->nextElement();
215        while (enumBO != NULL)
216        {
217          if (enumBO == object)
218          {
219            delete iterator;
220            return true;
221          }
222          enumBO = iterator->nextElement();
223        }
224        delete iterator;
225        break;
226      }
227      tmp = tmp->next;
228    }
229  }
230  return false;
231}
232
233/**
234 * prints out a string of all the types this Object matches
235 * @param object a Pointer to the object to analyze
236 */
237void ClassList::whatIs(const BaseObject* object)
238{
239  ClassList* tmp = ClassList::first;
240  while (likely(tmp != NULL))
241  {
242    if (object->isA(tmp->classID))
243    {
244      PRINT(0)("=%s=-", tmp->className);
245    }
246    tmp = tmp->next;
247  }
248}
249
250/**
251 * Print out some very nice debug information
252 * @param debugLevel the level of verbosity
253 * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed)
254 */
255void ClassList::debug(unsigned int debugLevel, long classID)
256{
257  if (debugLevel > 3)
258    debugLevel = 3;
259  PRINT(0)("==========================\n");
260  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
261  PRINT(0)("==========================\n");
262  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
263  ClassList* tmp = ClassList::first;
264  char niceString[100];
265  unsigned int lenCount = 0;
266
267  while (likely(tmp != NULL))
268  {
269    if ((debugLevel >= 1 || tmp->objectList->getSize() > 0 ) &&
270         (classID == CL_NULL || unlikely (classID == tmp->classID)))
271    {
272      lenCount = 1;
273      while (pow(10,lenCount) <= tmp->objectList->getSize())
274        ++lenCount;
275      for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
276        (niceString[i]) = ' ';
277      niceString[30-strlen(tmp->className) - lenCount] = '\0';
278
279      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
280
281      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
282      {
283        PRINT(0)("|  Listing Instances:\n");
284        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
285        BaseObject* enumBO = iterator->nextElement();
286        while (enumBO)
287        {
288          PRINT(0)("|   (class %s): NAME(%s)->%p ", enumBO->getClassName(), enumBO->getName(), enumBO);
289          if (debugLevel == 3)
290            ClassList::whatIs(enumBO);
291          PRINT(0)("\n");
292          enumBO = iterator->nextElement();
293        }
294        delete iterator;
295      }
296    }
297    tmp = tmp->next;
298  }
299  PRINT(0)("=======================CL=\n");
300}
Note: See TracBrowser for help on using the repository browser.