Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Passing Reference inastead of Pointer to create ShellCommand

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