Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Executor works just fine.
Changed: Paramerte* to MT_*, to make things more general

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