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 | * Benjamin Grauer |
---|
23 | * Co-authors: |
---|
24 | * Fabian 'x3n' Landau |
---|
25 | */ |
---|
26 | |
---|
27 | /*! |
---|
28 | @file Convert.h |
---|
29 | @brief Definition and Implementation of the Convert class. |
---|
30 | */ |
---|
31 | |
---|
32 | #ifndef _Convert_H__ |
---|
33 | #define _Convert_H__ |
---|
34 | |
---|
35 | #include <string> |
---|
36 | #include <sstream> |
---|
37 | |
---|
38 | #include "UtilPrereqs.h" |
---|
39 | #include "Math.h" |
---|
40 | #include "SubString.h" |
---|
41 | #include "MultiTypeMath.h" |
---|
42 | |
---|
43 | // DEFAULT CLASS |
---|
44 | template <typename FromType, typename ToType> |
---|
45 | class Converter |
---|
46 | { |
---|
47 | public: |
---|
48 | bool operator()(ToType* output, const FromType& input) const |
---|
49 | { |
---|
50 | return false; |
---|
51 | } |
---|
52 | }; |
---|
53 | |
---|
54 | // PARTIAL SPECIALIZATION TO CONVERT TO STRINGS |
---|
55 | template<typename FromType> |
---|
56 | class Converter<FromType, std::string> |
---|
57 | { |
---|
58 | public: |
---|
59 | bool operator()(std::string* output, const FromType& input) const |
---|
60 | { |
---|
61 | std::ostringstream oss; |
---|
62 | if (oss << input) |
---|
63 | { |
---|
64 | (*output) = oss.str(); |
---|
65 | return true; |
---|
66 | } |
---|
67 | else |
---|
68 | return false; |
---|
69 | } |
---|
70 | }; |
---|
71 | |
---|
72 | // PARTIAL SPECIALIZATION TO CONVERT FROM STRING |
---|
73 | template<typename ToType> |
---|
74 | class Converter<std::string, ToType> |
---|
75 | { |
---|
76 | public: |
---|
77 | bool operator()(ToType* output, const std::string& input) const |
---|
78 | { |
---|
79 | std::istringstream iss(input); |
---|
80 | if (iss >> (*output)) |
---|
81 | return true; |
---|
82 | else |
---|
83 | return false; |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | // FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE |
---|
88 | template<typename FromType, typename ToType> |
---|
89 | static bool ConvertValue(ToType* output, const FromType& input) |
---|
90 | { |
---|
91 | Converter<FromType, ToType> converter; |
---|
92 | return converter(output, input); |
---|
93 | } |
---|
94 | |
---|
95 | // THE SAME, BUT WITH DEFAULT VALUE |
---|
96 | template<typename FromType, typename ToType> |
---|
97 | static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
98 | { |
---|
99 | Converter<FromType, ToType> converter; |
---|
100 | if (converter(output, input)) |
---|
101 | return true; |
---|
102 | |
---|
103 | (*output) = fallback; |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | // THE SAME LIKE BEFORE, BUT NEEDS NO POINTER TO THE OUTPUT |
---|
108 | template<typename FromType, typename ToType> |
---|
109 | static ToType ConvertValueAndReturn(const FromType& input) |
---|
110 | { |
---|
111 | ToType output = ToType(); |
---|
112 | ConvertValue(&output, input); |
---|
113 | return output; |
---|
114 | } |
---|
115 | |
---|
116 | // THE SAME, BUT WITH DEFAULT VALUE |
---|
117 | template<typename FromType, typename ToType> |
---|
118 | static ToType ConvertValueAndReturn(const FromType& input, const ToType& fallback) |
---|
119 | { |
---|
120 | ToType output = fallback; |
---|
121 | ConvertValue(&output, input, fallback); |
---|
122 | return output; |
---|
123 | } |
---|
124 | |
---|
125 | ////////////////////////// |
---|
126 | // MORE SPECIALISATIONS // |
---|
127 | ////////////////////////// |
---|
128 | |
---|
129 | // STRING TO STRING |
---|
130 | template<> |
---|
131 | class Converter<std::string, std::string> |
---|
132 | { |
---|
133 | public: |
---|
134 | bool operator()(std::string* output, const std::string& input) const |
---|
135 | { |
---|
136 | (*output) = std::string(input); |
---|
137 | return true; |
---|
138 | } |
---|
139 | }; |
---|
140 | |
---|
141 | //////////////// |
---|
142 | // MULTITYPES // |
---|
143 | //////////////// |
---|
144 | |
---|
145 | // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEPRIMITIVE |
---|
146 | template<typename ToType> |
---|
147 | class Converter<MultiTypePrimitive, ToType> |
---|
148 | { |
---|
149 | public: |
---|
150 | bool operator()(ToType* output, const MultiTypePrimitive& input) const |
---|
151 | { |
---|
152 | if (input.getType() == MT_void) |
---|
153 | return ConvertValue(output, input.getVoid()); |
---|
154 | else if (input.getType() == MT_int) |
---|
155 | return ConvertValue(output, input.getInt()); |
---|
156 | else if (input.getType() == MT_uint) |
---|
157 | return ConvertValue(output, input.getUnsignedInt()); |
---|
158 | else if (input.getType() == MT_char) |
---|
159 | return ConvertValue(output, input.getChar()); |
---|
160 | else if (input.getType() == MT_uchar) |
---|
161 | return ConvertValue(output, input.getUnsignedChar()); |
---|
162 | else if (input.getType() == MT_short) |
---|
163 | return ConvertValue(output, input.getShort()); |
---|
164 | else if (input.getType() == MT_ushort) |
---|
165 | return ConvertValue(output, input.getUnsignedShort()); |
---|
166 | else if (input.getType() == MT_long) |
---|
167 | return ConvertValue(output, input.getLong()); |
---|
168 | else if (input.getType() == MT_ulong) |
---|
169 | return ConvertValue(output, input.getUnsignedLong()); |
---|
170 | else if (input.getType() == MT_float) |
---|
171 | return ConvertValue(output, input.getFloat()); |
---|
172 | else if (input.getType() == MT_double) |
---|
173 | return ConvertValue(output, input.getDouble()); |
---|
174 | else if (input.getType() == MT_longdouble) |
---|
175 | return ConvertValue(output, input.getLongDouble()); |
---|
176 | else if (input.getType() == MT_bool) |
---|
177 | return ConvertValue(output, input.getBool()); |
---|
178 | else |
---|
179 | return false; |
---|
180 | } |
---|
181 | }; |
---|
182 | template<> |
---|
183 | class Converter<MultiTypePrimitive, std::string> |
---|
184 | { |
---|
185 | public: |
---|
186 | bool operator()(std::string* output, const MultiTypePrimitive& input) const |
---|
187 | { |
---|
188 | if (input.getType() == MT_void) |
---|
189 | return ConvertValue(output, input.getVoid()); |
---|
190 | else if (input.getType() == MT_int) |
---|
191 | return ConvertValue(output, input.getInt()); |
---|
192 | else if (input.getType() == MT_uint) |
---|
193 | return ConvertValue(output, input.getUnsignedInt()); |
---|
194 | else if (input.getType() == MT_char) |
---|
195 | return ConvertValue(output, input.getChar()); |
---|
196 | else if (input.getType() == MT_uchar) |
---|
197 | return ConvertValue(output, input.getUnsignedChar()); |
---|
198 | else if (input.getType() == MT_short) |
---|
199 | return ConvertValue(output, input.getShort()); |
---|
200 | else if (input.getType() == MT_ushort) |
---|
201 | return ConvertValue(output, input.getUnsignedShort()); |
---|
202 | else if (input.getType() == MT_long) |
---|
203 | return ConvertValue(output, input.getLong()); |
---|
204 | else if (input.getType() == MT_ulong) |
---|
205 | return ConvertValue(output, input.getUnsignedLong()); |
---|
206 | else if (input.getType() == MT_float) |
---|
207 | return ConvertValue(output, input.getFloat()); |
---|
208 | else if (input.getType() == MT_double) |
---|
209 | return ConvertValue(output, input.getDouble()); |
---|
210 | else if (input.getType() == MT_longdouble) |
---|
211 | return ConvertValue(output, input.getLongDouble()); |
---|
212 | else if (input.getType() == MT_bool) |
---|
213 | return ConvertValue(output, input.getBool()); |
---|
214 | else |
---|
215 | return false; |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPESTRING |
---|
220 | template<typename ToType> |
---|
221 | class Converter<MultiTypeString, ToType> |
---|
222 | { |
---|
223 | public: |
---|
224 | bool operator()(ToType* output, const MultiTypeString& input) const |
---|
225 | { |
---|
226 | if (input.getType() == MT_constchar) |
---|
227 | return ConvertValue(output, input.getConstChar()); |
---|
228 | else if (input.getType() == MT_string) |
---|
229 | return ConvertValue(output, input.getString()); |
---|
230 | else if (input.getType() == MT_xmlelement) |
---|
231 | return ConvertValue(output, input.getXMLElement()); |
---|
232 | else |
---|
233 | return ConvertValue(output, (MultiTypePrimitive)input); |
---|
234 | } |
---|
235 | }; |
---|
236 | template<> |
---|
237 | class Converter<MultiTypeString, std::string> |
---|
238 | { |
---|
239 | public: |
---|
240 | bool operator()(std::string* output, const MultiTypeString& input) const |
---|
241 | { |
---|
242 | if (input.getType() == MT_constchar) |
---|
243 | return ConvertValue(output, input.getConstChar()); |
---|
244 | else if (input.getType() == MT_string) |
---|
245 | return ConvertValue(output, input.getString()); |
---|
246 | else if (input.getType() == MT_xmlelement) |
---|
247 | return ConvertValue(output, input.getXMLElement()); |
---|
248 | else |
---|
249 | return ConvertValue(output, (MultiTypePrimitive)input); |
---|
250 | } |
---|
251 | }; |
---|
252 | |
---|
253 | // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEMATH |
---|
254 | template<typename ToType> |
---|
255 | class Converter<MultiTypeMath, ToType> |
---|
256 | { |
---|
257 | public: |
---|
258 | bool operator()(ToType* output, const MultiTypeMath& input) const |
---|
259 | { |
---|
260 | if (input.getType() == MT_vector2) |
---|
261 | return ConvertValue(output, input.getVector2()); |
---|
262 | else if (input.getType() == MT_vector3) |
---|
263 | return ConvertValue(output, input.getVector3()); |
---|
264 | else if (input.getType() == MT_quaternion) |
---|
265 | return ConvertValue(output, input.getQuaternion()); |
---|
266 | else if (input.getType() == MT_colourvalue) |
---|
267 | return ConvertValue(output, input.getColourValue()); |
---|
268 | else if (input.getType() == MT_radian) |
---|
269 | return ConvertValue(output, input.getRadian()); |
---|
270 | else if (input.getType() == MT_degree) |
---|
271 | return ConvertValue(output, input.getDegree()); |
---|
272 | else |
---|
273 | return ConvertValue(output, (MultiTypeString)input); |
---|
274 | } |
---|
275 | }; |
---|
276 | template<> |
---|
277 | class Converter<MultiTypeMath, std::string> |
---|
278 | { |
---|
279 | public: |
---|
280 | bool operator()(std::string* output, const MultiTypeMath& input) const |
---|
281 | { |
---|
282 | if (input.getType() == MT_vector2) |
---|
283 | return ConvertValue(output, input.getVector2()); |
---|
284 | else if (input.getType() == MT_vector3) |
---|
285 | return ConvertValue(output, input.getVector3()); |
---|
286 | else if (input.getType() == MT_quaternion) |
---|
287 | return ConvertValue(output, input.getQuaternion()); |
---|
288 | else if (input.getType() == MT_colourvalue) |
---|
289 | return ConvertValue(output, input.getColourValue()); |
---|
290 | else if (input.getType() == MT_radian) |
---|
291 | return ConvertValue(output, input.getRadian()); |
---|
292 | else if (input.getType() == MT_degree) |
---|
293 | return ConvertValue(output, input.getDegree()); |
---|
294 | else |
---|
295 | return ConvertValue(output, (MultiTypeString)input); |
---|
296 | } |
---|
297 | }; |
---|
298 | |
---|
299 | |
---|
300 | //////////////////// |
---|
301 | // MATH TO STRING // |
---|
302 | //////////////////// |
---|
303 | |
---|
304 | // Vector2 to std::string |
---|
305 | template <> |
---|
306 | class Converter<orxonox::Vector2, std::string> |
---|
307 | { |
---|
308 | public: |
---|
309 | bool operator()(std::string* output, const orxonox::Vector2& input) const |
---|
310 | { |
---|
311 | std::ostringstream ostream; |
---|
312 | if (ostream << input.x << "," << input.y) |
---|
313 | { |
---|
314 | (*output) = ostream.str(); |
---|
315 | return true; |
---|
316 | } |
---|
317 | |
---|
318 | return false; |
---|
319 | } |
---|
320 | }; |
---|
321 | |
---|
322 | // Vector3 to std::string |
---|
323 | template <> |
---|
324 | class Converter<orxonox::Vector3, std::string> |
---|
325 | { |
---|
326 | public: |
---|
327 | bool operator()(std::string* output, const orxonox::Vector3& input) const |
---|
328 | { |
---|
329 | std::ostringstream ostream; |
---|
330 | if (ostream << input.x << "," << input.y << "," << input.z) |
---|
331 | { |
---|
332 | (*output) = ostream.str(); |
---|
333 | return true; |
---|
334 | } |
---|
335 | |
---|
336 | return false; |
---|
337 | } |
---|
338 | }; |
---|
339 | |
---|
340 | // Vector4 to std::string |
---|
341 | template <> |
---|
342 | class Converter<orxonox::Vector4, std::string> |
---|
343 | { |
---|
344 | public: |
---|
345 | bool operator()(std::string* output, const orxonox::Vector4& input) const |
---|
346 | { |
---|
347 | std::ostringstream ostream; |
---|
348 | if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) |
---|
349 | { |
---|
350 | (*output) = ostream.str(); |
---|
351 | return true; |
---|
352 | } |
---|
353 | |
---|
354 | return false; |
---|
355 | } |
---|
356 | }; |
---|
357 | |
---|
358 | // Quaternion to std::string |
---|
359 | template <> |
---|
360 | class Converter<orxonox::Quaternion, std::string> |
---|
361 | { |
---|
362 | public: |
---|
363 | bool operator()(std::string* output, const orxonox::Quaternion& input) const |
---|
364 | { |
---|
365 | std::ostringstream ostream; |
---|
366 | if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) |
---|
367 | { |
---|
368 | (*output) = ostream.str(); |
---|
369 | return true; |
---|
370 | } |
---|
371 | |
---|
372 | return false; |
---|
373 | } |
---|
374 | }; |
---|
375 | |
---|
376 | // ColourValue to std::string |
---|
377 | template <> |
---|
378 | class Converter<orxonox::ColourValue, std::string> |
---|
379 | { |
---|
380 | public: |
---|
381 | bool operator()(std::string* output, const orxonox::ColourValue& input) const |
---|
382 | { |
---|
383 | std::ostringstream ostream; |
---|
384 | if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) |
---|
385 | { |
---|
386 | (*output) = ostream.str(); |
---|
387 | return true; |
---|
388 | } |
---|
389 | |
---|
390 | return false; |
---|
391 | } |
---|
392 | }; |
---|
393 | |
---|
394 | |
---|
395 | //////////////////// |
---|
396 | // STRING TO MATH // |
---|
397 | //////////////////// |
---|
398 | |
---|
399 | // std::string to Vector2 |
---|
400 | template <> |
---|
401 | class Converter<std::string, orxonox::Vector2> |
---|
402 | { |
---|
403 | public: |
---|
404 | bool operator()(orxonox::Vector2* output, const std::string& input) const |
---|
405 | { |
---|
406 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
407 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
408 | |
---|
409 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); |
---|
410 | |
---|
411 | if (tokens.size() >= 2) |
---|
412 | { |
---|
413 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
414 | return false; |
---|
415 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
416 | return false; |
---|
417 | |
---|
418 | return true; |
---|
419 | } |
---|
420 | |
---|
421 | return false; |
---|
422 | } |
---|
423 | }; |
---|
424 | |
---|
425 | // std::string to Vector3 |
---|
426 | template <> |
---|
427 | class Converter<std::string, orxonox::Vector3> |
---|
428 | { |
---|
429 | public: |
---|
430 | bool operator()(orxonox::Vector3* output, const std::string& input) const |
---|
431 | { |
---|
432 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
433 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
434 | |
---|
435 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); |
---|
436 | |
---|
437 | if (tokens.size() >= 3) |
---|
438 | { |
---|
439 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
440 | return false; |
---|
441 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
442 | return false; |
---|
443 | if (!ConvertValue(&(output->z), tokens[2])) |
---|
444 | return false; |
---|
445 | |
---|
446 | return true; |
---|
447 | } |
---|
448 | |
---|
449 | return false; |
---|
450 | } |
---|
451 | }; |
---|
452 | |
---|
453 | // std::string to Vector4 |
---|
454 | template <> |
---|
455 | class Converter<std::string, orxonox::Vector4> |
---|
456 | { |
---|
457 | public: |
---|
458 | bool operator()(orxonox::Vector4* output, const std::string& input) const |
---|
459 | { |
---|
460 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
461 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
462 | |
---|
463 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); |
---|
464 | |
---|
465 | if (tokens.size() >= 4) |
---|
466 | { |
---|
467 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
468 | return false; |
---|
469 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
470 | return false; |
---|
471 | if (!ConvertValue(&(output->z), tokens[2])) |
---|
472 | return false; |
---|
473 | if (!ConvertValue(&(output->w), tokens[3])) |
---|
474 | return false; |
---|
475 | |
---|
476 | return true; |
---|
477 | } |
---|
478 | |
---|
479 | return false; |
---|
480 | } |
---|
481 | }; |
---|
482 | |
---|
483 | // std::string to Quaternion |
---|
484 | template <> |
---|
485 | class Converter<std::string, orxonox::Quaternion> |
---|
486 | { |
---|
487 | public: |
---|
488 | bool operator()(orxonox::Quaternion* output, const std::string& input) const |
---|
489 | { |
---|
490 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
491 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
492 | |
---|
493 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); |
---|
494 | |
---|
495 | if (tokens.size() >= 4) |
---|
496 | { |
---|
497 | if (!ConvertValue(&(output->w), tokens[0])) |
---|
498 | return false; |
---|
499 | if (!ConvertValue(&(output->x), tokens[1])) |
---|
500 | return false; |
---|
501 | if (!ConvertValue(&(output->y), tokens[2])) |
---|
502 | return false; |
---|
503 | if (!ConvertValue(&(output->z), tokens[3])) |
---|
504 | return false; |
---|
505 | |
---|
506 | return true; |
---|
507 | } |
---|
508 | |
---|
509 | return false; |
---|
510 | } |
---|
511 | }; |
---|
512 | |
---|
513 | // std::string to ColourValue |
---|
514 | template <> |
---|
515 | class Converter<std::string, orxonox::ColourValue> |
---|
516 | { |
---|
517 | public: |
---|
518 | bool operator()(orxonox::ColourValue* output, const std::string& input) const |
---|
519 | { |
---|
520 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
521 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
522 | |
---|
523 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); |
---|
524 | |
---|
525 | if (tokens.size() >= 4) |
---|
526 | { |
---|
527 | if (!ConvertValue(&(output->r), tokens[0])) |
---|
528 | return false; |
---|
529 | if (!ConvertValue(&(output->g), tokens[1])) |
---|
530 | return false; |
---|
531 | if (!ConvertValue(&(output->b), tokens[2])) |
---|
532 | return false; |
---|
533 | if (!ConvertValue(&(output->a), tokens[3])) |
---|
534 | return false; |
---|
535 | |
---|
536 | return true; |
---|
537 | } |
---|
538 | |
---|
539 | return false; |
---|
540 | } |
---|
541 | }; |
---|
542 | |
---|
543 | #endif /* _Convert_H__ */ |
---|