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
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   \brief 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   \brief 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
92  regClass->objectList->add(objectPointer);
93}
94
95/**
96 * removes an Object from a the ClassList
97 * @param objectPointer the Object to delete from the List
98 */
99void ClassList::removeFromClassList(BaseObject* objectPointer)
100{
101  ClassList* tmp = ClassList::first;
102  while (likely(tmp != NULL))
103  {
104    if (objectPointer->isA(tmp->classID))
105    {
106      tmp->objectList->remove(objectPointer);
107    }
108
109    tmp = tmp->next;
110  }
111}
112
113/**
114 * checks if the BaseObject* object exists.
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    {
129      if (tmp->classID == classID || classID == CL_NULL)
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.
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    {
170      if (tmp->classID == classID || classID == CL_NULL)
171      {
172        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
173        BaseObject* enumBO = iterator->nextElement();
174        while (enumBO != NULL)
175        {
176          if (enumBO == object)
177          {
178            delete iterator;
179            return true;
180          }
181          enumBO = iterator->nextElement();
182        }
183        delete iterator;
184        break;
185      }
186      tmp = tmp->next;
187    }
188  }
189  return false;
190}
191
192/**
193 * Print out some very nice debug information
194 */
195    void ClassList::debug(unsigned int debugLevel)
196{
197  if (debugLevel > 2)
198    debugLevel = 2;
199  PRINT(0)("==========================\n");
200  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
201  PRINT(0)("==========================\n");
202  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
203  ClassList* tmp = ClassList::first;
204  char niceString[100];
205  unsigned int lenCount = 0;
206
207  while (likely(tmp != NULL))
208  {
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';
217
218      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
219
220      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
221      {
222        PRINT(0)("|  Listing Instances:\n");
223        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
224        BaseObject* enumBO = iterator->nextElement();
225        while (enumBO)
226        {
227          PRINT(0)("|   (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO);
228          enumBO = iterator->nextElement();
229        }
230        delete iterator;
231      }
232    }
233    tmp = tmp->next;
234  }
235  PRINT(0)("=======================CL=\n");
236}
Note: See TracBrowser for help on using the repository browser.