Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setting the resolution works again (workaround. now i fix the subprojects again.)

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  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
108    tmp = tmp->next;
109  }
110}
111
112/**
113 * checks if the BaseObject* object exists.
114 * @param name the name of the BaseObject to look for
115 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
116 * @return true, if the Object Exists in the specified ClassID, false otherwise
117 * @todo: speed this up!!
118 */
119BaseObject* ClassList::getObject(const char* name, long classID)
120{
121  if(unlikely(ClassList::first == NULL) || name == NULL)
122    return NULL;
123  else
124  {
125    ClassList* tmp = ClassList::first;
126    while (likely(tmp != NULL))
127    {
128      if (tmp->classID == classID || classID == CL_NULL)
129      {
130        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
131        BaseObject* enumBO = iterator->nextElement();
132        const char* tmpName;
133        while (enumBO != NULL)
134        {
135          tmpName = enumBO->getName();
136          if (tmpName && !strcmp(tmpName, name))
137          {
138            delete iterator;
139            return enumBO;
140          }
141          enumBO = iterator->nextElement();
142        }
143        delete iterator;
144        break;
145      }
146      tmp = tmp->next;
147    }
148  }
149  return NULL;
150}
151
152
153/**
154 * checks if the BaseObject* object exists.
155 * @param object the Pointer to a BaseObject to check if it exists
156 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere.
157 * @return true, if the Object Exists in the specified ClassID, false otherwise
158 * @todo: speed this up!!
159 */
160bool ClassList::exists(BaseObject* object, long classID)
161{
162  if(unlikely(ClassList::first == NULL))
163    return false;
164  else
165  {
166    ClassList* tmp = ClassList::first;
167    while (likely(tmp != NULL))
168    {
169      if (tmp->classID == classID || classID == CL_NULL)
170      {
171        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
172        BaseObject* enumBO = iterator->nextElement();
173        while (enumBO != NULL)
174        {
175          if (enumBO == object)
176          {
177            delete iterator;
178            return true;
179          }
180          enumBO = iterator->nextElement();
181        }
182        delete iterator;
183        break;
184      }
185      tmp = tmp->next;
186    }
187  }
188  return false;
189}
190
191/**
192 * Print out some very nice debug information
193 */
194    void ClassList::debug(unsigned int debugLevel)
195{
196  if (debugLevel > 2)
197    debugLevel = 2;
198  PRINT(0)("==========================\n");
199  PRINT(0)("=  CLASS_LIST (level %d)  =\n", debugLevel);
200  PRINT(0)("==========================\n");
201  PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount);
202  ClassList* tmp = ClassList::first;
203  char niceString[100];
204  unsigned int lenCount = 0;
205
206  while (likely(tmp != NULL))
207  {
208    if (debugLevel >= 1 || tmp->objectList->getSize() > 0)
209    {
210      lenCount = 1;
211      while (pow(10,lenCount) <= tmp->objectList->getSize())
212        ++lenCount;
213      for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++)
214        (niceString[i]) = ' ';
215      niceString[30-strlen(tmp->className) - lenCount] = '\0';
216
217      PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize());
218
219      if (debugLevel >=2 && tmp->objectList->getSize() > 0)
220      {
221        PRINT(0)("|  Listing Instances:\n");
222        tIterator<BaseObject>* iterator = tmp->objectList->getIterator();
223        BaseObject* enumBO = iterator->nextElement();
224        while (enumBO)
225        {
226          PRINT(0)("|   (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO);
227          enumBO = iterator->nextElement();
228        }
229        delete iterator;
230      }
231    }
232    tmp = tmp->next;
233  }
234  PRINT(0)("=======================CL=\n");
235}
Note: See TracBrowser for help on using the repository browser.