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 | |
---|
32 | MultiTypePrimitive::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 | |
---|
64 | bool MultiTypePrimitive::operator==(const MultiTypePrimitive& mtp) const |
---|
65 | { |
---|
66 | if (this->type_ == mtp.type_) |
---|
67 | { |
---|
68 | if (this->type_ == MT_int) |
---|
69 | return (this->value_.int_ == mtp.value_.int_); |
---|
70 | else if (this->type_ == MT_uint) |
---|
71 | return (this->value_.uint_ == mtp.value_.uint_); |
---|
72 | else if (this->type_ == MT_char) |
---|
73 | return (this->value_.char_ == mtp.value_.char_); |
---|
74 | else if (this->type_ == MT_uchar) |
---|
75 | return (this->value_.uchar_ == mtp.value_.uchar_); |
---|
76 | else if (this->type_ == MT_short) |
---|
77 | return (this->value_.short_ == mtp.value_.short_); |
---|
78 | else if (this->type_ == MT_ushort) |
---|
79 | return (this->value_.ushort_ == mtp.value_.ushort_); |
---|
80 | else if (this->type_ == MT_long) |
---|
81 | return (this->value_.long_ == mtp.value_.long_); |
---|
82 | else if (this->type_ == MT_ulong) |
---|
83 | return (this->value_.ulong_ == mtp.value_.ulong_); |
---|
84 | else if (this->type_ == MT_float) |
---|
85 | return (this->value_.float_ == mtp.value_.float_); |
---|
86 | else if (this->type_ == MT_double) |
---|
87 | return (this->value_.double_ == mtp.value_.double_); |
---|
88 | else if (this->type_ == MT_longdouble) |
---|
89 | return (this->value_.longdouble_ == mtp.value_.longdouble_); |
---|
90 | else if (this->type_ == MT_bool) |
---|
91 | return (this->value_.bool_ == mtp.value_.bool_); |
---|
92 | } |
---|
93 | |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | bool MultiTypePrimitive::operator!=(const MultiTypePrimitive& mtp) const |
---|
98 | { |
---|
99 | if (this->type_ == mtp.type_) |
---|
100 | { |
---|
101 | if (this->type_ == MT_int) |
---|
102 | return (this->value_.int_ != mtp.value_.int_); |
---|
103 | else if (this->type_ == MT_uint) |
---|
104 | return (this->value_.uint_ != mtp.value_.uint_); |
---|
105 | else if (this->type_ == MT_char) |
---|
106 | return (this->value_.char_ != mtp.value_.char_); |
---|
107 | else if (this->type_ == MT_uchar) |
---|
108 | return (this->value_.uchar_ != mtp.value_.uchar_); |
---|
109 | else if (this->type_ == MT_short) |
---|
110 | return (this->value_.short_ != mtp.value_.short_); |
---|
111 | else if (this->type_ == MT_ushort) |
---|
112 | return (this->value_.ushort_ != mtp.value_.ushort_); |
---|
113 | else if (this->type_ == MT_long) |
---|
114 | return (this->value_.long_ != mtp.value_.long_); |
---|
115 | else if (this->type_ == MT_ulong) |
---|
116 | return (this->value_.ulong_ != mtp.value_.ulong_); |
---|
117 | else if (this->type_ == MT_float) |
---|
118 | return (this->value_.float_ != mtp.value_.float_); |
---|
119 | else if (this->type_ == MT_double) |
---|
120 | return (this->value_.double_ != mtp.value_.double_); |
---|
121 | else if (this->type_ == MT_longdouble) |
---|
122 | return (this->value_.longdouble_ != mtp.value_.longdouble_); |
---|
123 | else if (this->type_ == MT_bool) |
---|
124 | return (this->value_.bool_ != mtp.value_.bool_); |
---|
125 | } |
---|
126 | |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | MultiTypePrimitive::operator int() const |
---|
131 | { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypePrimitive, int>(*this); } |
---|
132 | MultiTypePrimitive::operator unsigned int() const |
---|
133 | { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned int>(*this); } |
---|
134 | MultiTypePrimitive::operator char() const |
---|
135 | { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypePrimitive, char>(*this); } |
---|
136 | MultiTypePrimitive::operator unsigned char() const |
---|
137 | { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned char>(*this); } |
---|
138 | MultiTypePrimitive::operator short() const |
---|
139 | { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypePrimitive, short>(*this); } |
---|
140 | MultiTypePrimitive::operator unsigned short() const |
---|
141 | { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned short>(*this); } |
---|
142 | MultiTypePrimitive::operator long() const |
---|
143 | { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypePrimitive, long>(*this); } |
---|
144 | MultiTypePrimitive::operator unsigned long() const |
---|
145 | { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypePrimitive, unsigned long>(*this); } |
---|
146 | MultiTypePrimitive::operator float() const |
---|
147 | { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypePrimitive, float>(*this); } |
---|
148 | MultiTypePrimitive::operator double() const |
---|
149 | { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypePrimitive, double>(*this); } |
---|
150 | MultiTypePrimitive::operator long double() const |
---|
151 | { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypePrimitive, long double>(*this); } |
---|
152 | MultiTypePrimitive::operator bool() const |
---|
153 | { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypePrimitive, bool>(*this); } |
---|
154 | |
---|
155 | void MultiTypePrimitive::setValue(const MultiTypePrimitive& mtp) |
---|
156 | { |
---|
157 | this->type_ = mtp.type_; |
---|
158 | this->value_ = mtp.value_; |
---|
159 | } |
---|
160 | |
---|
161 | std::string MultiTypePrimitive::toString() const |
---|
162 | { |
---|
163 | std::string output; |
---|
164 | |
---|
165 | if (this->type_ == MT_int) |
---|
166 | ConvertValue(&output, this->value_.int_); |
---|
167 | else if (this->type_ == MT_uint) |
---|
168 | ConvertValue(&output, this->value_.uint_); |
---|
169 | else if (this->type_ == MT_char) |
---|
170 | ConvertValue(&output, this->value_.char_); |
---|
171 | else if (this->type_ == MT_uchar) |
---|
172 | ConvertValue(&output, this->value_.uchar_); |
---|
173 | else if (this->type_ == MT_short) |
---|
174 | ConvertValue(&output, this->value_.short_); |
---|
175 | else if (this->type_ == MT_ushort) |
---|
176 | ConvertValue(&output, this->value_.ushort_); |
---|
177 | else if (this->type_ == MT_long) |
---|
178 | ConvertValue(&output, this->value_.long_); |
---|
179 | else if (this->type_ == MT_ulong) |
---|
180 | ConvertValue(&output, this->value_.ulong_); |
---|
181 | else if (this->type_ == MT_float) |
---|
182 | ConvertValue(&output, this->value_.float_); |
---|
183 | else if (this->type_ == MT_double) |
---|
184 | ConvertValue(&output, this->value_.double_); |
---|
185 | else if (this->type_ == MT_longdouble) |
---|
186 | ConvertValue(&output, this->value_.longdouble_); |
---|
187 | else if (this->type_ == MT_bool) |
---|
188 | ConvertValue(&output, this->value_.bool_); |
---|
189 | |
---|
190 | return output; |
---|
191 | } |
---|
192 | |
---|
193 | bool MultiTypePrimitive::fromString(const std::string value) |
---|
194 | { |
---|
195 | if (this->type_ == MT_int) |
---|
196 | return ConvertValue(&this->value_.int_, value, (int)0); |
---|
197 | else if (this->type_ == MT_uint) |
---|
198 | return ConvertValue(&this->value_.uint_, value, (unsigned int)0); |
---|
199 | else if (this->type_ == MT_char) |
---|
200 | return ConvertValue(&this->value_.char_, value, (char)0); |
---|
201 | else if (this->type_ == MT_uchar) |
---|
202 | return ConvertValue(&this->value_.uchar_, value, (unsigned char)0); |
---|
203 | else if (this->type_ == MT_short) |
---|
204 | return ConvertValue(&this->value_.short_, value, (short)0); |
---|
205 | else if (this->type_ == MT_ushort) |
---|
206 | return ConvertValue(&this->value_.ushort_, value, (unsigned short)0); |
---|
207 | else if (this->type_ == MT_long) |
---|
208 | return ConvertValue(&this->value_.long_, value, (long)0); |
---|
209 | else if (this->type_ == MT_ulong) |
---|
210 | return ConvertValue(&this->value_.ulong_, value, (unsigned long)0); |
---|
211 | else if (this->type_ == MT_float) |
---|
212 | return ConvertValue(&this->value_.float_, value, (float)0.0); |
---|
213 | else if (this->type_ == MT_double) |
---|
214 | return ConvertValue(&this->value_.double_, value, (double)0.0); |
---|
215 | else if (this->type_ == MT_longdouble) |
---|
216 | return ConvertValue(&this->value_.longdouble_, value, (long double)0.0); |
---|
217 | else if (this->type_ == MT_bool) |
---|
218 | return ConvertValue(&this->value_.bool_, value, false); |
---|
219 | else |
---|
220 | return false; |
---|
221 | } |
---|
222 | |
---|
223 | std::ostream& operator<<(std::ostream& out, const MultiTypePrimitive& mtp) |
---|
224 | { |
---|
225 | out << mtp.toString(); |
---|
226 | return out; |
---|
227 | } |
---|