Changeset 1052 for code/trunk/src/util/Convert.h
- Timestamp:
- Apr 14, 2008, 3:42:49 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/util/Convert.h
r871 r1052 21 21 * Author: 22 22 * Benjamin Grauer 23 * Fabian 'x3n' Landau 23 24 * Co-authors: 24 * Fabian 'x3n' Landau25 * ... 25 26 */ 26 27 … … 42 43 43 44 44 // DEFAULT CLASS 45 template <typename FromType, typename ToType> 46 class Converter 47 { 48 public: 49 bool operator()(ToType* output, const FromType& input) const 50 { 51 return false; 52 } 53 }; 54 55 // PARTIAL SPECIALIZATION TO CONVERT TO STRINGS 56 template<typename FromType> 57 class Converter<FromType, std::string> 58 { 59 public: 60 bool operator()(std::string* output, const FromType& input) const 61 { 62 std::ostringstream oss; 63 if (oss << input) 64 { 65 (*output) = oss.str(); 45 ////////// 46 // MAIN // 47 ////////// 48 49 // Enum to declare the wanted conversion preference in case of equal type-levels 50 enum ConversionPreference 51 { 52 CP_PreferToType, 53 CP_PreferFromType, 54 }; 55 56 // Helper classes to determine the preferred partial template specialization 57 class _ToType_ {}; 58 class _FromType_ {}; 59 class _Explicit_ {}; 60 61 62 // The default convert functions 63 template <class FromType, class ToType, class Type> 64 struct ConverterSpecialized 65 { 66 enum { specialized = false }; 67 static bool convert(ToType* output, const FromType& input) 68 { return false; } 69 }; 70 71 72 // The default convert function if both types are the same 73 template <class BothTypes> 74 struct ConverterSpecialized<BothTypes, BothTypes, _Explicit_> 75 { 76 enum { specialized = true }; 77 static bool convert(BothTypes* output, const BothTypes& input) 78 { (*output) = input; return true; } 79 }; 80 81 82 // The possible levels 83 #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) 84 #define __mid__ 1 // Everything that has overloaded << and >> operators to operate on a std::stream 85 #define __high__ 2 // Everything that doesn't fullfill the lowerlevel-requirements and therefore needs specialized conversions 86 87 // Defines the levels of all types: Default is __high__ so you don't have to define every high-level type 88 template <class T> struct ConverterLevel { enum { level = __high__ }; }; 89 template <> struct ConverterLevel<std::string> { enum { level = __mid__ }; }; 90 template <> struct ConverterLevel<orxonox::Radian> { enum { level = __mid__ }; }; 91 template <> struct ConverterLevel<orxonox::Degree> { enum { level = __mid__ }; }; 92 template <> struct ConverterLevel<int> { enum { level = __low__ }; }; 93 template <> struct ConverterLevel<unsigned int> { enum { level = __low__ }; }; 94 template <> struct ConverterLevel<char> { enum { level = __low__ }; }; 95 template <> struct ConverterLevel<unsigned char> { enum { level = __low__ }; }; 96 template <> struct ConverterLevel<short> { enum { level = __low__ }; }; 97 template <> struct ConverterLevel<unsigned short> { enum { level = __low__ }; }; 98 template <> struct ConverterLevel<long> { enum { level = __low__ }; }; 99 template <> struct ConverterLevel<unsigned long> { enum { level = __low__ }; }; 100 template <> struct ConverterLevel<float> { enum { level = __low__ }; }; 101 template <> struct ConverterLevel<double> { enum { level = __low__ }; }; 102 template <> struct ConverterLevel<long double> { enum { level = __low__ }; }; 103 template <> struct ConverterLevel<bool> { enum { level = __low__ }; }; 104 105 106 // Calculates the preference based on the levels of FromType and ToType 107 template <int from, int to> 108 struct ConverterPreference 109 { 110 enum 111 { 112 // The maximum of both levels: element of {0, 1, 2} 113 // max 0: Both types are primitives or have a similar behaviour 114 // max 1: At least one type is not a primitive, but both can be put on a std::stream 115 // max 2: There is at least one generic type that needs specialized conversions 116 max = (from > to) ? from : to, 117 118 // The difference between both levels limited to +-1: element of {-1, 0, 1} 119 // diff -1: The FromType has higher level than the ToType 120 // diff 0: Both types have the same level 121 // diff 1: The ToType has higher level than the FromType 122 diff = ((to - from) > 1) ? 1 : (((to - from) < -1) ? -1 : to - from) 123 }; 124 }; 125 126 127 // The default conversion: This usually does nothing 128 template <int max, class FromType, class ToType> 129 struct ConverterDefault 130 { 131 static bool convert(ToType* output, const FromType& input) 132 { 133 return false; 134 } 135 }; 136 // 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> 137 template <class FromType, class ToType> 138 struct ConverterDefault<0, FromType, ToType> 139 { 140 static bool convert(ToType* output, const FromType& input) 141 { 142 (*output) = (ToType)input; 66 143 return true; 67 } 68 else 69 return false; 70 } 71 }; 72 73 // PARTIAL SPECIALIZATION TO CONVERT FROM STRING 74 template<typename ToType> 75 class Converter<std::string, ToType> 76 { 77 public: 78 bool operator()(ToType* output, const std::string& input) const 79 { 80 std::istringstream iss(input); 81 if (iss >> (*output)) 144 } 145 }; 146 147 148 // Converter: Converts input of FromType into output of ToType 149 template <int diff, int max, class FromType, class ToType, ConversionPreference pref> 150 struct Converter 151 { 152 static bool convert(ToType* output, const FromType& input) 153 { 154 return false; 155 } 156 }; 157 // Converter: level{FromType} > level{ToType} 158 template <int max, class FromType, class ToType, ConversionPreference pref> 159 struct Converter<-1, max, FromType, ToType, pref> 160 { static bool convert(ToType* output, const FromType& input) 161 { 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)); } }; 162 // Converter: level{FromType} < level{ToType} 163 template <int max, class FromType, class ToType, ConversionPreference pref> 164 struct Converter<1, max, FromType, ToType, pref> 165 { static bool convert(ToType* output, const FromType& input) 166 { 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)); } }; 167 // Converter: level{FromType} = level{ToType} 168 // CP_PreferToType 169 template <int max, class FromType, class ToType> 170 struct Converter<0, max, FromType, ToType, CP_PreferToType> 171 { static bool convert(ToType* output, const FromType& input) 172 { 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)); } }; 173 // CP_PreferFromType 174 template <int max, class FromType, class ToType> 175 struct Converter<0, max, FromType, ToType, CP_PreferFromType> 176 { static bool convert(ToType* output, const FromType& input) 177 { 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)); } }; 178 179 180 // Calls the Converter::convertValue function with the correct template type parameters calculated by ConverterPreference 181 template <class FromType, class ToType> 182 static bool convertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) 183 { 184 return (preference == CP_PreferToType) ? 185 Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, 186 ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, 187 FromType, 188 ToType, 189 CP_PreferToType>::convert(output, input) 190 : Converter<ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::diff, 191 ConverterPreference<ConverterLevel<FromType>::level, ConverterLevel<ToType>::level>::max, 192 FromType, 193 ToType, 194 CP_PreferFromType>::convert(output, input); 195 } 196 197 198 ////////////////////// 199 // HELPER FUNCTIONS // 200 ////////////////////// 201 202 // Helper function: Calls convertValue with and without default value and returns true if the conversion was successful 203 template<class FromType, class ToType> 204 static bool ConvertValue(ToType* output, const FromType& input, ConversionPreference preference = CP_PreferToType) 205 { 206 return convertValue(output, input, preference); 207 } 208 template<class FromType, class ToType> 209 static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) 210 { 211 if (convertValue(output, input, preference)) 82 212 return true; 83 else 84 return false; 85 } 86 }; 87 88 // FUNCTION SO WE DO NOT HAVE TO TELL THE COMPILER ABOUT THE TYPE 89 template<typename FromType, typename ToType> 90 static bool ConvertValue(ToType* output, const FromType& input) 91 { 92 Converter<FromType, ToType> converter; 93 return converter(output, input); 213 214 (*output) = fallback; 215 return false; 94 216 } 95 217 96 // THE SAME, BUT WITH DEFAULT VALUE 97 template<typename FromType, typename ToType> 98 static bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) 99 { 100 Converter<FromType, ToType> converter; 101 if (converter(output, input)) 102 return true; 103 104 (*output) = fallback; 105 return false; 218 // Helper function: Calls convertValue with and without default value and returns the converted value 219 template<class FromType, class ToType> 220 static ToType getConvertedValue(const FromType& input, ConversionPreference preference = CP_PreferToType) 221 { 222 ToType output = ToType(); 223 ConvertValue(&output, input, preference); 224 return output; 106 225 } 107 108 // THE SAME LIKE BEFORE, BUT NEEDS NO POINTER TO THE OUTPUT 109 template<typename FromType, typename ToType> 110 static ToType ConvertValueAndReturn(const FromType& input) 111 { 112 ToType output; 113 ConvertValue(&output, input); 114 return output; 226 template<class FromType, class ToType> 227 static ToType getConvertedValue(const FromType& input, const ToType& fallback, ConversionPreference preference = CP_PreferToType) 228 { 229 ToType output = fallback; 230 ConvertValue(&output, input, fallback, preference); 231 return output; 115 232 } 116 233 117 // THE SAME, BUT WITH DEFAULT VALUE 118 template<typename FromType, typename ToType> 119 static ToType ConvertValueAndReturn(const FromType& input, const FromType& fallback) 120 { 121 ToType output; 122 ConvertValue(&output, input, fallback); 123 return output; 124 } 125 126 ////////////////////////// 127 // MORE SPECIALISATIONS // 128 ////////////////////////// 129 130 // STRING TO STRING 131 template<> 132 class Converter<std::string, std::string> 133 { 134 public: 135 bool operator()(std::string* output, const std::string& input) const 136 { 137 (*output) = std::string(input); 138 return true; 139 } 140 }; 234 235 ///////////////////// 236 // SPECIALIZATIONS // 237 ///////////////////// 238 239 ///////////// 240 // SAMPLES // 241 ///////////// 242 /* 243 // convert everything to xyz 244 template <class FromType> 245 struct ConverterSpecialized<FromType, xyz, _ToType_> 246 { 247 enum { specialized = true }; 248 static bool convert(xyz* output, const FromType& input) 249 { return ...; } 250 }; 251 252 // convert xyz to everything 253 template <class ToType> 254 struct ConverterSpecialized<xyz, ToType, _FromType_> 255 { 256 enum { specialized = true }; 257 static bool convert(ToType* output, const xyz& input) 258 { return ...; } 259 }; 260 261 // convert abc to xyz 262 template <> 263 struct ConverterSpecialized<abc, xyz, _Explicit_> 264 { 265 enum { specialized = true }; 266 static bool convert(xyz* output, const abc& input) 267 { return ...; } 268 }; 269 */ 270 271 //////////// 272 // STRING // 273 //////////// 274 275 // convert to string 276 template <class FromType> 277 struct ConverterSpecialized<FromType, std::string, _ToType_> 278 { 279 enum { specialized = true }; 280 static bool convert(std::string* output, const FromType& input) 281 { 282 std::ostringstream oss; 283 if (oss << input) 284 { 285 (*output) = oss.str(); 286 return true; 287 } 288 else 289 return false; 290 } 291 }; 292 293 // convert from string 294 template <class ToType> 295 struct ConverterSpecialized<std::string, ToType, _FromType_> 296 { 297 enum { specialized = true }; 298 static bool convert(ToType* output, const std::string& input) 299 { 300 std::istringstream iss(input); 301 if (iss >> (*output)) 302 return true; 303 else 304 return false; 305 } 306 }; 307 141 308 142 309 //////////////// … … 144 311 //////////////// 145 312 146 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEPRIMITIVE 147 template<typename ToType> 148 class Converter<MultiTypePrimitive, ToType> 149 { 150 public: 151 bool operator()(ToType* output, const MultiTypePrimitive& input) const 152 { 153 if (input.getType() == MT_int) 154 return ConvertValue(output, input.getInt()); 155 else if (input.getType() == MT_uint) 156 return ConvertValue(output, input.getUnsignedInt()); 157 else if (input.getType() == MT_char) 158 return ConvertValue(output, input.getChar()); 159 else if (input.getType() == MT_uchar) 160 return ConvertValue(output, input.getUnsignedChar()); 161 else if (input.getType() == MT_short) 162 return ConvertValue(output, input.getShort()); 163 else if (input.getType() == MT_ushort) 164 return ConvertValue(output, input.getUnsignedShort()); 165 else if (input.getType() == MT_long) 166 return ConvertValue(output, input.getLong()); 167 else if (input.getType() == MT_ulong) 168 return ConvertValue(output, input.getUnsignedLong()); 169 else if (input.getType() == MT_float) 170 return ConvertValue(output, input.getFloat()); 171 else if (input.getType() == MT_double) 172 return ConvertValue(output, input.getDouble()); 173 else if (input.getType() == MT_longdouble) 174 return ConvertValue(output, input.getLongDouble()); 175 else if (input.getType() == MT_bool) 176 return ConvertValue(output, input.getBool()); 177 else 178 return false; 179 } 180 }; 181 template<> 182 class Converter<MultiTypePrimitive, std::string> 183 { 184 public: 185 bool operator()(std::string* output, const MultiTypePrimitive& input) const 186 { 187 if (input.getType() == MT_int) 188 return ConvertValue(output, input.getInt()); 189 else if (input.getType() == MT_uint) 190 return ConvertValue(output, input.getUnsignedInt()); 191 else if (input.getType() == MT_char) 192 return ConvertValue(output, input.getChar()); 193 else if (input.getType() == MT_uchar) 194 return ConvertValue(output, input.getUnsignedChar()); 195 else if (input.getType() == MT_short) 196 return ConvertValue(output, input.getShort()); 197 else if (input.getType() == MT_ushort) 198 return ConvertValue(output, input.getUnsignedShort()); 199 else if (input.getType() == MT_long) 200 return ConvertValue(output, input.getLong()); 201 else if (input.getType() == MT_ulong) 202 return ConvertValue(output, input.getUnsignedLong()); 203 else if (input.getType() == MT_float) 204 return ConvertValue(output, input.getFloat()); 205 else if (input.getType() == MT_double) 206 return ConvertValue(output, input.getDouble()); 207 else if (input.getType() == MT_longdouble) 208 return ConvertValue(output, input.getLongDouble()); 209 else if (input.getType() == MT_bool) 210 return ConvertValue(output, input.getBool()); 211 else 212 return false; 213 } 214 }; 215 216 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPESTRING 217 template<typename ToType> 218 class Converter<MultiTypeString, ToType> 219 { 220 public: 221 bool operator()(ToType* output, const MultiTypeString& input) const 222 { 223 if (input.getType() == MT_constchar) 224 return ConvertValue(output, input.getConstChar()); 225 else if (input.getType() == MT_string) 226 return ConvertValue(output, input.getString()); 227 else 228 return ConvertValue(output, (MultiTypePrimitive)input); 229 } 230 }; 231 template<> 232 class Converter<MultiTypeString, std::string> 233 { 234 public: 235 bool operator()(std::string* output, const MultiTypeString& input) const 236 { 237 if (input.getType() == MT_constchar) 238 return ConvertValue(output, input.getConstChar()); 239 else if (input.getType() == MT_string) 240 return ConvertValue(output, input.getString()); 241 else 242 return ConvertValue(output, (MultiTypePrimitive)input); 243 } 244 }; 245 246 // PARTIAL SPECIALIZATION TO CONVERT FROM MULTITYPEMATH 247 template<typename ToType> 248 class Converter<MultiTypeMath, ToType> 249 { 250 public: 251 bool operator()(ToType* output, const MultiTypeMath& input) const 252 { 253 if (input.getType() == MT_vector2) 254 return ConvertValue(output, input.getVector2()); 255 else if (input.getType() == MT_vector3) 256 return ConvertValue(output, input.getVector3()); 257 else if (input.getType() == MT_quaternion) 258 return ConvertValue(output, input.getQuaternion()); 259 else if (input.getType() == MT_colourvalue) 260 return ConvertValue(output, input.getColourValue()); 261 else if (input.getType() == MT_radian) 262 return ConvertValue(output, input.getRadian()); 263 else if (input.getType() == MT_degree) 264 return ConvertValue(output, input.getDegree()); 265 else 266 return ConvertValue(output, (MultiTypeString)input); 267 } 268 }; 269 template<> 270 class Converter<MultiTypeMath, std::string> 271 { 272 public: 273 bool operator()(std::string* output, const MultiTypeMath& input) const 274 { 275 if (input.getType() == MT_vector2) 276 return ConvertValue(output, input.getVector2()); 277 else if (input.getType() == MT_vector3) 278 return ConvertValue(output, input.getVector3()); 279 else if (input.getType() == MT_quaternion) 280 return ConvertValue(output, input.getQuaternion()); 281 else if (input.getType() == MT_colourvalue) 282 return ConvertValue(output, input.getColourValue()); 283 else if (input.getType() == MT_radian) 284 return ConvertValue(output, input.getRadian()); 285 else if (input.getType() == MT_degree) 286 return ConvertValue(output, input.getDegree()); 287 else 288 return ConvertValue(output, (MultiTypeString)input); 313 // convert from MultiTypePrimitive 314 template <class ToType> 315 struct ConverterSpecialized<MultiTypePrimitive, ToType, _FromType_> 316 { 317 enum { specialized = true }; 318 static bool convert(ToType* output, const MultiTypePrimitive& input) 319 { 320 if (input.getType() == MT_void) 321 return ConvertValue(output, input.getVoid()); 322 else if (input.getType() == MT_int) 323 return ConvertValue(output, input.getInt()); 324 else if (input.getType() == MT_uint) 325 return ConvertValue(output, input.getUnsignedInt()); 326 else if (input.getType() == MT_char) 327 return ConvertValue(output, input.getChar()); 328 else if (input.getType() == MT_uchar) 329 return ConvertValue(output, input.getUnsignedChar()); 330 else if (input.getType() == MT_short) 331 return ConvertValue(output, input.getShort()); 332 else if (input.getType() == MT_ushort) 333 return ConvertValue(output, input.getUnsignedShort()); 334 else if (input.getType() == MT_long) 335 return ConvertValue(output, input.getLong()); 336 else if (input.getType() == MT_ulong) 337 return ConvertValue(output, input.getUnsignedLong()); 338 else if (input.getType() == MT_float) 339 return ConvertValue(output, input.getFloat()); 340 else if (input.getType() == MT_double) 341 return ConvertValue(output, input.getDouble()); 342 else if (input.getType() == MT_longdouble) 343 return ConvertValue(output, input.getLongDouble()); 344 else if (input.getType() == MT_bool) 345 return ConvertValue(output, input.getBool()); 346 else 347 return false; 348 } 349 }; 350 351 // convert from MultiTypeString 352 template <class ToType> 353 struct ConverterSpecialized<MultiTypeString, ToType, _FromType_> 354 { 355 enum { specialized = true }; 356 static bool convert(ToType* output, const MultiTypeString& input) 357 { 358 if (input.getType() == MT_constchar) 359 return ConvertValue(output, input.getConstChar()); 360 else if (input.getType() == MT_string) 361 return ConvertValue(output, input.getString()); 362 else 363 return ConvertValue(output, (MultiTypePrimitive)input); 364 } 365 }; 366 367 // convert from MultiTypeMath 368 template <class ToType> 369 struct ConverterSpecialized<MultiTypeMath, ToType, _FromType_> 370 { 371 enum { specialized = true }; 372 static bool convert(ToType* output, const MultiTypeMath& input) 373 { 374 if (input.getType() == MT_vector2) 375 return ConvertValue(output, input.getVector2()); 376 else if (input.getType() == MT_vector3) 377 return ConvertValue(output, input.getVector3()); 378 else if (input.getType() == MT_quaternion) 379 return ConvertValue(output, input.getQuaternion()); 380 else if (input.getType() == MT_colourvalue) 381 return ConvertValue(output, input.getColourValue()); 382 else if (input.getType() == MT_radian) 383 return ConvertValue(output, input.getRadian()); 384 else if (input.getType() == MT_degree) 385 return ConvertValue(output, input.getDegree()); 386 else 387 return ConvertValue(output, (MultiTypeString)input); 289 388 } 290 389 }; … … 297 396 // Vector2 to std::string 298 397 template <> 299 class Converter<orxonox::Vector2, std::string> 300 { 301 public: 302 bool operator()(std::string* output, const orxonox::Vector2& input) const 303 { 304 std::ostringstream ostream; 305 if (ostream << input.x << "," << input.y) 306 { 307 (*output) = ostream.str(); 308 return true; 309 } 310 311 return false; 398 struct ConverterSpecialized<orxonox::Vector2, std::string, _Explicit_> 399 { 400 enum { specialized = true }; 401 static bool convert(std::string* output, const orxonox::Vector2& input) 402 { 403 std::ostringstream ostream; 404 if (ostream << input.x << "," << input.y) 405 { 406 (*output) = ostream.str(); 407 return true; 408 } 409 return false; 312 410 } 313 411 }; … … 315 413 // Vector3 to std::string 316 414 template <> 317 class Converter<orxonox::Vector3, std::string> 318 { 319 public: 320 bool operator()(std::string* output, const orxonox::Vector3& input) const 321 { 322 std::ostringstream ostream; 323 if (ostream << input.x << "," << input.y << "," << input.z) 324 { 325 (*output) = ostream.str(); 326 return true; 327 } 328 329 return false; 415 struct ConverterSpecialized<orxonox::Vector3, std::string, _Explicit_> 416 { 417 enum { specialized = true }; 418 static bool convert(std::string* output, const orxonox::Vector3& input) 419 { 420 std::ostringstream ostream; 421 if (ostream << input.x << "," << input.y << "," << input.z) 422 { 423 (*output) = ostream.str(); 424 return true; 425 } 426 return false; 330 427 } 331 428 }; … … 333 430 // Vector4 to std::string 334 431 template <> 335 class Converter<orxonox::Vector4, std::string> 336 { 337 public: 338 bool operator()(std::string* output, const orxonox::Vector4& input) const 339 { 340 std::ostringstream ostream; 341 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 342 { 343 (*output) = ostream.str(); 344 return true; 345 } 346 347 return false; 432 struct ConverterSpecialized<orxonox::Vector4, std::string, _Explicit_> 433 { 434 enum { specialized = true }; 435 static bool convert(std::string* output, const orxonox::Vector4& input) 436 { 437 std::ostringstream ostream; 438 if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) 439 { 440 (*output) = ostream.str(); 441 return true; 442 } 443 return false; 348 444 } 349 445 }; … … 351 447 // Quaternion to std::string 352 448 template <> 353 class Converter<orxonox::Quaternion, std::string> 354 { 355 public: 356 bool operator()(std::string* output, const orxonox::Quaternion& input) const 357 { 358 std::ostringstream ostream; 359 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 360 { 361 (*output) = ostream.str(); 362 return true; 363 } 364 365 return false; 449 struct ConverterSpecialized<orxonox::Quaternion, std::string, _Explicit_> 450 { 451 enum { specialized = true }; 452 static bool convert(std::string* output, const orxonox::Quaternion& input) 453 { 454 std::ostringstream ostream; 455 if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) 456 { 457 (*output) = ostream.str(); 458 return true; 459 } 460 return false; 366 461 } 367 462 }; … … 369 464 // ColourValue to std::string 370 465 template <> 371 class Converter<orxonox::ColourValue, std::string> 372 { 373 public: 374 bool operator()(std::string* output, const orxonox::ColourValue& input) const 375 { 376 std::ostringstream ostream; 377 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 378 { 379 (*output) = ostream.str(); 380 return true; 381 } 382 383 return false; 466 struct ConverterSpecialized<orxonox::ColourValue, std::string, _Explicit_> 467 { 468 enum { specialized = true }; 469 static bool convert(std::string* output, const orxonox::ColourValue& input) 470 { 471 std::ostringstream ostream; 472 if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) 473 { 474 (*output) = ostream.str(); 475 return true; 476 } 477 return false; 384 478 } 385 479 }; … … 392 486 // std::string to Vector2 393 487 template <> 394 class Converter<std::string, orxonox::Vector2> 395 { 396 public: 397 bool operator()(orxonox::Vector2* output, const std::string& input) const 398 { 399 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 400 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 401 402 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 403 404 if (tokens.size() >= 2) 405 { 406 if (!ConvertValue(&(output->x), tokens[0])) 407 return false; 408 if (!ConvertValue(&(output->y), tokens[1])) 409 return false; 410 411 return true; 412 } 413 414 return false; 488 struct ConverterSpecialized<std::string, orxonox::Vector2, _Explicit_> 489 { 490 enum { specialized = true }; 491 static bool convert(orxonox::Vector2* output, const std::string& input) 492 { 493 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 494 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 495 496 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 497 if (tokens.size() >= 2) 498 { 499 if (!ConvertValue(&(output->x), tokens[0])) 500 return false; 501 if (!ConvertValue(&(output->y), tokens[1])) 502 return false; 503 504 return true; 505 } 506 return false; 415 507 } 416 508 }; … … 418 510 // std::string to Vector3 419 511 template <> 420 class Converter<std::string, orxonox::Vector3> 421 { 422 public: 423 bool operator()(orxonox::Vector3* output, const std::string& input) const 424 { 425 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 426 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 427 428 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 429 430 if (tokens.size() >= 3) 431 { 432 if (!ConvertValue(&(output->x), tokens[0])) 433 return false; 434 if (!ConvertValue(&(output->y), tokens[1])) 435 return false; 436 if (!ConvertValue(&(output->z), tokens[2])) 437 return false; 438 439 return true; 440 } 441 442 return false; 512 struct ConverterSpecialized<std::string, orxonox::Vector3, _Explicit_> 513 { 514 enum { specialized = true }; 515 static bool convert(orxonox::Vector3* output, const std::string& input) 516 { 517 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 518 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 519 520 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 521 if (tokens.size() >= 3) 522 { 523 if (!ConvertValue(&(output->x), tokens[0])) 524 return false; 525 if (!ConvertValue(&(output->y), tokens[1])) 526 return false; 527 if (!ConvertValue(&(output->z), tokens[2])) 528 return false; 529 530 return true; 531 } 532 return false; 443 533 } 444 534 }; … … 446 536 // std::string to Vector4 447 537 template <> 448 class Converter<std::string, orxonox::Vector4> 449 { 450 public: 451 bool operator()(orxonox::Vector4* output, const std::string& input) const 452 { 453 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 454 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 455 456 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 457 458 if (tokens.size() >= 4) 459 { 460 if (!ConvertValue(&(output->x), tokens[0])) 461 return false; 462 if (!ConvertValue(&(output->y), tokens[1])) 463 return false; 464 if (!ConvertValue(&(output->z), tokens[2])) 465 return false; 466 if (!ConvertValue(&(output->w), tokens[3])) 467 return false; 468 469 return true; 470 } 471 472 return false; 538 struct ConverterSpecialized<std::string, orxonox::Vector4, _Explicit_> 539 { 540 enum { specialized = true }; 541 static bool convert(orxonox::Vector4* output, const std::string& input) 542 { 543 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 544 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 545 546 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 547 if (tokens.size() >= 4) 548 { 549 if (!ConvertValue(&(output->x), tokens[0])) 550 return false; 551 if (!ConvertValue(&(output->y), tokens[1])) 552 return false; 553 if (!ConvertValue(&(output->z), tokens[2])) 554 return false; 555 if (!ConvertValue(&(output->w), tokens[3])) 556 return false; 557 558 return true; 559 } 560 return false; 473 561 } 474 562 }; … … 476 564 // std::string to Quaternion 477 565 template <> 478 class Converter<std::string, orxonox::Quaternion> 479 { 480 public: 481 bool operator()(orxonox::Quaternion* output, const std::string& input) const 482 { 483 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 484 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 485 486 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 487 488 if (tokens.size() >= 4) 489 { 490 if (!ConvertValue(&(output->w), tokens[0])) 491 return false; 492 if (!ConvertValue(&(output->x), tokens[1])) 493 return false; 494 if (!ConvertValue(&(output->y), tokens[2])) 495 return false; 496 if (!ConvertValue(&(output->z), tokens[3])) 497 return false; 498 499 return true; 500 } 501 502 return false; 566 struct ConverterSpecialized<std::string, orxonox::Quaternion, _Explicit_> 567 { 568 enum { specialized = true }; 569 static bool convert(orxonox::Quaternion* output, const std::string& input) 570 { 571 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 572 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 573 574 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 575 if (tokens.size() >= 4) 576 { 577 if (!ConvertValue(&(output->w), tokens[0])) 578 return false; 579 if (!ConvertValue(&(output->x), tokens[1])) 580 return false; 581 if (!ConvertValue(&(output->y), tokens[2])) 582 return false; 583 if (!ConvertValue(&(output->z), tokens[3])) 584 return false; 585 586 return true; 587 } 588 return false; 503 589 } 504 590 }; … … 506 592 // std::string to ColourValue 507 593 template <> 508 class Converter<std::string, orxonox::ColourValue> 509 { 510 public: 511 bool operator()(orxonox::ColourValue* output, const std::string& input) const 512 { 513 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 514 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 515 516 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', '"', '\0', '\0', '\0'); 517 518 if (tokens.size() >= 4) 519 { 520 if (!ConvertValue(&(output->r), tokens[0])) 521 return false; 522 if (!ConvertValue(&(output->g), tokens[1])) 523 return false; 524 if (!ConvertValue(&(output->b), tokens[2])) 525 return false; 526 if (!ConvertValue(&(output->a), tokens[3])) 527 return false; 528 529 return true; 530 } 531 532 return false; 594 struct ConverterSpecialized<std::string, orxonox::ColourValue, _Explicit_> 595 { 596 enum { specialized = true }; 597 static bool convert(orxonox::ColourValue* output, const std::string& input) 598 { 599 unsigned int opening_parenthesis, closing_parenthesis = input.find(')'); 600 if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } 601 602 SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); 603 if (tokens.size() >= 4) 604 { 605 if (!ConvertValue(&(output->r), tokens[0])) 606 return false; 607 if (!ConvertValue(&(output->g), tokens[1])) 608 return false; 609 if (!ConvertValue(&(output->b), tokens[2])) 610 return false; 611 if (!ConvertValue(&(output->a), tokens[3])) 612 return false; 613 614 return true; 615 } 616 return false; 533 617 } 534 618 };
Note: See TracChangeset
for help on using the changeset viewer.