Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/multi_type.cc @ 8697

Last change on this file since 8697 was 8316, checked in by bensch, 18 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

File size: 11.0 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5643]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5536]18#include "multi_type.h"
[1853]19
[5659]20//#include "stdincl.h"
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
[7199]25#include <sstream>
[5659]26
27#ifdef DEBUG
28#include "debug.h"
29#endif
30
[1856]31using namespace std;
[1853]32
[5545]33/**
[7199]34 * @brief creates a multiType without any stored value at all.
[5545]35 */
[7197]36MultiType::MultiType(MT_Type type)
[5540]37{
[7197]38  this->type = type;
[7200]39  switch (this->type)
40  {
[8035]41    default:
[7221]42      this->value.Float = 0.0f;
43      break;
[8035]44    case MT_BOOL:
[7200]45      this->value.Bool = false;
46      break;
[8035]47    case MT_INT:
[7200]48      this->value.Int = 0;
49      break;
[8035]50    case MT_FLOAT:
[7200]51      this->value.Float = 0.0f;
52      break;
[8035]53    case MT_CHAR:
[7200]54      this->value.Char = '\0';
55      break;
[8035]56    case MT_STRING:
[7201]57      this->storedString = "";
58      break;
[7200]59  }
[5540]60}
[5545]61/**
[7199]62 * @brief creates a multiType out of a boolean
[5545]63 * @param value the Value of this MulitType
64 */
[5537]65MultiType::MultiType(bool value)
66{
[5540]67  this->setBool(value);
[5537]68}
69
[5545]70/**
[7199]71 * @brief creates a multiType out of an integer
[5545]72 * @param value the Value of this MulitType
73 */
[5536]74MultiType::MultiType(int value)
75{
[5540]76  this->setInt(value);
[5536]77}
[1856]78
[5545]79/**
[7199]80 * @brief creates a multiType out of a float (double)
[5545]81 * @param value the Value of this MulitType
82 */
[5539]83MultiType::MultiType(double value)
[3365]84{
[5540]85  this->setFloat(value);
[5536]86}
[4320]87
[5545]88/**
[7199]89 * @brief creates a multiType out of a char
[5545]90 * @param value the Value of this MulitType
91 */
[5536]92MultiType::MultiType(char value)
93{
[5540]94  this->setChar(value);
[3365]95}
[1853]96
[5545]97/**
[7199]98 * @brief creates a multiType out of a String
99 * @param value the Value of this MulitType
100 */
101MultiType::MultiType(const std::string& value)
102{
103  this->setString(value);
104}
105
106/**
107 * @brief constructs a new MultiType from another one (copy)
108 */
[5659]109MultiType::MultiType(const MultiType& multiType)
110{
111  *this = multiType;
112}
113
[3245]114/**
[7199]115 * @brief standard deconstructor
[3245]116*/
[5536]117MultiType::~MultiType ()
[7199]118{ }
[5537]119
[5540]120/**
[7199]121 * @brief copy operator
[5540]122 * @param mt: the entity to copy
[7199]123 * @returns A Copy of itself. (strings inside are copied as well)
[5540]124 */
[5659]125MultiType& MultiType::operator= (const MultiType& mt)
126{
127  this->type = mt.type;
128  this->value = mt.value;
[7199]129  this->storedString = mt.storedString;
130
[5659]131  return *this;
132}
133
[5545]134/**
[6643]135 * @brief checks if the two Multitypes match
136 * @param mt MultiType to check against this one
137 * @returns true on match. false otherwise
138 *
139 * Two MultiType match if and only if
140 *  1. the internal Type is the same
141 *  2. the stored values match
142 */
143bool MultiType::operator==(const MultiType& mt) const
144{
145  if (this->type != mt.type)
146    return false;
147
148  switch (this->type)
149  {
[8035]150    case MT_NULL:
[6643]151      return true;
[8035]152    case MT_BOOL:
[6643]153      return (this->value.Bool == mt.value.Bool);
[8035]154    case MT_INT:
[6643]155      return (this->value.Int == mt.value.Int);
[8035]156    case MT_CHAR:
[6643]157      return (this->value.Char == mt.value.Char);
[8035]158    case MT_FLOAT:
[6643]159      return (this->value.Float == mt.value.Float);
[8035]160    case MT_STRING:
[7199]161      return (this->storedString == mt.storedString);
[8316]162    default:
163      return false;
[6643]164  }
165}
166
167
168/**
[7199]169 * @brief sets the type of this MultiType and resets to the default value
[5643]170 * @param type the new Type
171 */
[6645]172void MultiType::setType(MT_Type type)
[5540]173{
[7199]174  if (this->type == type)
175    return;
176
177  switch (type)
178  {
[8035]179    case MT_BOOL:
[7199]180      this->setBool(this->getBool());
181      break;
[8035]182    case MT_INT:
[7199]183      this->setInt(this->getInt());
184      break;
[8035]185    case MT_FLOAT:
[7199]186      this->setFloat(this->getFloat());
187      break;
[8035]188    case MT_CHAR:
[7199]189      this->setChar(this->getChar());
190      break;
[8035]191    case MT_STRING:
[7199]192      this->setString(this->getString());
193      break;
[8316]194    default:
195      this->type = type;
[7199]196  }
[5540]197}
198
[5545]199/**
[7199]200 * @brief sets the Value of mt without changing the type of this MultiType
201 * @param mt: the MultiType to get the value from
202 *
203 * This is a pure Value copy. The current type will be preserved.
204 *
205 * @TODO speedup
206 */
207void MultiType::setValueOf(const MultiType& mt)
208{
209  MT_Type prevType = this->type;
210
211  *this = mt;
212  this->setType(prevType);
213}
214
215
216/**
217 * @brief sets a new Value to the MultiType
[5545]218 * @param value the new Value as a bool
219 */
[5540]220void MultiType::setBool(bool value)
221{
222  this->type = MT_BOOL;
223  this->value.Bool = value;
224}
225
[5545]226/**
[7199]227 * @brief sets a new Value to the MultiType
[5545]228 * @param value the new Value as an int
229 */
[5540]230void MultiType::setInt(int value)
231{
232  this->type = MT_INT;
233  this->value.Int = value;
234}
235
[5545]236/**
[7199]237 * @brief sets a new Value to the MultiType
[5545]238 * @param value the new Value as a float
239 */
[5540]240void MultiType::setFloat(float value)
241{
242  this->type = MT_FLOAT;
243  this->value.Float = value;
244}
245
[5545]246/**
[7199]247 * @brief sets a new Value to the MultiType
[5545]248 * @param value the new Value as a char
249 */
[5540]250void MultiType::setChar(char value)
251{
252  this->type = MT_CHAR;
253  this->value.Char = value;
254}
255
[5545]256/**
[7199]257 * @brief sets a new Value to the MultiType
[5545]258 * @param value the new Value as a String
259 */
[7199]260void MultiType::setString(const std::string& value)
[5540]261{
262  this->type = MT_STRING;
[7199]263  this->storedString = value;
[5540]264}
265
266
[7199]267/**************************
268*** RETRIEVAL FUNCTIONS ***
269**************************/
[5545]270/**
271 * @returns the Value of this MultiType as a int
272 */
[5544]273bool MultiType::getBool() const
[5537]274{
275  // default case:
276  if (this->type & MT_BOOL)
277    return this->value.Bool;
278  // Special Cases:
279  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
280  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
281  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
[7199]282  else if (this->type & MT_STRING) return (this->storedString == "true" ||
283                                            this->storedString == "TRUE" ||
284                                            this->storedString != "0"); //! @TODO make this better...
[5538]285
286  return false;
[5537]287}
288
[5545]289/**
290 * @returns the Value of this MultiType as a int
291 */
[5544]292int MultiType::getInt() const
[5537]293{
294  // default case:
295  if (this->type & MT_INT)
296    return this->value.Int;
297  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
298  else if (this->type & MT_FLOAT) return (int) this->value.Float;
299  else if (this->type & MT_CHAR) return (int) this->value.Char;
[7199]300  else if (this->type & MT_STRING)
301  {
[7284]302    std::stringstream ssStream(this->storedString);
303    int iReturn;
304    ssStream >> iReturn;
305    return iReturn;
[5538]306  }
307  return 0;
[5537]308}
309
[7199]310
[5545]311/**
312 * @returns the Value of this MultiType as a float
313 */
[5544]314float MultiType::getFloat() const
[5537]315{
[7199]316  // default case:
[5539]317  if (this->type & MT_FLOAT)
318    return this->value.Float;
319  if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f;
[5537]320  else if (this->type & MT_INT) return (float) this->value.Int;
321  else if (this->type & MT_CHAR) return (float) this->value.Char;
[7199]322  else if (this->type & MT_STRING)
323  {
[5538]324    char* endPtr = NULL;
[7199]325    double result = strtod(this->storedString.c_str(), &endPtr);
[7284]326    if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + this->storedString.size())
[5538]327      return 0.0f;
328    else
329      return result;
330  }
331  return 0.0f;
[5537]332}
333
334
[5545]335/**
336 * @returns the Value of this MultiType as a char
337 */
[5544]338char MultiType::getChar() const
[5537]339{
[7199]340  // default case:
[5537]341  if (this->type & MT_INT)
342    return this->value.Int;
343  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
344  else if (this->type & MT_INT) return (int) this->value.Int;
345  else if (this->type & MT_FLOAT) return (char) this->value.Float;
[7199]346  else if (this->type & MT_STRING) return this->storedString[0];
[5538]347
348  return '\0';
[5537]349}
350
[7199]351
[5545]352/**
353 * @returns the Value of this MultiType as a String
354 */
[7199]355std::string MultiType::getString() const
[5537]356{
[7199]357  // default case:
[5537]358  if (this->type & MT_STRING)
[7199]359    return this->storedString;
[5538]360  else
361  {
362    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
[7199]363
[5641]364    else if (this->type & MT_INT)
365    {
[7199]366      char tmpString[32];
[5538]367      sprintf(tmpString, "%d", this->value.Int);
[7199]368      return tmpString;
[5538]369    }
[7199]370    else if (this->type & MT_FLOAT)
[5538]371    {
[7199]372      char tmpString[64];
[5538]373      sprintf(tmpString, "%f", this->value.Float);
[7199]374      return tmpString;
[5538]375    }
[7200]376    else if (this->type & MT_CHAR)
377    {
378      char tmpString[2];
379      tmpString[0] = this->value.Char;
380      tmpString[1] = '\0';
381      return tmpString;
382    }
[5538]383  }
384  return "";
[5537]385}
386
[5545]387/**
[8035]388 * @brief returns a Constant string (actually this is slower than getString()
389 * @returns a constant string of the stored version's one.
390 * @note this  could lead to a inconsistency of data
391 */
392const std::string& MultiType::getConstString() const
393{
394
395  MultiType::constString = this->getString();
396  return MultiType::constString;
397}
398
399
400/**
[7199]401 * @returns a formated c-string of the held value
[5545]402 */
[7199]403const char* MultiType::getCString()
[5544]404{
[7199]405  if (this->type & MT_STRING) return this->storedString.c_str();
406  else
407  {
408    this->storedString = this->getString();
409    return this->storedString.c_str();
410  }
411}
412
413/**
414 * @brief prints out some nice debug output
415 */
416void MultiType::debug() const
417{
[5659]418#ifdef DEBUG
419  PRINT(0)
420#else
421  printf
422#endif
[7199]423  ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n",
[7401]424   MultiType::MultiTypeToString(this->type).c_str(),
[7199]425   this->getBool(),
426   this->getInt(),
427   this->getFloat(),
428   this->getChar(),
429   this->getString().c_str()
430  );
431}
[5544]432
433
[5545]434/**
[7199]435 * @brief Resets the MultiType to default values.
[5643]436 */
437void MultiType::reset()
438{
[5659]439  switch ( this->type )
[5643]440  {
[8035]441    case MT_BOOL:
[5643]442      this->setBool(false);
[7200]443      break;
[8035]444    case MT_INT:
[5643]445      this->setInt(0);
446      break;
[8035]447    case MT_FLOAT:
[5643]448      this->setFloat(0.0f);
449      break;
[8035]450    case MT_CHAR:
[5643]451      this->setChar('\0');
452      break;
[8035]453    case MT_STRING:
[5643]454      this->setString("");
455      break;
[8035]456    default:
[5659]457#ifdef DEBUG
458      PRINTF(2)("Unknown Type not reseting\n");
459#endif
460      break;
[5643]461  }
462}
463
464/**
[7199]465 * @brief converts a MT_Type into a String
[5545]466 * @param type: the MT_Type
467 * @returns: the Type as a constant String (do not delete)
468 */
[7401]469const std::string& MultiType::MultiTypeToString(MT_Type type)
[5544]470{
[5659]471  switch ( type )
[5544]472  {
[8035]473    case MT_BOOL:
[7401]474      return MultiType::typeNames[1];
[8035]475    case MT_INT:
[7401]476      return MultiType::typeNames[2];
[8035]477    case MT_FLOAT:
[7401]478      return MultiType::typeNames[3];
[8035]479    case MT_CHAR:
[7401]480      return MultiType::typeNames[4];
[8035]481    case MT_STRING:
[7401]482      return MultiType::typeNames[5];
[8316]483    default:
484      return MultiType::typeNames[0];
[5544]485  }
486}
487
[5545]488/**
[7199]489 * @brief converts a String into a MT_Type
[5545]490 * @param type: the Type as a String
491 * @returns: the Type as MT_Type
492 */
[7221]493MT_Type MultiType::StringToMultiType(const std::string& type)
[5544]494{
[7401]495  if (type == MultiType::typeNames[1])
[5544]496    return MT_BOOL;
[7401]497  if (type == MultiType::typeNames[2])
[5544]498    return MT_INT;
[7401]499  if (type == MultiType::typeNames[3])
[5544]500    return MT_FLOAT;
[7401]501  if (type == MultiType::typeNames[4])
[5544]502    return MT_CHAR;
[7401]503  if (type == MultiType::typeNames[5])
[5544]504    return MT_STRING;
[7200]505
506  return MT_NULL;
[5544]507}
[7401]508
509
[8035]510std::string MultiType::constString = "";
[7401]511const std::string MultiType::typeNames[] =
512  {
513    "NONE",      //0
514    "bool",      //1
515    "int",       //2
516    "float",     //3
517    "char",      //4
518    "string"     //5
519  };
Note: See TracBrowser for help on using the repository browser.