Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/base_object.cc @ 7174

Last change on this file since 7174 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
RevLine 
[3302]1
2
[4591]3/*
[3302]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
[6640]15   co-programmer: Benjamin Grauer
[3302]16*/
[5439]17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_BASE
[3302]18
19#include "base_object.h"
[4747]20
[4436]21#include "load_param.h"
[4595]22#include "compiler.h"
[4747]23#include "class_list.h"
[3302]24
[6341]25#include "synchronizeable.h"
26
[3302]27using namespace std;
28
29
30/**
[6280]31 * @brief sets the name from a LoadXML-Element
[4836]32 * @param root the element to load from
[5439]33 */
[6517]34BaseObject::BaseObject()
[3531]35{
[6276]36  this->className = "BaseObject";
[4591]37  this->classID = CL_BASE_OBJECT;
[4435]38
39  this->objectName = NULL;
[6280]40  this->classList = NULL;
[6517]41  this->xmlElem = NULL;
[4436]42
[4778]43//  ClassList::addToClassList(this, this->classID, "BaseObject");
[3531]44}
[3302]45
46/**
[6280]47 * @brief standard deconstructor
[3302]48*/
[4591]49BaseObject::~BaseObject ()
[3531]50{
[4747]51  ClassList::removeFromClassList(this);
52
[4261]53  //  delete []this->className;
[4435]54  if (this->objectName)
[5447]55    delete[] this->objectName;
[6517]56  if (this->xmlElem != NULL)
57    delete this->xmlElem;
[5447]58}
[3302]59
[4436]60/**
[6280]61 * @brief loads parameters
[4836]62 * @param root the element to load from
[5439]63 */
[4436]64void BaseObject::loadParams(const TiXmlElement* root)
65{
[6517]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();
[4436]72  // name setup
[5671]73  LoadParam(root, "name", this, BaseObject, setName)
[5645]74      .describe("the Name of the Object.");
[4436]75}
[4321]76
77/**
[6280]78 * @brief sets the class identifiers
[4836]79 * @param id a number for the class from class_id.h enumeration
80 * @param className the class name
[4321]81*/
[5791]82void BaseObject::setClassID(ClassID classID, const char* className)
[4320]83{
[6282]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 ));
[6276]86
[5791]87  this->classID |= (long)classID;
[4320]88  this->className = className;
[4747]89
[6280]90  this->classList = ClassList::addToClassList(this, classID, this->classID, className);
[4320]91}
[4318]92
[6280]93
[4435]94/**
[6280]95 * @brief set the name of the Object
[4591]96 */
[4742]97void BaseObject::setName (const char* objectName)
[4435]98{
99  if (this->objectName)
[5113]100    delete[] this->objectName;
[5645]101  if (objectName != NULL)
[4592]102  {
103    this->objectName = new char[strlen(objectName)+1];
104    strcpy(this->objectName, objectName);
105  }
[4591]106  else
[4435]107    this->objectName = NULL;
108}
[4592]109
[6281]110
[6280]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}
[4592]123
[6280]124
125
[4592]126/**
[6280]127 * @brief checks if the class is a classID
[4836]128 * @param classID the Identifier to check for
129 * @returns true if it is, false otherwise
[4592]130*/
[6077]131bool BaseObject::isA (ClassID classID) const
[4592]132{
[4837]133  // if classID is a derivable object from a SUPERCLASS
134  if (classID & CL_MASK_SUPER_CLASS)
[4594]135  {
[4837]136    if( likely(this->classID & classID))
[4594]137      return true;
[4837]138  }
139  // if classID is a SubSuperClass, and
140  else if (classID & CL_MASK_SUBSUPER_CLASS)
141  {
[7123]142    if (likely(((this->classID & CL_MASK_SUBSUPER_CLASS_IDA) == (classID & CL_MASK_SUBSUPER_CLASS_IDA)) &&
[5915]143        this->classID & classID & CL_MASK_SUBSUPER_CLASS_IDB))
[4837]144      return true;
145  }
146  // if classID is a LOWLEVEL-class
[4594]147  else
148  {
[4837]149    if( likely((this->classID & CL_MASK_LOWLEVEL_CLASS) == classID))
[4594]150      return true;
151  }
[4592]152  return false;
153}
154
[6280]155
156
[4592]157/**
[6280]158 * @brief checks if the class is a classID
[5513]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{
[6077]164  ClassID classID = ClassList::StringToID(className);
[5513]165  if (classID != CL_NULL)
166    return this->isA(classID);
167}
168
[6280]169
[5626]170/**
[6280]171 * @brief compares the ObjectName with an external name
[5626]172 * @param objectName: the name to check.
173 * @returns true on match, false otherwise.
174 */
[5791]175bool BaseObject::operator==(const char* objectName)
[5626]176{
177  if (likely(this->objectName != NULL && objectName != NULL))
178    return (strcmp(this->objectName, objectName)) ? false : true;
179}
[5513]180
[5626]181
[5513]182/**
[6280]183 * @brief displays everything this class is
[5642]184 * @TODO REIMPLEMENT WITH SENSE.
[4592]185 */
[4746]186void BaseObject::whatIs() const
[4592]187{
[6280]188
[4592]189}
[6341]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  }
[6815]206  SYNCHELP_READ_STRINGM( this->objectName, NWT_BO_NAME );
[6498]207  if ( this->objectName && !strcmp(this->objectName, "") )
208  {
209    delete[] this->objectName;
210    this->objectName = NULL;
211  }
[6341]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
[6634]226  //PRINTF(0)("objectname = %s\n", this->objectName);
[6815]227  SYNCHELP_WRITE_STRING( this->objectName, NWT_BO_NAME );
[6341]228
229  return SYNCHELP_WRITE_N;
230}
Note: See TracBrowser for help on using the repository browser.