Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 8.9 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
26#ifdef DEBUG
27#include "debug.h"
28#endif
29
30using namespace std;
31
32/**
33 * creates a multiType without any stored value at all.
34 */
35MultiType::MultiType()
36{
37  this->init();
38}
39/**
40 * creates a multiType out of a boolean
41 * @param value the Value of this MulitType
42 */
43MultiType::MultiType(bool value)
44{
45  this->init();
46  this->setBool(value);
47}
48
49/**
50 * creates a multiType out of an integer
51 * @param value the Value of this MulitType
52 */
53MultiType::MultiType(int value)
54{
55  this->init();
56  this->setInt(value);
57}
58
59/**
60 * creates a multiType out of a float (double)
61 * @param value the Value of this MulitType
62 */
63MultiType::MultiType(double value)
64{
65  this->init();
66  this->setFloat(value);
67}
68
69/**
70 * creates a multiType out of a char
71 * @param value the Value of this MulitType
72 */
73MultiType::MultiType(char value)
74{
75  this->init();
76  this->setChar(value);
77}
78
79/**
80 * creates a multiType out of a String
81 * @param value the Value of this MulitType
82 */
83MultiType::MultiType(const char* value)
84{
85  this->init();
86  this->setString(value);
87}
88
89MultiType::MultiType(const MultiType& multiType)
90{
91  this->init();
92  printf("test2");
93  *this = multiType;
94}
95
96/**
97 * standard deconstructor
98*/
99MultiType::~MultiType ()
100{
101  if (this->storedString != NULL)
102    delete[] this->storedString;
103}
104
105/**
106 * copy Constructor
107 * @param mt: the entity to copy
108 * @returns a Copy of itself. (strings inside are copied as well)
109 */
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
123/**
124 * initializes the MultiType
125 */
126void MultiType::init()
127{
128  this->type = MT_NULL;
129  this->storedString = NULL;
130  this->value.Float = 0.0f;
131}
132
133
134/**
135 * sets the type of this MultiType and resets to the default value
136 * @param type the new Type
137 */
138void MultiType::setType(int type)
139{
140  if (this->type != type)
141    this->type = (MT_Type)type;
142}
143
144/**
145 * sets a new Value to the MultiType
146 * @param value the new Value as a bool
147 */
148void MultiType::setBool(bool value)
149{
150  this->type = MT_BOOL;
151  this->value.Bool = value;
152}
153
154/**
155 * sets a new Value to the MultiType
156 * @param value the new Value as an int
157 */
158void MultiType::setInt(int value)
159{
160  this->type = MT_INT;
161  this->value.Int = value;
162}
163
164/**
165 * sets a new Value to the MultiType
166 * @param value the new Value as a float
167 */
168void MultiType::setFloat(float value)
169{
170  this->type = MT_FLOAT;
171  this->value.Float = value;
172}
173
174/**
175 * sets a new Value to the MultiType
176 * @param value the new Value as a char
177 */
178void MultiType::setChar(char value)
179{
180  this->type = MT_CHAR;
181  this->value.Char = value;
182}
183
184/**
185 * sets a new Value to the MultiType
186 * @param value the new Value as a String
187 */
188void MultiType::setString(const char* value)
189{
190  this->type = MT_STRING;
191
192  if (this->storedString != NULL)
193    delete[] this->storedString;
194
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  }
208}
209
210
211
212
213
214/**
215 * @returns the Value of this MultiType as a int
216 */
217bool MultiType::getBool() const
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;
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.
227
228  return false;
229}
230
231/**
232 * @returns the Value of this MultiType as a int
233 */
234int MultiType::getInt() const
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;
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;
252}
253
254/**
255 * @returns the Value of this MultiType as a float
256 */
257float MultiType::getFloat() const
258{
259 // default case:
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;
263  else if (this->type & MT_INT) return (float) this->value.Int;
264  else if (this->type & MT_CHAR) return (float) this->value.Char;
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;
275}
276
277
278/**
279 * @returns the Value of this MultiType as a char
280 */
281char MultiType::getChar() const
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;
289  else if (this->type & MT_STRING) return *this->value.String;
290
291  return '\0';
292}
293
294/**
295 * @returns the Value of this MultiType as a String
296 */
297const char* MultiType::getString()
298{
299 // default case:
300  if (this->type & MT_STRING)
301    return (this->value.String != NULL)? this->value.String : "";
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    }
311    if (this->type & MT_CHAR)
312    {
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    {
320      sprintf(tmpString, "%d", this->value.Int);
321      this->storedString = new char[strlen(tmpString)+1];
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 "";
335}
336
337/**
338 * prints out some nice debug output
339 */
340void MultiType::debug()
341{
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",
348         MultiType::MultiTypeToString(this->type),
349         this->getBool(),
350         this->getInt(),
351         this->getFloat(),
352         this->getChar(),
353         this->getString()
354      );
355
356
357}
358
359/**
360 * Resets the MultiType to default values.
361 */
362void MultiType::reset()
363{
364  switch ( this->type )
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;
380    default:
381#ifdef DEBUG
382      PRINTF(2)("Unknown Type not reseting\n");
383#endif
384      break;
385  }
386}
387
388/**
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 */
393const char* MultiType::MultiTypeToString(MT_Type type)
394{
395  switch ( type )
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
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 */
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.