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