Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellCommand now uses MultiType instead of many different strange constellations →
saves space and complexity (a bit complexity at least)

File size: 7.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: ...
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)
104  {
105    this->storedString = new char[strlen (mt.storedString)+1];
106    strcpy(this->storedString, mt.storedString);
107    this->value.String = this->storedString;
108  }
109}
110
111/**
112 * initializes the MultiType
113 */
114void MultiType::init()
115{
116  this->type = MT_NULL;
117  this->storedString = NULL;
118}
119
120
121
122void MultiType::setType(MT_Type)
123{
124
125}
126
127/**
128 * sets a new Value to the MultiType
129 * @param value the new Value as a bool
130 */
131void MultiType::setBool(bool value)
132{
133  this->type = MT_BOOL;
134  this->value.Bool = value;
135}
136
137/**
138 * sets a new Value to the MultiType
139 * @param value the new Value as an int
140 */
141void MultiType::setInt(int value)
142{
143  this->type = MT_INT;
144  this->value.Int = value;
145}
146
147/**
148 * sets a new Value to the MultiType
149 * @param value the new Value as a float
150 */
151void MultiType::setFloat(float value)
152{
153  this->type = MT_FLOAT;
154  this->value.Float = value;
155
156}
157
158/**
159 * sets a new Value to the MultiType
160 * @param value the new Value as a char
161 */
162void MultiType::setChar(char value)
163{
164  this->type = MT_CHAR;
165  this->value.Char = value;
166}
167
168/**
169 * sets a new Value to the MultiType
170 * @param value the new Value as a String
171 */
172void MultiType::setString(const char* value)
173{
174  this->type = MT_STRING;
175
176  if (this->storedString != NULL)
177    delete[] this->storedString;
178
179  if (value == NULL)
180  {
181    this->storedString = new char[1];
182    this->storedString[0] = '\0';
183    this->value.String = this->storedString;
184    return;
185  }
186  else
187  {
188    this->storedString = new char[strlen(value)+1];
189    strcpy(storedString, value);
190    this->value.String = this->storedString;
191  }
192}
193
194
195
196
197
198/**
199 * @returns the Value of this MultiType as a int
200 */
201bool MultiType::getBool() const
202{
203  // default case:
204  if (this->type & MT_BOOL)
205    return this->value.Bool;
206  // Special Cases:
207  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
208  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
209  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
210  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.
211
212  return false;
213}
214
215/**
216 * @returns the Value of this MultiType as a int
217 */
218int MultiType::getInt() const
219{
220  // default case:
221  if (this->type & MT_INT)
222    return this->value.Int;
223  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
224  else if (this->type & MT_FLOAT) return (int) this->value.Float;
225  else if (this->type & MT_CHAR) return (int) this->value.Char;
226  else if (this->type & MT_STRING) {
227    char* endPtr = NULL;
228    int result = strtol(this->value.String, &endPtr, 10);
229    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
230      return 0;
231    else
232      return result;
233  }
234
235  return 0;
236}
237
238/**
239 * @returns the Value of this MultiType as a float
240 */
241float MultiType::getFloat() const
242{
243 // default case:
244  if (this->type & MT_FLOAT)
245    return this->value.Float;
246  if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f;
247  else if (this->type & MT_INT) return (float) this->value.Int;
248  else if (this->type & MT_CHAR) return (float) this->value.Char;
249  else if (this->type & MT_STRING) {
250    char* endPtr = NULL;
251    double result = strtod(this->value.String, &endPtr);
252    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
253      return 0.0f;
254    else
255      return result;
256  }
257
258  return 0.0f;
259}
260
261
262/**
263 * @returns the Value of this MultiType as a char
264 */
265char MultiType::getChar() const
266{
267 // default case:
268  if (this->type & MT_INT)
269    return this->value.Int;
270  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
271  else if (this->type & MT_INT) return (int) this->value.Int;
272  else if (this->type & MT_FLOAT) return (char) this->value.Float;
273  else if (this->type & MT_STRING) return *this->value.String;
274
275  return '\0';
276}
277
278/**
279 * @returns the Value of this MultiType as a String
280 */
281const char* MultiType::getString()
282{
283 // default case:
284  if (this->type & MT_STRING)
285    return this->value.String;
286  else
287  {
288    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
289    else if (this->type & MT_CHAR) &this->value.Char;
290
291    char tmpString[128];
292    if (this->storedString != NULL)
293    {
294      delete[] this->storedString;
295      this->storedString = NULL;
296    }
297
298    if (this->type & MT_INT)
299    {
300      sprintf(tmpString, "%d", this->value.Int);
301      this->storedString = new char[strlen (tmpString)+1];
302      strcpy (this->storedString, tmpString);
303      return this->storedString;
304    }
305    if (this->type & MT_FLOAT)
306    {
307      sprintf(tmpString, "%f", this->value.Float);
308      this->storedString = new char[strlen (tmpString)+1];
309      strcpy (this->storedString, tmpString);
310      return this->storedString;
311    }
312  }
313
314  return "";
315}
316
317/**
318 * prints out some nice debug output
319 */
320void MultiType::debug()
321{
322  printf("MultiType of Type: %s is: BOOL: %d, INT: %d, FLOAT: %f, CHAR: %c, STRING %s\n",
323         MultiType::MultiTypeToString(this->type),
324         this->getBool(),
325         this->getInt(),
326         this->getFloat(),
327         this->getChar(),
328         this->getString()
329  );
330
331
332}
333
334/**
335 * converts a MT_Type into a String
336 * @param type: the MT_Type
337 * @returns: the Type as a constant String (do not delete)
338 */
339const char* MultiType::MultiTypeToString(MT_Type type)
340{
341  switch (type)
342  {
343   default:
344    return "NONE";
345   case MT_BOOL:
346    return "bool";
347   case MT_INT:
348    return "int";
349   case MT_FLOAT:
350    return "float";
351   case MT_CHAR:
352    return "char";
353   case MT_STRING:
354    return "string";
355  }
356}
357
358/**
359 * converts a String into a MT_Type
360 * @param type: the Type as a String
361 * @returns: the Type as MT_Type
362 */
363MT_Type MultiType::StringToMultiType(const char* type)
364{
365  if (!strncmp(type, "bool", 4))
366    return MT_BOOL;
367  if (!strncmp(type, "int", 3))
368    return MT_INT;
369  if (!strncmp(type, "float", 5))
370    return MT_FLOAT;
371  if (!strncmp(type, "char", 4))
372    return MT_CHAR;
373  if (!strncmp(type, "string", 6))
374    return MT_STRING;
375}
Note: See TracBrowser for help on using the repository browser.