Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/util/multi_type.cc @ 9346

Last change on this file since 9346 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
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 "multi_type.h"
19
20//#include "stdincl.h"
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25#include <sstream>
26
27#ifdef DEBUG
28#include "debug.h"
29#endif
30
31using namespace std;
32
33/**
34 * @brief creates a multiType without any stored value at all.
35 */
36MultiType::MultiType(MT_Type type)
37{
38  this->type = type;
39  switch (this->type)
40  {
41    default:
42      this->value.Float = 0.0f;
43      break;
44    case MT_BOOL:
45      this->value.Bool = false;
46      break;
47    case MT_INT:
48      this->value.Int = 0;
49      break;
50    case MT_FLOAT:
51      this->value.Float = 0.0f;
52      break;
53    case MT_CHAR:
54      this->value.Char = '\0';
55      break;
56    case MT_STRING:
57      this->storedString = "";
58      break;
59  }
60}
61/**
62 * @brief creates a multiType out of a boolean
63 * @param value the Value of this MulitType
64 */
65MultiType::MultiType(bool value)
66{
67  this->setBool(value);
68}
69
70/**
71 * @brief creates a multiType out of an integer
72 * @param value the Value of this MulitType
73 */
74MultiType::MultiType(int value)
75{
76  this->setInt(value);
77}
78
79/**
80 * @brief creates a multiType out of a float (double)
81 * @param value the Value of this MulitType
82 */
83MultiType::MultiType(double value)
84{
85  this->setFloat(value);
86}
87
88/**
89 * @brief creates a multiType out of a char
90 * @param value the Value of this MulitType
91 */
92MultiType::MultiType(char value)
93{
94  this->setChar(value);
95}
96
97/**
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 */
109MultiType::MultiType(const MultiType& multiType)
110{
111  *this = multiType;
112}
113
114/**
115 * @brief standard deconstructor
116*/
117MultiType::~MultiType ()
118{ }
119
120/**
121 * @brief copy operator
122 * @param mt: the entity to copy
123 * @returns A Copy of itself. (strings inside are copied as well)
124 */
125MultiType& MultiType::operator= (const MultiType& mt)
126{
127  this->type = mt.type;
128  this->value = mt.value;
129  this->storedString = mt.storedString;
130
131  return *this;
132}
133
134/**
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  {
150    case MT_NULL:
151      return true;
152    case MT_BOOL:
153      return (this->value.Bool == mt.value.Bool);
154    case MT_INT:
155      return (this->value.Int == mt.value.Int);
156    case MT_CHAR:
157      return (this->value.Char == mt.value.Char);
158    case MT_FLOAT:
159      return (this->value.Float == mt.value.Float);
160    case MT_STRING:
161      return (this->storedString == mt.storedString);
162    default:
163      return false;
164  }
165}
166
167
168/**
169 * @brief sets the type of this MultiType and resets to the default value
170 * @param type the new Type
171 */
172void MultiType::setType(MT_Type type)
173{
174  if (this->type == type)
175    return;
176
177  switch (type)
178  {
179    case MT_BOOL:
180      this->setBool(this->getBool());
181      break;
182    case MT_INT:
183      this->setInt(this->getInt());
184      break;
185    case MT_FLOAT:
186      this->setFloat(this->getFloat());
187      break;
188    case MT_CHAR:
189      this->setChar(this->getChar());
190      break;
191    case MT_STRING:
192      this->setString(this->getString());
193      break;
194    default:
195      this->type = type;
196  }
197}
198
199/**
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
218 * @param value the new Value as a bool
219 */
220void MultiType::setBool(bool value)
221{
222  this->type = MT_BOOL;
223  this->value.Bool = value;
224}
225
226/**
227 * @brief sets a new Value to the MultiType
228 * @param value the new Value as an int
229 */
230void MultiType::setInt(int value)
231{
232  this->type = MT_INT;
233  this->value.Int = value;
234}
235
236/**
237 * @brief sets a new Value to the MultiType
238 * @param value the new Value as a float
239 */
240void MultiType::setFloat(float value)
241{
242  this->type = MT_FLOAT;
243  this->value.Float = value;
244}
245
246/**
247 * @brief sets a new Value to the MultiType
248 * @param value the new Value as a char
249 */
250void MultiType::setChar(char value)
251{
252  this->type = MT_CHAR;
253  this->value.Char = value;
254}
255
256/**
257 * @brief sets a new Value to the MultiType
258 * @param value the new Value as a String
259 */
260void MultiType::setString(const std::string& value)
261{
262  this->type = MT_STRING;
263  this->storedString = value;
264}
265
266
267/**************************
268*** RETRIEVAL FUNCTIONS ***
269**************************/
270/**
271 * @returns the Value of this MultiType as a int
272 */
273bool MultiType::getBool() const
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;
282  else if (this->type & MT_STRING) return (this->storedString == "true" ||
283                                            this->storedString == "TRUE" ||
284                                            this->storedString != "0"); //! @TODO make this better...
285
286  return false;
287}
288
289/**
290 * @returns the Value of this MultiType as a int
291 */
292int MultiType::getInt() const
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;
300  else if (this->type & MT_STRING)
301  {
302    std::stringstream ssStream(this->storedString);
303    int iReturn;
304    ssStream >> iReturn;
305    return iReturn;
306  }
307  return 0;
308}
309
310
311/**
312 * @returns the Value of this MultiType as a float
313 */
314float MultiType::getFloat() const
315{
316  // default case:
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;
320  else if (this->type & MT_INT) return (float) this->value.Int;
321  else if (this->type & MT_CHAR) return (float) this->value.Char;
322  else if (this->type & MT_STRING)
323  {
324    char* endPtr = NULL;
325    double result = strtod(this->storedString.c_str(), &endPtr);
326    if ( endPtr >= this->storedString.c_str() && endPtr < this->storedString.c_str() + this->storedString.size())
327      return 0.0f;
328    else
329      return result;
330  }
331  return 0.0f;
332}
333
334
335/**
336 * @returns the Value of this MultiType as a char
337 */
338char MultiType::getChar() const
339{
340  // default case:
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;
346  else if (this->type & MT_STRING) return this->storedString[0];
347
348  return '\0';
349}
350
351
352/**
353 * @returns the Value of this MultiType as a String
354 */
355std::string MultiType::getString() const
356{
357  // default case:
358  if (this->type & MT_STRING)
359    return this->storedString;
360  else
361  {
362    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
363
364    else if (this->type & MT_INT)
365    {
366      char tmpString[32];
367      sprintf(tmpString, "%d", this->value.Int);
368      return tmpString;
369    }
370    else if (this->type & MT_FLOAT)
371    {
372      char tmpString[64];
373      sprintf(tmpString, "%f", this->value.Float);
374      return tmpString;
375    }
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    }
383  }
384  return "";
385}
386
387/**
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/**
401 * @returns a formated c-string of the held value
402 */
403const char* MultiType::getCString()
404{
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{
418#ifdef DEBUG
419  PRINT(0)
420#else
421  printf
422#endif
423  ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n",
424   MultiType::MultiTypeToString(this->type).c_str(),
425   this->getBool(),
426   this->getInt(),
427   this->getFloat(),
428   this->getChar(),
429   this->getString().c_str()
430  );
431}
432
433
434/**
435 * @brief Resets the MultiType to default values.
436 */
437void MultiType::reset()
438{
439  switch ( this->type )
440  {
441    case MT_BOOL:
442      this->setBool(false);
443      break;
444    case MT_INT:
445      this->setInt(0);
446      break;
447    case MT_FLOAT:
448      this->setFloat(0.0f);
449      break;
450    case MT_CHAR:
451      this->setChar('\0');
452      break;
453    case MT_STRING:
454      this->setString("");
455      break;
456    default:
457#ifdef DEBUG
458      PRINTF(2)("Unknown Type not reseting\n");
459#endif
460      break;
461  }
462}
463
464/**
465 * @brief converts a MT_Type into a String
466 * @param type: the MT_Type
467 * @returns: the Type as a constant String (do not delete)
468 */
469const std::string& MultiType::MultiTypeToString(MT_Type type)
470{
471  switch ( type )
472  {
473    case MT_BOOL:
474      return MultiType::typeNames[1];
475    case MT_INT:
476      return MultiType::typeNames[2];
477    case MT_FLOAT:
478      return MultiType::typeNames[3];
479    case MT_CHAR:
480      return MultiType::typeNames[4];
481    case MT_STRING:
482      return MultiType::typeNames[5];
483    default:
484      return MultiType::typeNames[0];
485  }
486}
487
488/**
489 * @brief converts a String into a MT_Type
490 * @param type: the Type as a String
491 * @returns: the Type as MT_Type
492 */
493MT_Type MultiType::StringToMultiType(const std::string& type)
494{
495  if (type == MultiType::typeNames[1])
496    return MT_BOOL;
497  if (type == MultiType::typeNames[2])
498    return MT_INT;
499  if (type == MultiType::typeNames[3])
500    return MT_FLOAT;
501  if (type == MultiType::typeNames[4])
502    return MT_CHAR;
503  if (type == MultiType::typeNames[5])
504    return MT_STRING;
505
506  return MT_NULL;
507}
508
509
510std::string MultiType::constString = "";
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.