Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/MultiTypePrimitive.cc @ 931

Last change on this file since 931 was 925, checked in by landauf, 16 years ago
  • the MultiTypes can now handle pointers and xml-elements
  • added a const keyword in the ticpp.h file (TinyXML++)
File size: 10.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 *   Inspiration: MultiType by Benjamin Grauer
27 */
28
29#include "MultiTypePrimitive.h"
30#include "Convert.h"
31
32MultiTypePrimitive::MultiTypePrimitive(MultiType type)
33{
34    this->type_ = type;
35
36    if (type == MT_void)
37        this->value_.void_ = 0;
38    else if (type == MT_int)
39        this->value_.int_ = 0;
40    else if (type == MT_uint)
41        this->value_.uint_ = 0;
42    else if (type == MT_char)
43        this->value_.char_ = 0;
44    else if (type == MT_uchar)
45        this->value_.uchar_ = 0;
46    else if (type == MT_short)
47        this->value_.short_ = 0;
48    else if (type == MT_ushort)
49        this->value_.ushort_ = 0;
50    else if (type == MT_long)
51        this->value_.long_ = 0;
52    else if (type == MT_ulong)
53        this->value_.ulong_ = 0;
54    else if (type == MT_float)
55        this->value_.float_ = 0.0;
56    else if (type == MT_double)
57        this->value_.double_ = 0.0;
58    else if (type == MT_longdouble)
59        this->value_.longdouble_ = 0.0;
60    else if (type == MT_bool)
61        this->value_.bool_ = false;
62    else
63        this->value_.void_ = 0;
64}
65
66bool MultiTypePrimitive::operator==(const MultiTypePrimitive& mtp) const
67{
68    if (this->type_ == mtp.type_)
69    {
70        if (this->type_ == MT_void)
71            return (this->value_.void_ == mtp.value_.void_);
72        else if (this->type_ == MT_int)
73            return (this->value_.int_ == mtp.value_.int_);
74        else if (this->type_ == MT_uint)
75            return (this->value_.uint_ == mtp.value_.uint_);
76        else if (this->type_ == MT_char)
77            return (this->value_.char_ == mtp.value_.char_);
78        else if (this->type_ == MT_uchar)
79            return (this->value_.uchar_ == mtp.value_.uchar_);
80        else if (this->type_ == MT_short)
81            return (this->value_.short_ == mtp.value_.short_);
82        else if (this->type_ == MT_ushort)
83            return (this->value_.ushort_ == mtp.value_.ushort_);
84        else if (this->type_ == MT_long)
85            return (this->value_.long_ == mtp.value_.long_);
86        else if (this->type_ == MT_ulong)
87            return (this->value_.ulong_ == mtp.value_.ulong_);
88        else if (this->type_ == MT_float)
89            return (this->value_.float_ == mtp.value_.float_);
90        else if (this->type_ == MT_double)
91            return (this->value_.double_ == mtp.value_.double_);
92        else if (this->type_ == MT_longdouble)
93            return (this->value_.longdouble_ == mtp.value_.longdouble_);
94        else if (this->type_ == MT_bool)
95            return (this->value_.bool_ == mtp.value_.bool_);
96    }
97
98    return false;
99}
100
101bool MultiTypePrimitive::operator!=(const MultiTypePrimitive& mtp) const
102{
103    if (this->type_ == mtp.type_)
104    {
105        if (this->type_ == MT_void)
106            return (this->value_.void_ != mtp.value_.void_);
107        else if (this->type_ == MT_int)
108            return (this->value_.int_ != mtp.value_.int_);
109        else if (this->type_ == MT_uint)
110            return (this->value_.uint_ != mtp.value_.uint_);
111        else if (this->type_ == MT_char)
112            return (this->value_.char_ != mtp.value_.char_);
113        else if (this->type_ == MT_uchar)
114            return (this->value_.uchar_ != mtp.value_.uchar_);
115        else if (this->type_ == MT_short)
116            return (this->value_.short_ != mtp.value_.short_);
117        else if (this->type_ == MT_ushort)
118            return (this->value_.ushort_ != mtp.value_.ushort_);
119        else if (this->type_ == MT_long)
120            return (this->value_.long_ != mtp.value_.long_);
121        else if (this->type_ == MT_ulong)
122            return (this->value_.ulong_ != mtp.value_.ulong_);
123        else if (this->type_ == MT_float)
124            return (this->value_.float_ != mtp.value_.float_);
125        else if (this->type_ == MT_double)
126            return (this->value_.double_ != mtp.value_.double_);
127        else if (this->type_ == MT_longdouble)
128            return (this->value_.longdouble_ != mtp.value_.longdouble_);
129        else if (this->type_ == MT_bool)
130            return (this->value_.bool_ != mtp.value_.bool_);
131    }
132
133    return true;
134}
135
136MultiTypePrimitive::operator orxonox::BaseObject*() const
137{ return (this->type_ == MT_void) ? (orxonox::BaseObject*)this->value_.void_ : (orxonox::BaseObject*)ConvertValueAndReturn<MultiTypePrimitive, void*>(*this); }
138MultiTypePrimitive::operator void*() const
139{ return (this->type_ == MT_void) ? this->value_.void_ : ConvertValueAndReturn<MultiTypePrimitive, void*>(*this); }
140MultiTypePrimitive::operator int() const
141{ return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypePrimitive, int>(*this); }
142MultiTypePrimitive::operator unsigned int() const
143{ return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned int>(*this); }
144MultiTypePrimitive::operator char() const
145{ return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypePrimitive, char>(*this); }
146MultiTypePrimitive::operator unsigned char() const
147{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned char>(*this); }
148MultiTypePrimitive::operator short() const
149{ return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypePrimitive, short>(*this); }
150MultiTypePrimitive::operator unsigned short() const
151{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned short>(*this); }
152MultiTypePrimitive::operator long() const
153{ return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypePrimitive, long>(*this); }
154MultiTypePrimitive::operator unsigned long() const
155{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned long>(*this); }
156MultiTypePrimitive::operator float() const
157{ return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypePrimitive, float>(*this); }
158MultiTypePrimitive::operator double() const
159{ return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypePrimitive, double>(*this); }
160MultiTypePrimitive::operator long double() const
161{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypePrimitive, long double>(*this); }
162MultiTypePrimitive::operator bool() const
163{ return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypePrimitive, bool>(*this); }
164
165void MultiTypePrimitive::setValue(const MultiTypePrimitive& mtp)
166{
167    this->type_ = mtp.type_;
168    this->value_ = mtp.value_;
169}
170
171std::string MultiTypePrimitive::toString() const
172{
173    std::string output;
174
175    if (this->type_ == MT_void)
176        ConvertValue(&output, this->value_.void_);
177    else if (this->type_ == MT_int)
178        ConvertValue(&output, this->value_.int_);
179    else if (this->type_ == MT_uint)
180        ConvertValue(&output, this->value_.uint_);
181    else if (this->type_ == MT_char)
182        ConvertValue(&output, this->value_.char_);
183    else if (this->type_ == MT_uchar)
184        ConvertValue(&output, this->value_.uchar_);
185    else if (this->type_ == MT_short)
186        ConvertValue(&output, this->value_.short_);
187    else if (this->type_ == MT_ushort)
188        ConvertValue(&output, this->value_.ushort_);
189    else if (this->type_ == MT_long)
190        ConvertValue(&output, this->value_.long_);
191    else if (this->type_ == MT_ulong)
192        ConvertValue(&output, this->value_.ulong_);
193    else if (this->type_ == MT_float)
194        ConvertValue(&output, this->value_.float_);
195    else if (this->type_ == MT_double)
196        ConvertValue(&output, this->value_.double_);
197    else if (this->type_ == MT_longdouble)
198        ConvertValue(&output, this->value_.longdouble_);
199    else if (this->type_ == MT_bool)
200        ConvertValue(&output, this->value_.bool_);
201
202    return output;
203}
204
205bool MultiTypePrimitive::fromString(const std::string value)
206{
207    if (this->type_ == MT_void)
208        return ConvertValue(&this->value_.void_, value, (void*)0);
209    else if (this->type_ == MT_int)
210        return ConvertValue(&this->value_.int_, value, (int)0);
211    else if (this->type_ == MT_uint)
212        return ConvertValue(&this->value_.uint_, value, (unsigned int)0);
213    else if (this->type_ == MT_char)
214        return ConvertValue(&this->value_.char_, value, (char)0);
215    else if (this->type_ == MT_uchar)
216        return ConvertValue(&this->value_.uchar_, value, (unsigned char)0);
217    else if (this->type_ == MT_short)
218        return ConvertValue(&this->value_.short_, value, (short)0);
219    else if (this->type_ == MT_ushort)
220        return ConvertValue(&this->value_.ushort_, value, (unsigned short)0);
221    else if (this->type_ == MT_long)
222        return ConvertValue(&this->value_.long_, value, (long)0);
223    else if (this->type_ == MT_ulong)
224        return ConvertValue(&this->value_.ulong_, value, (unsigned long)0);
225    else if (this->type_ == MT_float)
226        return ConvertValue(&this->value_.float_, value, (float)0.0);
227    else if (this->type_ == MT_double)
228        return ConvertValue(&this->value_.double_, value, (double)0.0);
229    else if (this->type_ == MT_longdouble)
230        return ConvertValue(&this->value_.longdouble_, value, (long double)0.0);
231    else if (this->type_ == MT_bool)
232        return ConvertValue(&this->value_.bool_, value, false);
233    else
234        return false;
235}
236
237std::ostream& operator<<(std::ostream& out, const MultiTypePrimitive& mtp)
238{
239    out << mtp.toString();
240    return out;
241}
Note: See TracBrowser for help on using the repository browser.