Changeset 1001 for code/branches/core2/src/util/Convert.h
- Timestamp:
- Apr 8, 2008, 1:51:25 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core2/src/util/Convert.h
r994 r1001 41 41 #include "MultiTypeMath.h" 42 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 { 43 44 ////////// 45 // Main // 46 ////////// 47 /* 48 enum ConversionPreference 49 { 50 CP_ToType, 51 CP_FromType 52 }; 53 54 // Helper classes to determine the preferred partial template specialization 55 class _AnyType_ {}; 56 class _ToType_ {} static __to__; 57 class _FromType_ {} static __from__; 58 class _Explicit_ {} static __explicit__; 59 60 61 // The default convert functions 62 template <class FromType, class ToType> 63 static bool convert(ToType* output, const FromType& input, _ToType_* type) 64 { std::cout << "default to" << std::endl; return false; } 65 template <class FromType, class ToType> 66 static bool convert(ToType* output, const FromType& input, _FromType_* type) 67 { std::cout << "default from" << std::endl; return false; } 68 template <class FromType, class ToType> 69 static bool convert(ToType* output, const FromType& input, _Explicit_* type) 70 { std::cout << "default explicit" << std::endl; return false; } 71 72 73 // The default convert function if both types are the same 74 template <class BothTypes> 75 static bool convert(BothTypes* output, const BothTypes& input, _Explicit_* type) 76 { (*output) = input; return true; } 77 78 79 // The default conversion of primitives: A typecast (defined over two partial specialized templates to exclude all non-primitive types and classes) 80 template <class FromType, class ToType, class Type> 81 static bool convertDefault(ToType* output, const FromType& input, Type* type) 82 { return false; } 83 #define CONVERT_PRIMITIVE_DEFAULT(primitive) \ 84 template <class FromType> static bool convertDefault(primitive* output, const FromType& input, _ToType_* type) { return convertDefault(output, input, &__from__); } \ 85 template <class ToType> static bool convertDefault(ToType* output, const primitive& input, _FromType_* type) { (*output) = (ToType)input; return true; } 86 CONVERT_PRIMITIVE_DEFAULT(int) 87 CONVERT_PRIMITIVE_DEFAULT(unsigned int) 88 CONVERT_PRIMITIVE_DEFAULT(char) 89 CONVERT_PRIMITIVE_DEFAULT(unsigned char) 90 CONVERT_PRIMITIVE_DEFAULT(short) 91 CONVERT_PRIMITIVE_DEFAULT(unsigned short) 92 CONVERT_PRIMITIVE_DEFAULT(long) 93 CONVERT_PRIMITIVE_DEFAULT(unsigned long) 94 CONVERT_PRIMITIVE_DEFAULT(float) 95 CONVERT_PRIMITIVE_DEFAULT(double) 96 CONVERT_PRIMITIVE_DEFAULT(long double) 97 CONVERT_PRIMITIVE_DEFAULT(bool) 98 99 // Calls all four possibilities of converting two values: (the order of 2 and 3 can be changed by setting bPreferToType to false) 100 // 1) explicit specialization 101 // 2) partial specialization for ToType 102 // 3) partial specialization of the FromType 103 // 4) default conversion if available 104 template<class FromType, class ToType> 105 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 106 { 107 std::cout << "1_1\n"; 108 if (convert(output, input, &__explicit__)) 109 return true; 110 111 std::cout << "1_2\n"; 112 if (preference == CP_ToType) 113 { 114 std::cout << "1_3\n"; 115 if (convert(output, input, &__to__)) 116 return true; 117 std::cout << "1_4\n"; 118 if (convert(output, input, &__from__)) 119 return true; 120 std::cout << "1_5\n"; 121 } 122 else 123 { 124 std::cout << "1_6\n"; 125 if (convert(output, input, &__from__)) 126 return true; 127 std::cout << "1_7\n"; 128 if (convert(output, input, &__to__)) 129 return true; 130 std::cout << "1_8\n"; 131 } 132 std::cout << "1_9\n"; 133 return convertDefault(output, input, &__to__); 134 } 135 */ 136 137 // Enum to declare the wanted conversion preference in case of equal type-levels 138 enum ConversionPreference 139 { 140 CP_ToType, 141 CP_FromType 142 }; 143 144 // Helper classes to determine the preferred partial template specialization 145 class _AnyType_ {}; 146 class _ToType_ {} static __to__; 147 class _FromType_ {} static __from__; 148 class _Explicit_ {} static __explicit__; 149 150 151 // The default convert functions 152 template <class FromType, class ToType> 153 static bool convert(ToType* output, const FromType& input, _ToType_* type) 154 { std::cout << "default to" << std::endl; return false; } 155 template <class FromType, class ToType> 156 static bool convert(ToType* output, const FromType& input, _FromType_* type) 157 { std::cout << "default from" << std::endl; return false; } 158 template <class FromType, class ToType> 159 static bool convert(ToType* output, const FromType& input, _Explicit_* type) 160 { std::cout << "default explicit" << std::endl; return false; } 161 162 163 // The default convert function if both types are the same 164 template <class BothTypes> 165 static bool convert(BothTypes* output, const BothTypes& input, _Explicit_* type) 166 { (*output) = input; return true; } 167 168 169 // The possible levels 170 #define __low__ 0 171 #define __mid__ 1 172 #define __high__ 2 173 174 // Defines the levels of all types 175 template <class T> struct ConverterLeveL { enum { level = __high__ }; }; 176 template <> struct ConverterLeveL<std::string> { enum { level = __mid__ }; }; 177 template <> struct ConverterLeveL<int> { enum { level = __low__ }; }; 178 template <> struct ConverterLeveL<unsigned int> { enum { level = __low__ }; }; 179 template <> struct ConverterLeveL<char> { enum { level = __low__ }; }; 180 template <> struct ConverterLeveL<unsigned char> { enum { level = __low__ }; }; 181 template <> struct ConverterLeveL<short> { enum { level = __low__ }; }; 182 template <> struct ConverterLeveL<unsigned short> { enum { level = __low__ }; }; 183 template <> struct ConverterLeveL<long> { enum { level = __low__ }; }; 184 template <> struct ConverterLeveL<unsigned long> { enum { level = __low__ }; }; 185 template <> struct ConverterLeveL<float> { enum { level = __low__ }; }; 186 template <> struct ConverterLeveL<double> { enum { level = __low__ }; }; 187 template <> struct ConverterLeveL<long double> { enum { level = __low__ }; }; 188 template <> struct ConverterLeveL<bool> { enum { level = __low__ }; }; 189 190 191 // Calculates the preference based on the levels of FromType and ToType 192 template <int from, int to> 193 struct ConverterPreference 194 { 195 enum 196 { 197 max = (from > to) ? from : to, 198 diff = ((to - from) > 1) ? 1 : (((to - from) < -1) ? -1 : to - from) 199 }; 200 }; 201 202 203 // The default conversion: This usually does nothing 204 template <int max, class FromType, class ToType, class Type> 205 struct ConverterDefault 206 { 207 static bool convert(ToType* output, const FromType& input, Type* type) 208 { 209 return false; 210 } 211 }; 212 // 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> 213 template <class FromType, class ToType, class Type> 214 struct ConverterDefault<0, FromType, ToType, Type> 215 { 216 static bool convert(ToType* output, const FromType& input, Type* type) 217 { 218 (*output) = (ToType)input; 219 return true; 220 } 221 }; 222 223 224 // Converter: Converts input of FromType into output of ToType 225 template <int diff, int max, class FromType, class ToType> 226 struct Converter 227 { 228 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 229 { 230 return false; 231 } 232 }; 233 // Converter: FromType-level > ToType-level 234 template <int max, class FromType, class ToType> 235 struct Converter<-1, max, FromType, ToType> 236 { 237 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 238 { 239 if (convert(output, input, &__explicit__)) 240 return true; 241 if (convert(output, input, &__from__)) 242 return true; 243 if (ConverterDefault<max, FromType, ToType, _ToType_>::convert(output, input, &__to__)) 244 return true; 245 246 return false; 247 } 248 }; 249 // Converter: ToType-level > FromType-level 250 template <int max, class FromType, class ToType> 251 struct Converter<1, max, FromType, ToType> 252 { 253 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 254 { 255 if (convert(output, input, &__explicit__)) 256 return true; 257 if (convert(output, input, &__to__)) 258 return true; 259 if (ConverterDefault<max, FromType, ToType, _ToType_>::convert(output, input, &__to__)) 260 return true; 261 262 return false; 263 } 264 }; 265 // Converter: ToType-level = ToType-level 266 template <int max, class FromType, class ToType> 267 struct Converter<0, max, FromType, ToType> 268 { 269 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 270 { 271 if (convert(output, input, &__explicit__)) 272 return true; 273 274 if (preference == CP_ToType) 275 { 276 if (convert(output, input, &__to__)) 277 return true; 278 if (convert(output, input, &__from__)) 279 return true; 280 } 281 else 282 { 283 if (convert(output, input, &__from__)) 284 return true; 285 if (convert(output, input, &__to__)) 286 return true; 287 } 288 289 if (ConverterDefault<max, FromType, ToType, _ToType_>::convert(output, input, &__to__)) 290 return true; 291 292 return false; 293 } 294 }; 295 296 297 // Calls the Converter::convertValue function with the correct template type parameters calculated by ConverterPreference 298 template <class FromType, class ToType> 299 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 300 { 301 return Converter<ConverterPreference<ConverterLeveL<FromType>::level, ConverterLeveL<ToType>::level>::diff, ConverterPreference<ConverterLeveL<FromType>::level, ConverterLeveL<ToType>::level>::max, FromType, ToType>::convertValue(output, input, preference); 302 } 303 304 305 // Helper function: Calls convertValue with and without default value and returns true if the conversion was successful 306 template<class FromType, class ToType> 307 static bool ConvertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_ToType) 308 { 309 return convertValue(output, input, preference); 310 } 311 template<class FromType, class ToType> 312 static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback, ConversionPreference preference = CP_ToType) 313 { 314 if (convertValue(output, input, preference)) 315 return true; 316 317 (*output) = fallback; 318 return false; 319 } 320 321 // Helper function: Calls convertValue with and without default value and returns the converted value 322 template<class FromType, class ToType> 323 static ToType getConvertedValue(const FromType& input, ConversionPreference preference = CP_ToType) 324 { 325 ToType output = ToType(); 326 ConvertValue(&output, input, preference); 327 return output; 328 } 329 template<class FromType, class ToType> 330 static ToType getConvertedValue(const FromType& input, const ToType& fallback, ConversionPreference preference = CP_ToType) 331 { 332 ToType output = fallback; 333 ConvertValue(&output, input, fallback, preference); 334 return output; 335 } 336 337 ///////////////////// 338 // SPECIALISATIONS // 339 ///////////////////// 340 341 /* 342 template <class FromType> 343 static bool convertDefault(primitive* output, const FromType& input, _ToType_* type) 344 { 345 346 } 347 348 template <class ToType> 349 static bool convertDefault(ToType* output, const primitive& input, _FromType_* type) 350 { 351 352 } 353 354 355 template <> 356 static bool convertDefault(ToType* output, const FromType& input, _Explicit_* type) 357 { 358 359 } 360 */ 361 362 //////////// 363 // String // 364 //////////// 365 366 // convert to string 367 template <class FromType> 368 static bool convert(std::string* output, const FromType& input, _ToType_* type) 369 { 370 std::ostringstream oss; 371 if (oss << input) 372 { 64 373 (*output) = oss.str(); 65 374 return true; 66 67 375 } 376 else 68 377 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 378 } 379 380 // convert from string 381 template <class ToType> 382 static bool convert(ToType* output, const std::string& input, _FromType_* type) 383 { 384 std::istringstream iss(input); 385 if (iss >> (*output)) 386 return true; 387 else 83 388 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 }; 389 } 390 140 391 141 392 //////////////// … … 143 394 //////////////// 144 395 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) 396 // convert from MultiTypePrimitive 397 template <class ToType> 398 static bool convert(ToType* output, const MultiTypePrimitive& input, _FromType_* type) 399 { 400 if (input.getType() == MT_void) 153 401 return ConvertValue(output, input.getVoid()); 154 402 else if (input.getType() == MT_int) 155 403 return ConvertValue(output, input.getInt()); 156 404 else if (input.getType() == MT_uint) 157 405 return ConvertValue(output, input.getUnsignedInt()); 158 406 else if (input.getType() == MT_char) 159 407 return ConvertValue(output, input.getChar()); 160 408 else if (input.getType() == MT_uchar) 161 409 return ConvertValue(output, input.getUnsignedChar()); 162 410 else if (input.getType() == MT_short) 163 411 return ConvertValue(output, input.getShort()); 164 412 else if (input.getType() == MT_ushort) 165 413 return ConvertValue(output, input.getUnsignedShort()); 166 414 else if (input.getType() == MT_long) 167 415 return ConvertValue(output, input.getLong()); 168 416 else if (input.getType() == MT_ulong) 169 417 return ConvertValue(output, input.getUnsignedLong()); 170 418 else if (input.getType() == MT_float) 171 419 return ConvertValue(output, input.getFloat()); 172 420 else if (input.getType() == MT_double) 173 421 return ConvertValue(output, input.getDouble()); 174 422 else if (input.getType() == MT_longdouble) 175 423 return ConvertValue(output, input.getLongDouble()); 176 424 else if (input.getType() == MT_bool) 177 425 return ConvertValue(output, input.getBool()); 178 426 else 179 427 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) 428 } 429 430 // convert from MultiTypeString 431 template <class ToType> 432 static bool convert(ToType* output, const MultiTypeString& input, _FromType_* type) 433 { 434 if (input.getType() == MT_constchar) 227 435 return ConvertValue(output, input.getConstChar()); 228 436 else if (input.getType() == MT_string) 229 437 return ConvertValue(output, input.getString()); 230 else if (input.getType() == MT_xmlelement) 231 return ConvertValue(output, input.getXMLElement()); 232 else 438 else 233 439 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) 440 } 441 442 // convert from MultiTypeMath 443 template <class ToType> 444 static bool convert(ToType* output, const MultiTypeMath& input, _FromType_* type) 445 { 446 if (input.getType() == MT_vector2) 447 return ConvertValue(output, input.getVector2(), CP_FromType); 448 else if (input.getType() == MT_vector3) 449 return ConvertValue(output, input.getVector3(), CP_FromType); 450 else if (input.getType() == MT_quaternion) 451 return ConvertValue(output, input.getQuaternion(), CP_FromType); 452 else if (input.getType() == MT_colourvalue) 453 return ConvertValue(output, input.getColourValue(), CP_FromType); 454 else if (input.getType() == MT_radian) 269 455 return ConvertValue(output, input.getRadian()); 270 456 else if (input.getType() == MT_degree) 271 457 return ConvertValue(output, input.getDegree()); 272 458 else 273 459 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 }; 460 } 298 461 299 462 … … 304 467 // Vector2 to std::string 305 468 template <> 469 static bool convert(std::string* output, const orxonox::Vector2& input, _Explicit_* type) 470 { 471 std::ostringstream ostream; 472 if (ostream << input.x << "," << input.y) 473 { 474 (*output) = ostream.str(); 475 return true; 476 } 477 return false; 478 } 479 480 /* 481 // Vector2 to std::string 482 template <> 306 483 class Converter<orxonox::Vector2, std::string> 307 484 { … … 319 496 } 320 497 }; 321 498 */ 499 // Vector3 to std::string 500 template <> 501 static bool convert(std::string* output, const orxonox::Vector3& input, _Explicit_* type) 502 { 503 std::ostringstream ostream; 504 if (ostream << input.x << "," << input.y << "," << input.z) 505 { 506 (*output) = ostream.str(); 507 return true; 508 } 509 return false; 510 } 511 512 /* 322 513 // Vector3 to std::string 323 514 template <> … … 337 528 } 338 529 }; 339 530 */ 531 // Vector4 to std::string 532 template <> 533 static bool convert(std::string* output, const orxonox::Vector4& input, _Explicit_* type) 534 { 535 std::ostringstream ostream; 536 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 537 { 538 (*output) = ostream.str(); 539 return true; 540 } 541 return false; 542 } 543 /* 340 544 // Vector4 to std::string 341 545 template <> … … 355 559 } 356 560 }; 357 561 */ 562 // Quaternion to std::string 563 template <> 564 static bool convert(std::string* output, const orxonox::Quaternion& input, _Explicit_* type) 565 { 566 std::ostringstream ostream; 567 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 568 { 569 (*output) = ostream.str(); 570 return true; 571 } 572 return false; 573 } 574 /* 358 575 // Quaternion to std::string 359 576 template <> … … 373 590 } 374 591 }; 375 592 */ 593 // ColourValue to std::string 594 template <> 595 static bool convert(std::string* output, const orxonox::ColourValue& input, _Explicit_* type) 596 { 597 std::ostringstream ostream; 598 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 599 { 600 (*output) = ostream.str(); 601 return true; 602 } 603 return false; 604 } 605 /* 376 606 // ColourValue to std::string 377 607 template <> … … 391 621 } 392 622 }; 623 */ 393 624 394 625 … … 399 630 // std::string to Vector2 400 631 template <> 632 static bool convert(orxonox::Vector2* output, const std::string& input, _Explicit_* type) 633 { 634 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 635 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 636 637 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 638 if (tokens.size() >= 2) 639 { 640 if (!ConvertValue(&(output->x), tokens[0])) 641 return false; 642 if (!ConvertValue(&(output->y), tokens[1])) 643 return false; 644 645 return true; 646 } 647 return false; 648 } 649 /* 650 // std::string to Vector2 651 template <> 401 652 class Converter<std::string, orxonox::Vector2> 402 653 { … … 422 673 } 423 674 }; 424 675 */ 676 // std::string to Vector3 677 template <> 678 static bool convert(orxonox::Vector3* output, const std::string& input, _Explicit_* type) 679 { 680 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 681 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 682 683 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 684 if (tokens.size() >= 3) 685 { 686 if (!ConvertValue(&(output->x), tokens[0])) 687 return false; 688 if (!ConvertValue(&(output->y), tokens[1])) 689 return false; 690 if (!ConvertValue(&(output->z), tokens[2])) 691 return false; 692 693 return true; 694 } 695 return false; 696 } 697 /* 425 698 // std::string to Vector3 426 699 template <> … … 450 723 } 451 724 }; 452 725 */ 726 // std::string to Vector4 727 template <> 728 static bool convert(orxonox::Vector4* output, const std::string& input, _Explicit_* type) 729 { 730 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 731 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 732 733 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 734 if (tokens.size() >= 4) 735 { 736 if (!ConvertValue(&(output->x), tokens[0])) 737 return false; 738 if (!ConvertValue(&(output->y), tokens[1])) 739 return false; 740 if (!ConvertValue(&(output->z), tokens[2])) 741 return false; 742 if (!ConvertValue(&(output->w), tokens[3])) 743 return false; 744 745 return true; 746 } 747 return false; 748 } 749 /* 453 750 // std::string to Vector4 454 751 template <> … … 480 777 } 481 778 }; 482 779 */ 780 // std::string to Quaternion 781 template <> 782 static bool convert(orxonox::Quaternion* output, const std::string& input, _Explicit_* type) 783 { 784 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 785 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 786 787 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 788 if (tokens.size() >= 4) 789 { 790 if (!ConvertValue(&(output->w), tokens[0])) 791 return false; 792 if (!ConvertValue(&(output->x), tokens[1])) 793 return false; 794 if (!ConvertValue(&(output->y), tokens[2])) 795 return false; 796 if (!ConvertValue(&(output->z), tokens[3])) 797 return false; 798 799 return true; 800 } 801 return false; 802 } 803 /* 483 804 // std::string to Quaternion 484 805 template <> … … 510 831 } 511 832 }; 512 833 */ 834 // std::string to ColourValue 835 template <> 836 static bool convert(orxonox::ColourValue* output, const std::string& input, _Explicit_* type) 837 { 838 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 839 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 840 841 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 842 if (tokens.size() >= 4) 843 { 844 if (!ConvertValue(&(output->r), tokens[0])) 845 return false; 846 if (!ConvertValue(&(output->g), tokens[1])) 847 return false; 848 if (!ConvertValue(&(output->b), tokens[2])) 849 return false; 850 if (!ConvertValue(&(output->a), tokens[3])) 851 return false; 852 853 return true; 854 } 855 return false; 856 } 857 /* 513 858 // std::string to ColourValue 514 859 template <> … … 540 885 } 541 886 }; 887 */ 888 542 889 543 890 #endif /* _Convert_H__ */
Note: See TracChangeset
for help on using the changeset viewer.