Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5934 was 5660, checked in by bensch, 19 years ago

orxonox/trunk: less useless output… wow … big commit

File size: 8.9 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>
25
26#ifdef DEBUG
27#include "debug.h"
28#endif
29
[1856]30using namespace std;
[1853]31
[5545]32/**
33 * creates a multiType without any stored value at all.
34 */
[5540]35MultiType::MultiType()
36{
[5552]37  this->init();
[5540]38}
[5545]39/**
40 * creates a multiType out of a boolean
41 * @param value the Value of this MulitType
42 */
[5537]43MultiType::MultiType(bool value)
44{
45  this->init();
[5540]46  this->setBool(value);
[5537]47}
48
[5545]49/**
50 * creates a multiType out of an integer
51 * @param value the Value of this MulitType
52 */
[5536]53MultiType::MultiType(int value)
54{
[5537]55  this->init();
[5540]56  this->setInt(value);
[5536]57}
[1856]58
[5545]59/**
60 * creates a multiType out of a float (double)
61 * @param value the Value of this MulitType
62 */
[5539]63MultiType::MultiType(double value)
[3365]64{
[5537]65  this->init();
[5540]66  this->setFloat(value);
[5536]67}
[4320]68
[5545]69/**
70 * creates a multiType out of a char
71 * @param value the Value of this MulitType
72 */
[5536]73MultiType::MultiType(char value)
74{
[5537]75  this->init();
[5540]76  this->setChar(value);
[3365]77}
[1853]78
[5545]79/**
80 * creates a multiType out of a String
81 * @param value the Value of this MulitType
82 */
[5536]83MultiType::MultiType(const char* value)
84{
[5537]85  this->init();
[5540]86  this->setString(value);
[5536]87}
88
[5659]89MultiType::MultiType(const MultiType& multiType)
90{
91  this->init();
92  printf("test2");
93  *this = multiType;
94}
95
[3245]96/**
[4838]97 * standard deconstructor
[3245]98*/
[5536]99MultiType::~MultiType ()
[3543]100{
[5537]101  if (this->storedString != NULL)
[5541]102    delete[] this->storedString;
[3543]103}
[5537]104
[5540]105/**
106 * copy Constructor
107 * @param mt: the entity to copy
108 * @returns a Copy of itself. (strings inside are copied as well)
109 */
[5659]110MultiType& MultiType::operator= (const MultiType& mt)
111{
112  this->type = mt.type;
113  this->value = mt.value;
114  if (mt.type == MT_STRING && mt.storedString != NULL)
115  {
116    this->setString(mt.storedString);
117  }
118  else
119    this->storedString = NULL;
120  return *this;
121}
122
[5545]123/**
124 * initializes the MultiType
125 */
[5537]126void MultiType::init()
127{
[5540]128  this->type = MT_NULL;
[5537]129  this->storedString = NULL;
[5659]130  this->value.Float = 0.0f;
[5537]131}
132
133
[5643]134/**
135 * sets the type of this MultiType and resets to the default value
136 * @param type the new Type
137 */
[5633]138void MultiType::setType(int type)
[5540]139{
[5642]140  if (this->type != type)
[5643]141    this->type = (MT_Type)type;
[5540]142}
143
[5545]144/**
145 * sets a new Value to the MultiType
146 * @param value the new Value as a bool
147 */
[5540]148void MultiType::setBool(bool value)
149{
150  this->type = MT_BOOL;
151  this->value.Bool = value;
152}
153
[5545]154/**
155 * sets a new Value to the MultiType
156 * @param value the new Value as an int
157 */
[5540]158void MultiType::setInt(int value)
159{
160  this->type = MT_INT;
161  this->value.Int = value;
162}
163
[5545]164/**
165 * sets a new Value to the MultiType
166 * @param value the new Value as a float
167 */
[5540]168void MultiType::setFloat(float value)
169{
170  this->type = MT_FLOAT;
171  this->value.Float = value;
172}
173
[5545]174/**
175 * sets a new Value to the MultiType
176 * @param value the new Value as a char
177 */
[5540]178void MultiType::setChar(char value)
179{
180  this->type = MT_CHAR;
181  this->value.Char = value;
182}
183
[5545]184/**
185 * sets a new Value to the MultiType
186 * @param value the new Value as a String
187 */
[5540]188void MultiType::setString(const char* value)
189{
190  this->type = MT_STRING;
191
[5545]192  if (this->storedString != NULL)
193    delete[] this->storedString;
194
[5552]195  if (value == NULL)
196  {
197    this->storedString = new char[1];
198    this->storedString[0] = '\0';
199    this->value.String = this->storedString;
200    return;
201  }
202  else
203  {
204    this->storedString = new char[strlen(value)+1];
205    strcpy(storedString, value);
206    this->value.String = this->storedString;
207  }
[5540]208}
209
210
211
212
213
[5545]214/**
215 * @returns the Value of this MultiType as a int
216 */
[5544]217bool MultiType::getBool() const
[5537]218{
219  // default case:
220  if (this->type & MT_BOOL)
221    return this->value.Bool;
222  // Special Cases:
223  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
224  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
225  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
[5539]226  else if (this->type & MT_STRING) return (!strncmp(this->value.String, "true", 4) || !strncmp(this->value.String, "TRUE", 4) || !strncmp(this->value.String, "1", 1))? true : false; //! @TODO make this better.
[5538]227
228  return false;
[5537]229}
230
[5545]231/**
232 * @returns the Value of this MultiType as a int
233 */
[5544]234int MultiType::getInt() const
[5537]235{
236  // default case:
237  if (this->type & MT_INT)
238    return this->value.Int;
239  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
240  else if (this->type & MT_FLOAT) return (int) this->value.Float;
241  else if (this->type & MT_CHAR) return (int) this->value.Char;
[5538]242  else if (this->type & MT_STRING) {
243    char* endPtr = NULL;
244    int result = strtol(this->value.String, &endPtr, 10);
245    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
246      return 0;
247    else
248      return result;
249  }
250
251  return 0;
[5537]252}
253
[5545]254/**
255 * @returns the Value of this MultiType as a float
256 */
[5544]257float MultiType::getFloat() const
[5537]258{
259 // default case:
[5539]260  if (this->type & MT_FLOAT)
261    return this->value.Float;
262  if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f;
[5537]263  else if (this->type & MT_INT) return (float) this->value.Int;
264  else if (this->type & MT_CHAR) return (float) this->value.Char;
[5538]265  else if (this->type & MT_STRING) {
266    char* endPtr = NULL;
267    double result = strtod(this->value.String, &endPtr);
268    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
269      return 0.0f;
270    else
271      return result;
272  }
273
274  return 0.0f;
[5537]275}
276
277
[5545]278/**
279 * @returns the Value of this MultiType as a char
280 */
[5544]281char MultiType::getChar() const
[5537]282{
283 // default case:
284  if (this->type & MT_INT)
285    return this->value.Int;
286  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
287  else if (this->type & MT_INT) return (int) this->value.Int;
288  else if (this->type & MT_FLOAT) return (char) this->value.Float;
[5538]289  else if (this->type & MT_STRING) return *this->value.String;
290
291  return '\0';
[5537]292}
293
[5545]294/**
295 * @returns the Value of this MultiType as a String
296 */
[5537]297const char* MultiType::getString()
298{
299 // default case:
300  if (this->type & MT_STRING)
[5638]301    return (this->value.String != NULL)? this->value.String : "";
[5538]302  else
303  {
304    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
305    char tmpString[128];
306    if (this->storedString != NULL)
307    {
308      delete[] this->storedString;
309      this->storedString = NULL;
310    }
[5641]311    if (this->type & MT_CHAR)
[5538]312    {
[5641]313      this->storedString = new char[2];
314      this->storedString[0] = this->value.Char;
315      this->storedString[1] = '\0';
316      return this->storedString;
317    }
318    else if (this->type & MT_INT)
319    {
[5538]320      sprintf(tmpString, "%d", this->value.Int);
[5642]321      this->storedString = new char[strlen(tmpString)+1];
[5538]322      strcpy (this->storedString, tmpString);
323      return this->storedString;
324    }
325    if (this->type & MT_FLOAT)
326    {
327      sprintf(tmpString, "%f", this->value.Float);
328      this->storedString = new char[strlen (tmpString)+1];
329      strcpy (this->storedString, tmpString);
330      return this->storedString;
331    }
332  }
333
334  return "";
[5537]335}
336
[5545]337/**
338 * prints out some nice debug output
339 */
[5544]340void MultiType::debug()
341{
[5659]342#ifdef DEBUG
343  PRINT(0)
344#else
345  printf
346#endif
347      ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n",
[5544]348         MultiType::MultiTypeToString(this->type),
349         this->getBool(),
350         this->getInt(),
351         this->getFloat(),
352         this->getChar(),
353         this->getString()
[5659]354      );
[5544]355
356
357}
358
[5545]359/**
[5643]360 * Resets the MultiType to default values.
361 */
362void MultiType::reset()
363{
[5659]364  switch ( this->type )
[5643]365  {
366    case MT_BOOL:
367      this->setBool(false);
368    case MT_INT:
369      this->setInt(0);
370      break;
371    case MT_FLOAT:
372      this->setFloat(0.0f);
373      break;
374    case MT_CHAR:
375      this->setChar('\0');
376      break;
377    case MT_STRING:
378      this->setString("");
379      break;
[5659]380    default:
381#ifdef DEBUG
382      PRINTF(2)("Unknown Type not reseting\n");
383#endif
384      break;
[5643]385  }
386}
387
388/**
[5545]389 * converts a MT_Type into a String
390 * @param type: the MT_Type
391 * @returns: the Type as a constant String (do not delete)
392 */
[5544]393const char* MultiType::MultiTypeToString(MT_Type type)
394{
[5659]395  switch ( type )
[5544]396  {
397   default:
398    return "NONE";
399   case MT_BOOL:
400    return "bool";
401   case MT_INT:
402    return "int";
403   case MT_FLOAT:
404    return "float";
405   case MT_CHAR:
406    return "char";
407   case MT_STRING:
408    return "string";
409  }
410}
411
[5545]412/**
413 * converts a String into a MT_Type
414 * @param type: the Type as a String
415 * @returns: the Type as MT_Type
416 */
[5544]417MT_Type MultiType::StringToMultiType(const char* type)
418{
419  if (!strncmp(type, "bool", 4))
420    return MT_BOOL;
421  if (!strncmp(type, "int", 3))
422    return MT_INT;
423  if (!strncmp(type, "float", 5))
424    return MT_FLOAT;
425  if (!strncmp(type, "char", 4))
426    return MT_CHAR;
427  if (!strncmp(type, "string", 6))
428    return MT_STRING;
429}
Note: See TracBrowser for help on using the repository browser.