[1052] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[1052] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Benjamin Grauer |
---|
| 24 | * Fabian 'x3n' Landau |
---|
[726] | 25 | * Co-authors: |
---|
[1052] | 26 | * ... |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /*! |
---|
| 30 | @file Convert.h |
---|
| 31 | @brief Definition and Implementation of the Convert class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #ifndef _Convert_H__ |
---|
| 35 | #define _Convert_H__ |
---|
| 36 | |
---|
[1062] | 37 | #include "UtilPrereqs.h" |
---|
| 38 | |
---|
[1052] | 39 | #include <string> |
---|
| 40 | #include <sstream> |
---|
| 41 | |
---|
| 42 | #include "Math.h" |
---|
| 43 | #include "SubString.h" |
---|
| 44 | #include "MultiTypeMath.h" |
---|
[1064] | 45 | |
---|
| 46 | // disable annoying warning about forcing value to boolean |
---|
| 47 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC |
---|
| 48 | #pragma warning(push) |
---|
| 49 | #pragma warning(disable:4100 4800) |
---|
| 50 | #endif |
---|
[721] | 51 | |
---|
| 52 | |
---|
[1052] | 53 | ////////// |
---|
| 54 | // MAIN // |
---|
| 55 | ////////// |
---|
[721] | 56 | |
---|
[1052] | 57 | // Enum to declare the wanted conversion preference in case of equal type-levels |
---|
| 58 | enum ConversionPreference |
---|
| 59 | { |
---|
| 60 | CP_PreferToType, |
---|
| 61 | CP_PreferFromType, |
---|
| 62 | }; |
---|
[721] | 63 | |
---|
[1052] | 64 | // Helper classes to determine the preferred partial template specialization |
---|
| 65 | class _ToType_ {}; |
---|
| 66 | class _FromType_ {}; |
---|
| 67 | class _Explicit_ {}; |
---|
[726] | 68 | |
---|
[871] | 69 | |
---|
[1052] | 70 | // The default convert functions |
---|
| 71 | template <class FromType, class ToType, class Type> |
---|
| 72 | struct ConverterSpecialized |
---|
[721] | 73 | { |
---|
[1052] | 74 | enum { specialized = false }; |
---|
| 75 | static bool convert(ToType* output, const FromType& input) |
---|
| 76 | { return false; } |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | // The default convert function if both types are the same |
---|
| 81 | template <class BothTypes> |
---|
| 82 | struct ConverterSpecialized<BothTypes, BothTypes, _Explicit_> |
---|
| 83 | { |
---|
| 84 | enum { specialized = true }; |
---|
| 85 | static bool convert(BothTypes* output, const BothTypes& input) |
---|
| 86 | { (*output) = input; return true; } |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | // The possible levels |
---|
| 91 | #define __low__ 0 // Everything that is or behaves like a primitive type (an can be converted with a typecast to every other low-level type) |
---|
| 92 | #define __mid__ 1 // Everything that has overloaded << and >> operators to operate on a std::stream |
---|
| 93 | #define __high__ 2 // Everything that doesn't fullfill the lowerlevel-requirements and therefore needs specialized conversions |
---|
| 94 | |
---|
| 95 | // Defines the levels of all types: Default is __high__ so you don't have to define every high-level type |
---|
| 96 | template <class T> struct ConverterLevel { enum { level = __high__ }; }; |
---|
| 97 | template <> struct ConverterLevel<std::string> { enum { level = __mid__ }; }; |
---|
| 98 | template <> struct ConverterLevel<orxonox::Radian> { enum { level = __mid__ }; }; |
---|
| 99 | template <> struct ConverterLevel<orxonox::Degree> { enum { level = __mid__ }; }; |
---|
| 100 | template <> struct ConverterLevel<int> { enum { level = __low__ }; }; |
---|
| 101 | template <> struct ConverterLevel<unsigned int> { enum { level = __low__ }; }; |
---|
| 102 | template <> struct ConverterLevel<char> { enum { level = __low__ }; }; |
---|
| 103 | template <> struct ConverterLevel<unsigned char> { enum { level = __low__ }; }; |
---|
| 104 | template <> struct ConverterLevel<short> { enum { level = __low__ }; }; |
---|
| 105 | template <> struct ConverterLevel<unsigned short> { enum { level = __low__ }; }; |
---|
| 106 | template <> struct ConverterLevel<long> { enum { level = __low__ }; }; |
---|
| 107 | template <> struct ConverterLevel<unsigned long> { enum { level = __low__ }; }; |
---|
| 108 | template <> struct ConverterLevel<float> { enum { level = __low__ }; }; |
---|
| 109 | template <> struct ConverterLevel<double> { enum { level = __low__ }; }; |
---|
| 110 | template <> struct ConverterLevel<long double> { enum { level = __low__ }; }; |
---|
| 111 | template <> struct ConverterLevel<bool> { enum { level = __low__ }; }; |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | // Calculates the preference based on the levels of FromType and ToType |
---|
| 115 | template <int from, int to> |
---|
| 116 | struct ConverterPreference |
---|
| 117 | { |
---|
| 118 | enum |
---|
[871] | 119 | { |
---|
[1052] | 120 | // The maximum of both levels: element of {0, 1, 2} |
---|
| 121 | // max 0: Both types are primitives or have a similar behaviour |
---|
| 122 | // max 1: At least one type is not a primitive, but both can be put on a std::stream |
---|
| 123 | // max 2: There is at least one generic type that needs specialized conversions |
---|
| 124 | max = (from > to) ? from : to, |
---|
| 125 | |
---|
| 126 | // The difference between both levels limited to +-1: element of {-1, 0, 1} |
---|
| 127 | // diff -1: The FromType has higher level than the ToType |
---|
| 128 | // diff 0: Both types have the same level |
---|
| 129 | // diff 1: The ToType has higher level than the FromType |
---|
| 130 | diff = ((to - from) > 1) ? 1 : (((to - from) < -1) ? -1 : to - from) |
---|
| 131 | }; |
---|
[726] | 132 | }; |
---|
[721] | 133 | |
---|
[1052] | 134 | |
---|
| 135 | // The default conversion: This usually does nothing |
---|
| 136 | template <int max, class FromType, class ToType> |
---|
| 137 | struct ConverterDefault |
---|
[726] | 138 | { |
---|
[1052] | 139 | static bool convert(ToType* output, const FromType& input) |
---|
[721] | 140 | { |
---|
[871] | 141 | return false; |
---|
[721] | 142 | } |
---|
[726] | 143 | }; |
---|
[1052] | 144 | // The default conversion for primitives: A typecast (defined over two partial specialized templates to exclude all non-primitive types and classes) template <int max, class FromType, class ToType> |
---|
| 145 | template <class FromType, class ToType> |
---|
| 146 | struct ConverterDefault<0, FromType, ToType> |
---|
| 147 | { |
---|
| 148 | static bool convert(ToType* output, const FromType& input) |
---|
| 149 | { |
---|
| 150 | (*output) = (ToType)input; |
---|
| 151 | return true; |
---|
| 152 | } |
---|
| 153 | }; |
---|
[721] | 154 | |
---|
[1052] | 155 | |
---|
| 156 | // Converter: Converts input of FromType into output of ToType |
---|
| 157 | template <int diff, int max, class FromType, class ToType, ConversionPreference pref> |
---|
| 158 | struct Converter |
---|
[726] | 159 | { |
---|
[1052] | 160 | static bool convert(ToType* output, const FromType& input) |
---|
[871] | 161 | { |
---|
| 162 | return false; |
---|
| 163 | } |
---|
[726] | 164 | }; |
---|
[1052] | 165 | // Converter: level{FromType} > level{ToType} |
---|
| 166 | template <int max, class FromType, class ToType, ConversionPreference pref> |
---|
| 167 | struct Converter<-1, max, FromType, ToType, pref> |
---|
| 168 | { static bool convert(ToType* output, const FromType& input) |
---|
| 169 | { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; |
---|
| 170 | // Converter: level{FromType} < level{ToType} |
---|
| 171 | template <int max, class FromType, class ToType, ConversionPreference pref> |
---|
| 172 | struct Converter<1, max, FromType, ToType, pref> |
---|
| 173 | { static bool convert(ToType* output, const FromType& input) |
---|
| 174 | { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; |
---|
| 175 | // Converter: level{FromType} = level{ToType} |
---|
| 176 | // CP_PreferToType |
---|
| 177 | template <int max, class FromType, class ToType> |
---|
| 178 | struct Converter<0, max, FromType, ToType, CP_PreferToType> |
---|
| 179 | { static bool convert(ToType* output, const FromType& input) |
---|
| 180 | { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; |
---|
| 181 | // CP_PreferFromType |
---|
| 182 | template <int max, class FromType, class ToType> |
---|
| 183 | struct Converter<0, max, FromType, ToType, CP_PreferFromType> |
---|
| 184 | { static bool convert(ToType* output, const FromType& input) |
---|
| 185 | { return (ConverterSpecialized<FromType, ToType, _Explicit_>::specialized) ? (ConverterSpecialized<FromType, ToType, _Explicit_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _FromType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _FromType_>::convert(output, input)) : (ConverterSpecialized<FromType, ToType, _ToType_>::specialized) ? (ConverterSpecialized<FromType, ToType, _ToType_>::convert(output, input)) : (ConverterDefault<max, FromType, ToType>::convert(output, input)); } }; |
---|
[721] | 186 | |
---|
[1052] | 187 | |
---|
| 188 | // Calls the Converter::convertValue function with the correct template type parameters calculated by ConverterPreference |
---|
| 189 | template <class FromType, class ToType> |
---|
| 190 | static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) |
---|
[726] | 191 | { |
---|
[1052] | 192 | return (preference == CP_PreferToType) ? |
---|
| 193 | Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, |
---|
| 194 | ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, |
---|
| 195 | FromType, |
---|
| 196 | ToType, |
---|
| 197 | CP_PreferToType>::convert(output, input) |
---|
| 198 | : Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, |
---|
| 199 | ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, |
---|
| 200 | FromType, |
---|
| 201 | ToType, |
---|
| 202 | CP_PreferFromType>::convert(output, input); |
---|
[726] | 203 | } |
---|
[721] | 204 | |
---|
[1052] | 205 | |
---|
| 206 | ////////////////////// |
---|
| 207 | // HELPER FUNCTIONS // |
---|
| 208 | ////////////////////// |
---|
| 209 | |
---|
| 210 | // Helper function: Calls convertValue with and without default value and returns true if the conversion was successful |
---|
| 211 | template<class FromType, class ToType> |
---|
| 212 | static bool ConvertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) |
---|
[726] | 213 | { |
---|
[1052] | 214 | return convertValue(output, input, preference); |
---|
| 215 | } |
---|
| 216 | template<class FromType, class ToType> |
---|
| 217 | static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) |
---|
| 218 | { |
---|
| 219 | if (convertValue(output, input, preference)) |
---|
| 220 | return true; |
---|
[721] | 221 | |
---|
[1052] | 222 | (*output) = fallback; |
---|
| 223 | return false; |
---|
[726] | 224 | } |
---|
[721] | 225 | |
---|
[1052] | 226 | // Helper function: Calls convertValue with and without default value and returns the converted value |
---|
| 227 | template<class FromType, class ToType> |
---|
| 228 | static ToType getConvertedValue(const FromType& input, ConversionPreference preference = CP_PreferToType) |
---|
[871] | 229 | { |
---|
[1052] | 230 | ToType output = ToType(); |
---|
| 231 | ConvertValue(&output, input, preference); |
---|
| 232 | return output; |
---|
[871] | 233 | } |
---|
[1052] | 234 | template<class FromType, class ToType> |
---|
| 235 | static ToType getConvertedValue(const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) |
---|
[871] | 236 | { |
---|
[1052] | 237 | ToType output = fallback; |
---|
| 238 | ConvertValue(&output, input, fallback, preference); |
---|
| 239 | return output; |
---|
[871] | 240 | } |
---|
| 241 | |
---|
[1052] | 242 | |
---|
| 243 | ///////////////////// |
---|
| 244 | // SPECIALIZATIONS // |
---|
| 245 | ///////////////////// |
---|
[871] | 246 | |
---|
[1052] | 247 | ///////////// |
---|
| 248 | // SAMPLES // |
---|
| 249 | ///////////// |
---|
| 250 | /* |
---|
| 251 | // convert everything to xyz |
---|
| 252 | template <class FromType> |
---|
| 253 | struct ConverterSpecialized<FromType, xyz, _ToType_> |
---|
[871] | 254 | { |
---|
[1052] | 255 | enum { specialized = true }; |
---|
| 256 | static bool convert(xyz* output, const FromType& input) |
---|
| 257 | { return ...; } |
---|
[871] | 258 | }; |
---|
| 259 | |
---|
[1052] | 260 | // convert xyz to everything |
---|
| 261 | template <class ToType> |
---|
| 262 | struct ConverterSpecialized<xyz, ToType, _FromType_> |
---|
| 263 | { |
---|
| 264 | enum { specialized = true }; |
---|
| 265 | static bool convert(ToType* output, const xyz& input) |
---|
| 266 | { return ...; } |
---|
| 267 | }; |
---|
[871] | 268 | |
---|
[1052] | 269 | // convert abc to xyz |
---|
| 270 | template <> |
---|
| 271 | struct ConverterSpecialized<abc, xyz, _Explicit_> |
---|
[871] | 272 | { |
---|
[1052] | 273 | enum { specialized = true }; |
---|
| 274 | static bool convert(xyz* output, const abc& input) |
---|
| 275 | { return ...; } |
---|
[871] | 276 | }; |
---|
[1052] | 277 | */ |
---|
| 278 | |
---|
| 279 | //////////// |
---|
| 280 | // STRING // |
---|
| 281 | //////////// |
---|
| 282 | |
---|
| 283 | // convert to string |
---|
| 284 | template <class FromType> |
---|
| 285 | struct ConverterSpecialized<FromType, std::string, _ToType_> |
---|
[871] | 286 | { |
---|
[1052] | 287 | enum { specialized = true }; |
---|
| 288 | static bool convert(std::string* output, const FromType& input) |
---|
[871] | 289 | { |
---|
[1052] | 290 | std::ostringstream oss; |
---|
| 291 | if (oss << input) |
---|
| 292 | { |
---|
| 293 | (*output) = oss.str(); |
---|
| 294 | return true; |
---|
| 295 | } |
---|
| 296 | else |
---|
| 297 | return false; |
---|
[871] | 298 | } |
---|
| 299 | }; |
---|
| 300 | |
---|
[1052] | 301 | // convert from string |
---|
| 302 | template <class ToType> |
---|
| 303 | struct ConverterSpecialized<std::string, ToType, _FromType_> |
---|
[871] | 304 | { |
---|
[1052] | 305 | enum { specialized = true }; |
---|
| 306 | static bool convert(ToType* output, const std::string& input) |
---|
[871] | 307 | { |
---|
[1052] | 308 | std::istringstream iss(input); |
---|
| 309 | if (iss >> (*output)) |
---|
| 310 | return true; |
---|
| 311 | else |
---|
| 312 | return false; |
---|
[871] | 313 | } |
---|
| 314 | }; |
---|
[1052] | 315 | |
---|
| 316 | |
---|
| 317 | //////////////// |
---|
| 318 | // MULTITYPES // |
---|
| 319 | //////////////// |
---|
| 320 | |
---|
| 321 | // convert from MultiTypePrimitive |
---|
| 322 | template <class ToType> |
---|
| 323 | struct ConverterSpecialized<MultiTypePrimitive, ToType, _FromType_> |
---|
[871] | 324 | { |
---|
[1052] | 325 | enum { specialized = true }; |
---|
| 326 | static bool convert(ToType* output, const MultiTypePrimitive& input) |
---|
[871] | 327 | { |
---|
[1052] | 328 | if (input.getType() == MT_void) |
---|
| 329 | return ConvertValue(output, input.getVoid()); |
---|
| 330 | else if (input.getType() == MT_int) |
---|
| 331 | return ConvertValue(output, input.getInt()); |
---|
| 332 | else if (input.getType() == MT_uint) |
---|
| 333 | return ConvertValue(output, input.getUnsignedInt()); |
---|
| 334 | else if (input.getType() == MT_char) |
---|
| 335 | return ConvertValue(output, input.getChar()); |
---|
| 336 | else if (input.getType() == MT_uchar) |
---|
| 337 | return ConvertValue(output, input.getUnsignedChar()); |
---|
| 338 | else if (input.getType() == MT_short) |
---|
| 339 | return ConvertValue(output, input.getShort()); |
---|
| 340 | else if (input.getType() == MT_ushort) |
---|
| 341 | return ConvertValue(output, input.getUnsignedShort()); |
---|
| 342 | else if (input.getType() == MT_long) |
---|
| 343 | return ConvertValue(output, input.getLong()); |
---|
| 344 | else if (input.getType() == MT_ulong) |
---|
| 345 | return ConvertValue(output, input.getUnsignedLong()); |
---|
| 346 | else if (input.getType() == MT_float) |
---|
| 347 | return ConvertValue(output, input.getFloat()); |
---|
| 348 | else if (input.getType() == MT_double) |
---|
| 349 | return ConvertValue(output, input.getDouble()); |
---|
| 350 | else if (input.getType() == MT_longdouble) |
---|
| 351 | return ConvertValue(output, input.getLongDouble()); |
---|
| 352 | else if (input.getType() == MT_bool) |
---|
| 353 | return ConvertValue(output, input.getBool()); |
---|
| 354 | else |
---|
| 355 | return false; |
---|
[871] | 356 | } |
---|
| 357 | }; |
---|
| 358 | |
---|
[1052] | 359 | // convert from MultiTypeString |
---|
| 360 | template <class ToType> |
---|
| 361 | struct ConverterSpecialized<MultiTypeString, ToType, _FromType_> |
---|
[871] | 362 | { |
---|
[1052] | 363 | enum { specialized = true }; |
---|
| 364 | static bool convert(ToType* output, const MultiTypeString& input) |
---|
[871] | 365 | { |
---|
[1052] | 366 | if (input.getType() == MT_constchar) |
---|
| 367 | return ConvertValue(output, input.getConstChar()); |
---|
| 368 | else if (input.getType() == MT_string) |
---|
| 369 | return ConvertValue(output, input.getString()); |
---|
| 370 | else |
---|
| 371 | return ConvertValue(output, (MultiTypePrimitive)input); |
---|
[871] | 372 | } |
---|
| 373 | }; |
---|
[1052] | 374 | |
---|
| 375 | // convert from MultiTypeMath |
---|
| 376 | template <class ToType> |
---|
| 377 | struct ConverterSpecialized<MultiTypeMath, ToType, _FromType_> |
---|
[871] | 378 | { |
---|
[1052] | 379 | enum { specialized = true }; |
---|
| 380 | static bool convert(ToType* output, const MultiTypeMath& input) |
---|
[871] | 381 | { |
---|
[1052] | 382 | if (input.getType() == MT_vector2) |
---|
| 383 | return ConvertValue(output, input.getVector2()); |
---|
| 384 | else if (input.getType() == MT_vector3) |
---|
| 385 | return ConvertValue(output, input.getVector3()); |
---|
| 386 | else if (input.getType() == MT_quaternion) |
---|
| 387 | return ConvertValue(output, input.getQuaternion()); |
---|
| 388 | else if (input.getType() == MT_colourvalue) |
---|
| 389 | return ConvertValue(output, input.getColourValue()); |
---|
| 390 | else if (input.getType() == MT_radian) |
---|
| 391 | return ConvertValue(output, input.getRadian()); |
---|
| 392 | else if (input.getType() == MT_degree) |
---|
| 393 | return ConvertValue(output, input.getDegree()); |
---|
| 394 | else |
---|
| 395 | return ConvertValue(output, (MultiTypeString)input); |
---|
[871] | 396 | } |
---|
| 397 | }; |
---|
| 398 | |
---|
[1052] | 399 | |
---|
| 400 | //////////////////// |
---|
| 401 | // MATH TO STRING // |
---|
| 402 | //////////////////// |
---|
[871] | 403 | |
---|
| 404 | // Vector2 to std::string |
---|
| 405 | template <> |
---|
[1052] | 406 | struct ConverterSpecialized<orxonox::Vector2, std::string, _Explicit_> |
---|
[871] | 407 | { |
---|
[1052] | 408 | enum { specialized = true }; |
---|
| 409 | static bool convert(std::string* output, const orxonox::Vector2& input) |
---|
[871] | 410 | { |
---|
[1052] | 411 | std::ostringstream ostream; |
---|
| 412 | if (ostream << input.x << "," << input.y) |
---|
| 413 | { |
---|
| 414 | (*output) = ostream.str(); |
---|
| 415 | return true; |
---|
| 416 | } |
---|
| 417 | return false; |
---|
[871] | 418 | } |
---|
| 419 | }; |
---|
| 420 | |
---|
| 421 | // Vector3 to std::string |
---|
| 422 | template <> |
---|
[1052] | 423 | struct ConverterSpecialized<orxonox::Vector3, std::string, _Explicit_> |
---|
[871] | 424 | { |
---|
[1052] | 425 | enum { specialized = true }; |
---|
| 426 | static bool convert(std::string* output, const orxonox::Vector3& input) |
---|
[871] | 427 | { |
---|
[1052] | 428 | std::ostringstream ostream; |
---|
| 429 | if (ostream << input.x << "," << input.y << "," << input.z) |
---|
| 430 | { |
---|
| 431 | (*output) = ostream.str(); |
---|
| 432 | return true; |
---|
| 433 | } |
---|
| 434 | return false; |
---|
[871] | 435 | } |
---|
| 436 | }; |
---|
| 437 | |
---|
| 438 | // Vector4 to std::string |
---|
| 439 | template <> |
---|
[1052] | 440 | struct ConverterSpecialized<orxonox::Vector4, std::string, _Explicit_> |
---|
[871] | 441 | { |
---|
[1052] | 442 | enum { specialized = true }; |
---|
| 443 | static bool convert(std::string* output, const orxonox::Vector4& input) |
---|
[871] | 444 | { |
---|
[1052] | 445 | std::ostringstream ostream; |
---|
| 446 | if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) |
---|
| 447 | { |
---|
| 448 | (*output) = ostream.str(); |
---|
| 449 | return true; |
---|
| 450 | } |
---|
| 451 | return false; |
---|
[871] | 452 | } |
---|
| 453 | }; |
---|
| 454 | |
---|
| 455 | // Quaternion to std::string |
---|
| 456 | template <> |
---|
[1052] | 457 | struct ConverterSpecialized<orxonox::Quaternion, std::string, _Explicit_> |
---|
[871] | 458 | { |
---|
[1052] | 459 | enum { specialized = true }; |
---|
| 460 | static bool convert(std::string* output, const orxonox::Quaternion& input) |
---|
[871] | 461 | { |
---|
[1052] | 462 | std::ostringstream ostream; |
---|
| 463 | if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) |
---|
| 464 | { |
---|
| 465 | (*output) = ostream.str(); |
---|
| 466 | return true; |
---|
| 467 | } |
---|
| 468 | return false; |
---|
[871] | 469 | } |
---|
| 470 | }; |
---|
| 471 | |
---|
| 472 | // ColourValue to std::string |
---|
| 473 | template <> |
---|
[1052] | 474 | struct ConverterSpecialized<orxonox::ColourValue, std::string, _Explicit_> |
---|
[871] | 475 | { |
---|
[1052] | 476 | enum { specialized = true }; |
---|
| 477 | static bool convert(std::string* output, const orxonox::ColourValue& input) |
---|
[871] | 478 | { |
---|
[1052] | 479 | std::ostringstream ostream; |
---|
| 480 | if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) |
---|
| 481 | { |
---|
| 482 | (*output) = ostream.str(); |
---|
| 483 | return true; |
---|
| 484 | } |
---|
| 485 | return false; |
---|
[871] | 486 | } |
---|
| 487 | }; |
---|
| 488 | |
---|
| 489 | |
---|
[1052] | 490 | //////////////////// |
---|
| 491 | // STRING TO MATH // |
---|
| 492 | //////////////////// |
---|
[871] | 493 | |
---|
| 494 | // std::string to Vector2 |
---|
| 495 | template <> |
---|
[1052] | 496 | struct ConverterSpecialized<std::string, orxonox::Vector2, _Explicit_> |
---|
[871] | 497 | { |
---|
[1052] | 498 | enum { specialized = true }; |
---|
| 499 | static bool convert(orxonox::Vector2* output, const std::string& input) |
---|
[871] | 500 | { |
---|
[1052] | 501 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 502 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[871] | 503 | |
---|
[1052] | 504 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 505 | if (tokens.size() >= 2) |
---|
| 506 | { |
---|
| 507 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
| 508 | return false; |
---|
| 509 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
| 510 | return false; |
---|
[871] | 511 | |
---|
[1052] | 512 | return true; |
---|
| 513 | } |
---|
| 514 | return false; |
---|
[871] | 515 | } |
---|
| 516 | }; |
---|
| 517 | |
---|
| 518 | // std::string to Vector3 |
---|
| 519 | template <> |
---|
[1052] | 520 | struct ConverterSpecialized<std::string, orxonox::Vector3, _Explicit_> |
---|
[871] | 521 | { |
---|
[1052] | 522 | enum { specialized = true }; |
---|
| 523 | static bool convert(orxonox::Vector3* output, const std::string& input) |
---|
[871] | 524 | { |
---|
[1052] | 525 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 526 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[871] | 527 | |
---|
[1052] | 528 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 529 | if (tokens.size() >= 3) |
---|
| 530 | { |
---|
| 531 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
| 532 | return false; |
---|
| 533 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
| 534 | return false; |
---|
| 535 | if (!ConvertValue(&(output->z), tokens[2])) |
---|
| 536 | return false; |
---|
[871] | 537 | |
---|
[1052] | 538 | return true; |
---|
| 539 | } |
---|
| 540 | return false; |
---|
[871] | 541 | } |
---|
| 542 | }; |
---|
| 543 | |
---|
| 544 | // std::string to Vector4 |
---|
| 545 | template <> |
---|
[1052] | 546 | struct ConverterSpecialized<std::string, orxonox::Vector4, _Explicit_> |
---|
[871] | 547 | { |
---|
[1052] | 548 | enum { specialized = true }; |
---|
| 549 | static bool convert(orxonox::Vector4* output, const std::string& input) |
---|
[871] | 550 | { |
---|
[1052] | 551 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 552 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[871] | 553 | |
---|
[1052] | 554 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 555 | if (tokens.size() >= 4) |
---|
| 556 | { |
---|
| 557 | if (!ConvertValue(&(output->x), tokens[0])) |
---|
| 558 | return false; |
---|
| 559 | if (!ConvertValue(&(output->y), tokens[1])) |
---|
| 560 | return false; |
---|
| 561 | if (!ConvertValue(&(output->z), tokens[2])) |
---|
| 562 | return false; |
---|
| 563 | if (!ConvertValue(&(output->w), tokens[3])) |
---|
| 564 | return false; |
---|
[871] | 565 | |
---|
[1052] | 566 | return true; |
---|
| 567 | } |
---|
| 568 | return false; |
---|
[871] | 569 | } |
---|
| 570 | }; |
---|
| 571 | |
---|
| 572 | // std::string to Quaternion |
---|
| 573 | template <> |
---|
[1052] | 574 | struct ConverterSpecialized<std::string, orxonox::Quaternion, _Explicit_> |
---|
[871] | 575 | { |
---|
[1052] | 576 | enum { specialized = true }; |
---|
| 577 | static bool convert(orxonox::Quaternion* output, const std::string& input) |
---|
[871] | 578 | { |
---|
[1052] | 579 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 580 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[871] | 581 | |
---|
[1052] | 582 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 583 | if (tokens.size() >= 4) |
---|
| 584 | { |
---|
| 585 | if (!ConvertValue(&(output->w), tokens[0])) |
---|
| 586 | return false; |
---|
| 587 | if (!ConvertValue(&(output->x), tokens[1])) |
---|
| 588 | return false; |
---|
| 589 | if (!ConvertValue(&(output->y), tokens[2])) |
---|
| 590 | return false; |
---|
| 591 | if (!ConvertValue(&(output->z), tokens[3])) |
---|
| 592 | return false; |
---|
[871] | 593 | |
---|
[1052] | 594 | return true; |
---|
| 595 | } |
---|
| 596 | return false; |
---|
[871] | 597 | } |
---|
| 598 | }; |
---|
| 599 | |
---|
| 600 | // std::string to ColourValue |
---|
| 601 | template <> |
---|
[1052] | 602 | struct ConverterSpecialized<std::string, orxonox::ColourValue, _Explicit_> |
---|
[871] | 603 | { |
---|
[1052] | 604 | enum { specialized = true }; |
---|
| 605 | static bool convert(orxonox::ColourValue* output, const std::string& input) |
---|
[871] | 606 | { |
---|
[1052] | 607 | unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 608 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[871] | 609 | |
---|
[1052] | 610 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 611 | if (tokens.size() >= 4) |
---|
| 612 | { |
---|
| 613 | if (!ConvertValue(&(output->r), tokens[0])) |
---|
| 614 | return false; |
---|
| 615 | if (!ConvertValue(&(output->g), tokens[1])) |
---|
| 616 | return false; |
---|
| 617 | if (!ConvertValue(&(output->b), tokens[2])) |
---|
| 618 | return false; |
---|
| 619 | if (!ConvertValue(&(output->a), tokens[3])) |
---|
| 620 | return false; |
---|
[871] | 621 | |
---|
[1052] | 622 | return true; |
---|
| 623 | } |
---|
| 624 | return false; |
---|
[871] | 625 | } |
---|
| 626 | }; |
---|
[1064] | 627 | |
---|
| 628 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC |
---|
| 629 | #pragma warning(pop) |
---|
| 630 | #endif |
---|
[1052] | 631 | |
---|
| 632 | #endif /* _Convert_H__ */ |
---|