Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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