Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: renaming of BaseLoadParam to LoadParamBase

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