Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/util/MultiTypePrimitive.cc @ 848

Last change on this file since 848 was 848, checked in by landauf, 16 years ago

several changes:

  • XMLPort is now theoretically able to load something (but still buggy)
  • Expanded Convert with several partial template specializations
  • Expanded all MultiTypes with new functions, mostly to convert values
  • Expanded SubString with a new functionality: chars inside parentheses aren't split

It's not yet working as it should (at least not in all cases - loading the objects name works)

File size: 10.7 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_int)
37        this->value_.int_ = 0;
38    else if (type == MT_uint)
39        this->value_.uint_ = 0;
40    else if (type == MT_char)
41        this->value_.char_ = 0;
42    else if (type == MT_uchar)
43        this->value_.uchar_ = 0;
44    else if (type == MT_short)
45        this->value_.short_ = 0;
46    else if (type == MT_ushort)
47        this->value_.ushort_ = 0;
48    else if (type == MT_long)
49        this->value_.long_ = 0;
50    else if (type == MT_ulong)
51        this->value_.ulong_ = 0;
52    else if (type == MT_float)
53        this->value_.float_ = 0.0;
54    else if (type == MT_double)
55        this->value_.double_ = 0.0;
56    else if (type == MT_longdouble)
57        this->value_.longdouble_ = 0.0;
58    else if (type == MT_bool)
59        this->value_.bool_ = false;
60    else
61        this->value_.int_ = 0;
62}
63
64MultiTypePrimitive::MultiTypePrimitive(const MultiTypePrimitive& mtp)
65{
66    this->type_ = mtp.type_;
67    this->value_ = mtp.value_;
68}
69
70MultiTypePrimitive& MultiTypePrimitive::operator=(const MultiTypePrimitive& mtp)
71{
72    this->type_ = mtp.type_;
73    this->value_ = mtp.value_;
74    return *this;
75}
76
77bool MultiTypePrimitive::operator==(const MultiTypePrimitive& mtp) const
78{
79    if (this->type_ == mtp.type_)
80    {
81        if (this->type_ == MT_int)
82            return (this->value_.int_ == mtp.value_.int_);
83        else if (this->type_ == MT_uint)
84            return (this->value_.uint_ == mtp.value_.uint_);
85        else if (this->type_ == MT_char)
86            return (this->value_.char_ == mtp.value_.char_);
87        else if (this->type_ == MT_uchar)
88            return (this->value_.uchar_ == mtp.value_.uchar_);
89        else if (this->type_ == MT_short)
90            return (this->value_.short_ == mtp.value_.short_);
91        else if (this->type_ == MT_ushort)
92            return (this->value_.ushort_ == mtp.value_.ushort_);
93        else if (this->type_ == MT_long)
94            return (this->value_.long_ == mtp.value_.long_);
95        else if (this->type_ == MT_ulong)
96            return (this->value_.ulong_ == mtp.value_.ulong_);
97        else if (this->type_ == MT_float)
98            return (this->value_.float_ == mtp.value_.float_);
99        else if (this->type_ == MT_double)
100            return (this->value_.double_ == mtp.value_.double_);
101        else if (this->type_ == MT_longdouble)
102            return (this->value_.longdouble_ == mtp.value_.longdouble_);
103        else if (this->type_ == MT_bool)
104            return (this->value_.bool_ == mtp.value_.bool_);
105    }
106
107    return false;
108}
109
110bool MultiTypePrimitive::operator!=(const MultiTypePrimitive& mtp) const
111{
112    if (this->type_ == mtp.type_)
113    {
114        if (this->type_ == MT_int)
115            return (this->value_.int_ != mtp.value_.int_);
116        else if (this->type_ == MT_uint)
117            return (this->value_.uint_ != mtp.value_.uint_);
118        else if (this->type_ == MT_char)
119            return (this->value_.char_ != mtp.value_.char_);
120        else if (this->type_ == MT_uchar)
121            return (this->value_.uchar_ != mtp.value_.uchar_);
122        else if (this->type_ == MT_short)
123            return (this->value_.short_ != mtp.value_.short_);
124        else if (this->type_ == MT_ushort)
125            return (this->value_.ushort_ != mtp.value_.ushort_);
126        else if (this->type_ == MT_long)
127            return (this->value_.long_ != mtp.value_.long_);
128        else if (this->type_ == MT_ulong)
129            return (this->value_.ulong_ != mtp.value_.ulong_);
130        else if (this->type_ == MT_float)
131            return (this->value_.float_ != mtp.value_.float_);
132        else if (this->type_ == MT_double)
133            return (this->value_.double_ != mtp.value_.double_);
134        else if (this->type_ == MT_longdouble)
135            return (this->value_.longdouble_ != mtp.value_.longdouble_);
136        else if (this->type_ == MT_bool)
137            return (this->value_.bool_ != mtp.value_.bool_);
138    }
139
140    return true;
141}
142
143MultiTypePrimitive::operator int() const
144{
145    return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypePrimitive, int>(*this);
146}
147
148MultiTypePrimitive::operator unsigned int() const
149{
150    return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned int>(*this);
151}
152
153MultiTypePrimitive::operator char() const
154{
155    return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypePrimitive, char>(*this);
156}
157
158MultiTypePrimitive::operator unsigned char() const
159{
160    return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned char>(*this);
161}
162
163MultiTypePrimitive::operator short() const
164{
165    return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypePrimitive, short>(*this);
166}
167
168MultiTypePrimitive::operator unsigned short() const
169{
170    return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned short>(*this);
171}
172
173MultiTypePrimitive::operator long() const
174{
175    return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypePrimitive, long>(*this);
176}
177
178MultiTypePrimitive::operator unsigned long() const
179{
180    return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned long>(*this);
181}
182
183MultiTypePrimitive::operator float() const
184{
185    return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypePrimitive, float>(*this);
186}
187
188MultiTypePrimitive::operator double() const
189{
190    return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypePrimitive, double>(*this);
191}
192
193MultiTypePrimitive::operator long double() const
194{
195    return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypePrimitive, long double>(*this);
196}
197
198MultiTypePrimitive::operator bool() const
199{
200    return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypePrimitive, bool>(*this);
201}
202
203void MultiTypePrimitive::setValue(const MultiTypePrimitive& mtp)
204{
205    this->type_ = mtp.type_;
206    this->value_ = mtp.value_;
207}
208
209std::string MultiTypePrimitive::toString() const
210{
211    std::string output;
212
213    if (this->type_ == MT_int)
214        ConvertValue(&output, this->value_.int_);
215    else if (this->type_ == MT_uint)
216        ConvertValue(&output, this->value_.uint_);
217    else if (this->type_ == MT_char)
218        ConvertValue(&output, this->value_.char_);
219    else if (this->type_ == MT_uchar)
220        ConvertValue(&output, this->value_.uchar_);
221    else if (this->type_ == MT_short)
222        ConvertValue(&output, this->value_.short_);
223    else if (this->type_ == MT_ushort)
224        ConvertValue(&output, this->value_.ushort_);
225    else if (this->type_ == MT_long)
226        ConvertValue(&output, this->value_.long_);
227    else if (this->type_ == MT_ulong)
228        ConvertValue(&output, this->value_.ulong_);
229    else if (this->type_ == MT_float)
230        ConvertValue(&output, this->value_.float_);
231    else if (this->type_ == MT_double)
232        ConvertValue(&output, this->value_.double_);
233    else if (this->type_ == MT_longdouble)
234        ConvertValue(&output, this->value_.longdouble_);
235    else if (this->type_ == MT_bool)
236        ConvertValue(&output, this->value_.bool_);
237
238    return output;
239}
240
241bool MultiTypePrimitive::fromString(const std::string value)
242{
243    if (this->type_ == MT_int)
244        return ConvertValue(&this->value_.int_, value, (int)0);
245    else if (this->type_ == MT_uint)
246        return ConvertValue(&this->value_.uint_, value, (unsigned int)0);
247    else if (this->type_ == MT_char)
248        return ConvertValue(&this->value_.char_, value, (char)0);
249    else if (this->type_ == MT_uchar)
250        return ConvertValue(&this->value_.uchar_, value, (unsigned char)0);
251    else if (this->type_ == MT_short)
252        return ConvertValue(&this->value_.short_, value, (short)0);
253    else if (this->type_ == MT_ushort)
254        return ConvertValue(&this->value_.ushort_, value, (unsigned short)0);
255    else if (this->type_ == MT_long)
256        return ConvertValue(&this->value_.long_, value, (long)0);
257    else if (this->type_ == MT_ulong)
258        return ConvertValue(&this->value_.ulong_, value, (unsigned long)0);
259    else if (this->type_ == MT_float)
260        return ConvertValue(&this->value_.float_, value, (float)0.0);
261    else if (this->type_ == MT_double)
262        return ConvertValue(&this->value_.double_, value, (double)0.0);
263    else if (this->type_ == MT_longdouble)
264        return ConvertValue(&this->value_.longdouble_, value, (long double)0.0);
265    else if (this->type_ == MT_bool)
266        return ConvertValue(&this->value_.bool_, value, false);
267    else
268        return false;
269}
270
271std::ostream& operator<<(std::ostream& out, const MultiTypePrimitive& mtp)
272{
273    if (mtp.isA(MT_int))
274        out << mtp.getInt();
275    else if (mtp.isA(MT_uint))
276        out << mtp.getUnsignedInt();
277    else if (mtp.isA(MT_char))
278        out << mtp.getChar();
279    else if (mtp.isA(MT_uchar))
280        out << mtp.getUnsignedChar();
281    else if (mtp.isA(MT_short))
282        out << mtp.getShort();
283    else if (mtp.isA(MT_ushort))
284        out << mtp.getUnsignedShort();
285    else if (mtp.isA(MT_long))
286        out << mtp.getLong();
287    else if (mtp.isA(MT_ulong))
288        out << mtp.getUnsignedLong();
289    else if (mtp.isA(MT_float))
290        out << mtp.getFloat();
291    else if (mtp.isA(MT_double))
292        out << mtp.getDouble();
293    else if (mtp.isA(MT_longdouble))
294        out << mtp.getLongDouble();
295    else if (mtp.isA(MT_bool))
296        out << mtp.getBool();
297
298    return out;
299}
Note: See TracBrowser for help on using the repository browser.