Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/control/src/lib/lang/base_object.cc @ 7352

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

orxonox/trunk: some cleanup before the new tag

  1. BaseObject isA was wrong… every subclass was detected as any other subclass
  2. Classes get unloaded again as they should
  3. some minor fixes (Material* → Material) and some other cleanups
File size: 5.3 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: Benjamin Grauer
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_BASE
18
19#include "base_object.h"
20
21#include "load_param.h"
22#include "compiler.h"
23#include "class_list.h"
24
25#include "synchronizeable.h"
26
27using namespace std;
28
29
30/**
31 * @brief sets the name from a LoadXML-Element
32 * @param root the element to load from
33 */
34BaseObject::BaseObject()
35{
36  this->className = "BaseObject";
37  this->classID = CL_BASE_OBJECT;
38
39  this->objectName = NULL;
40  this->classList = NULL;
41  this->xmlElem = NULL;
42
43//  ClassList::addToClassList(this, this->classID, "BaseObject");
44}
45
46/**
47 * @brief standard deconstructor
48*/
49BaseObject::~BaseObject ()
50{
51  ClassList::removeFromClassList(this);
52
53  //  delete []this->className;
54  if (this->objectName)
55    delete[] this->objectName;
56  if (this->xmlElem != NULL)
57    delete this->xmlElem;
58}
59
60/**
61 * @brief loads parameters
62 * @param root the element to load from
63 */
64void BaseObject::loadParams(const TiXmlElement* root)
65{
66  // all loadParams should sometime arrive here.
67  assert (root != NULL);
68
69  if (this->xmlElem != NULL)
70    delete this->xmlElem;
71  this->xmlElem = root->Clone();
72  // name setup
73  LoadParam(root, "name", this, BaseObject, setName)
74      .describe("the Name of the Object.");
75}
76
77/**
78 * @brief sets the class identifiers
79 * @param id a number for the class from class_id.h enumeration
80 * @param className the class name
81*/
82void BaseObject::setClassID(ClassID classID, const char* className)
83{
84  //printf("%s(0x%.8X)->%s(0x%.8X)\n", this->className, this->classID, className, classID);
85  assert (!(this->classID & classID & !CL_MASK_SUBSUPER_CLASS_IDA ));
86
87  this->classID |= (long)classID;
88  this->className = className;
89
90  this->classList = ClassList::addToClassList(this, classID, this->classID, className);
91}
92
93
94/**
95 * @brief set the name of the Object
96 */
97void BaseObject::setName (const char* objectName)
98{
99  if (this->objectName)
100    delete[] this->objectName;
101  if (objectName != NULL)
102  {
103    this->objectName = new char[strlen(objectName)+1];
104    strcpy(this->objectName, objectName);
105  }
106  else
107    this->objectName = NULL;
108}
109
110
111/**
112 * @brief queries for the ClassID of the Leaf Class (the last made class of this type
113 * @returns the ClassID of the Leaf Class (e.g. the ID of the Class)
114 *
115 * the returned ID can be used to generate new Objects of the same type through
116 * Factory::fabricate(Object->getLeafClassID());
117 */
118ClassID BaseObject::getLeafClassID() const
119{
120  assert (this->classList != NULL);
121  return this->classList->getLeafClassID();
122}
123
124
125
126/**
127 * @brief checks if the class is a classID
128 * @param classID the Identifier to check for
129 * @returns true if it is, false otherwise
130*/
131bool BaseObject::isA (ClassID classID) const
132{
133  // if classID is a derivable object from a SUPERCLASS
134  if (classID & CL_MASK_SUPER_CLASS)
135  {
136    if( likely(this->classID & classID))
137      return true;
138  }
139  // if classID is a SubSuperClass, and
140  else if (classID & CL_MASK_SUBSUPER_CLASS)
141  {
142    if (likely(((this->classID & CL_MASK_SUBSUPER_CLASS_IDA) == (classID & CL_MASK_SUBSUPER_CLASS_IDA)) &&
143        this->classID & classID & CL_MASK_SUBSUPER_CLASS_IDB))
144      return true;
145  }
146  // if classID is a LOWLEVEL-class
147  else
148  {
149    if( likely((this->classID & CL_MASK_LOWLEVEL_CLASS) == classID))
150      return true;
151  }
152  return false;
153}
154
155
156
157/**
158 * @brief checks if the class is a classID
159 * @param classID the Identifier to check for
160 * @returns true if it is, false otherwise
161 */
162bool BaseObject::isA (const char* className) const
163{
164  ClassID classID = ClassList::StringToID(className);
165  if (classID != CL_NULL)
166    return this->isA(classID);
167}
168
169
170/**
171 * @brief compares the ObjectName with an external name
172 * @param objectName: the name to check.
173 * @returns true on match, false otherwise.
174 */
175bool BaseObject::operator==(const char* objectName)
176{
177  if (likely(this->objectName != NULL && objectName != NULL))
178    return (strcmp(this->objectName, objectName)) ? false : true;
179}
180
181
182/**
183 * @brief displays everything this class is
184 * @TODO REIMPLEMENT WITH SENSE.
185 */
186void BaseObject::whatIs() const
187{
188
189}
190
191/**
192 * Writes data from network containing information about the state
193 * @param data pointer to data
194 * @param length length of data
195 * @param sender hostID of sender
196 */
197int BaseObject::writeState( const byte * data, int length, int sender )
198{
199  SYNCHELP_READ_BEGIN();
200
201  if ( objectName )
202  {
203    delete[] objectName;
204    objectName = NULL;
205  }
206  SYNCHELP_READ_STRINGM( this->objectName, NWT_BO_NAME );
207  if ( this->objectName && !strcmp(this->objectName, "") )
208  {
209    delete[] this->objectName;
210    this->objectName = NULL;
211  }
212
213  return SYNCHELP_READ_N;
214}
215
216/**
217 * data copied in data will bee sent to another host
218 * @param data pointer to data
219 * @param maxLength max length of data
220 * @return the number of bytes writen
221 */
222int BaseObject::readState( byte * data, int maxLength )
223{
224  SYNCHELP_WRITE_BEGIN();
225
226  //PRINTF(0)("objectname = %s\n", this->objectName);
227  SYNCHELP_WRITE_STRING( this->objectName, NWT_BO_NAME );
228
229  return SYNCHELP_WRITE_N;
230}
Note: See TracBrowser for help on using the repository browser.