Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more elaborate MultiType

File size: 3.3 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
21using namespace std;
22
23MultiType::MultiType(bool value)
24{
25  this->init();
26  this->type = MT_BOOL;
27  this->value.Bool = value;
28
29}
30
31MultiType::MultiType(int value)
32{
33  this->init();
34  this->type = MT_INT;
35  this->value.Int = value;
36}
37
38
39MultiType::MultiType(float value)
40{
41  this->init();
42  this->type = MT_FLOAT;
43  this->value.Float = value;
44}
45
46
47MultiType::MultiType(char value)
48{
49  this->init();
50  this->type = MT_CHAR;
51  this->value.Char = value;
52}
53
54
55MultiType::MultiType(const char* value)
56{
57  this->init();
58  this->type = MT_STRING;
59  this->value.String = new char[strlen(value)+1];
60  strcpy(this->value.String, value);
61
62  this->storedString = this->value.String;
63}
64
65/**
66 * standard deconstructor
67*/
68MultiType::~MultiType ()
69{
70  if (this->storedString != NULL)
71    delete this->storedString;
72}
73
74void MultiType::init()
75{
76  this->storedString = NULL;
77}
78
79
80bool MultiType::getBool()
81{
82  // default case:
83  if (this->type & MT_BOOL)
84    return this->value.Bool;
85  // Special Cases:
86  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
87  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
88  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
89  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;
90}
91
92
93int MultiType::getInt()
94{
95  // default case:
96  if (this->type & MT_INT)
97    return this->value.Int;
98  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
99  else if (this->type & MT_FLOAT) return (int) this->value.Float;
100  else if (this->type & MT_CHAR) return (int) this->value.Char;
101  else if (this->type & MT_STRING) return 1; //! @TODO
102}
103
104
105float MultiType::getFloat()
106{
107 // default case:
108  if (this->type & MT_FLOAT) return this->value.Float;
109    return this->value.Int;
110  if (this->type & MT_BOOL) return (this->value.Bool)? 1.0f : 0.0f;
111  else if (this->type & MT_INT) return (float) this->value.Int;
112  else if (this->type & MT_CHAR) return (float) this->value.Char;
113  else if (this->type & MT_STRING) return 1; //! @TODO
114}
115
116
117char MultiType::getChar()
118{
119 // default case:
120  if (this->type & MT_INT)
121    return this->value.Int;
122  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
123  else if (this->type & MT_INT) return (int) this->value.Int;
124  else if (this->type & MT_FLOAT) return (char) this->value.Float;
125  else if (this->type & MT_STRING) return 1; //! @TODO
126}
127
128const char* MultiType::getString()
129{
130 // default case:
131  if (this->type & MT_STRING)
132    return this->value.String;
133/*  else if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
134  else if (this->type & MT_INT) return 1; //! @TODO
135  else if (this->type & MT_FLOAT) return (int) this->value.Float;
136  else if (this->type & MT_CHAR) return (int) this->value.Char;*/
137}
138
Note: See TracBrowser for help on using the repository browser.