Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4815 was 4815, checked in by patrick, 19 years ago

orxonox/trunk: now some other singletons get deleted at the end of the game. there is some classid problem with the collision detection framework: the stuff gets deleted, but the names remain in the list. bensch: you will have to adjust the class ids of the cd so it works, no idea how i could do this :)

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