Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: valgrind sweep

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