Changeset 848 for code/branches/core/src/util/Convert.h
- Timestamp:
- Mar 5, 2008, 1:29:47 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core/src/util/Convert.h
r845 r848 38 38 #include "UtilPrereqs.h" 39 39 #include "Math.h" 40 #include "SubString.h" 41 #include "MultiTypeMath.h" 42 40 43 41 44 // DEFAULT CLASS … … 103 106 } 104 107 105 106 107 // MORE SPECIALISATIONS 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; 115 } 116 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 }; 141 142 //////////////// 143 // MULTITYPES // 144 //////////////// 145 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 false; 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 false; 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 false; 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 false; 289 } 290 }; 291 292 293 //////////////////// 294 // MATH TO STRING // 295 //////////////////// 296 108 297 // Vector2 to std::string 109 298 template <> … … 196 385 }; 197 386 387 388 //////////////////// 389 // STRING TO MATH // 390 //////////////////// 391 392 // std::string to Vector2 393 template <> 394 class Converter<std::string, orxonox::Vector2> 395 { 396 public: 397 bool operator()(orxonox::Vector2* output, const std::string& input) const 398 { 399 SubString tokens(input, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); 400 401 if (tokens.size() >= 2) 402 { 403 if (!ConvertValue(&(output->x), tokens[0])) 404 return false; 405 if (!ConvertValue(&(output->y), tokens[1])) 406 return false; 407 408 return true; 409 } 410 411 return false; 412 } 413 }; 414 415 // std::string to Vector3 416 template <> 417 class Converter<std::string, orxonox::Vector3> 418 { 419 public: 420 bool operator()(orxonox::Vector3* output, const std::string& input) const 421 { 422 SubString tokens(input, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); 423 424 if (tokens.size() >= 3) 425 { 426 if (!ConvertValue(&(output->x), tokens[0])) 427 return false; 428 if (!ConvertValue(&(output->y), tokens[1])) 429 return false; 430 if (!ConvertValue(&(output->z), tokens[2])) 431 return false; 432 433 return true; 434 } 435 436 return false; 437 } 438 }; 439 440 // std::string to Vector4 441 template <> 442 class Converter<std::string, orxonox::Vector4> 443 { 444 public: 445 bool operator()(orxonox::Vector4* output, const std::string& input) const 446 { 447 SubString tokens(input, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); 448 449 if (tokens.size() >= 4) 450 { 451 if (!ConvertValue(&(output->x), tokens[0])) 452 return false; 453 if (!ConvertValue(&(output->y), tokens[1])) 454 return false; 455 if (!ConvertValue(&(output->z), tokens[2])) 456 return false; 457 if (!ConvertValue(&(output->w), tokens[3])) 458 return false; 459 460 return true; 461 } 462 463 return false; 464 } 465 }; 466 467 // std::string to Quaternion 468 template <> 469 class Converter<std::string, orxonox::Quaternion> 470 { 471 public: 472 bool operator()(orxonox::Quaternion* output, const std::string& input) const 473 { 474 SubString tokens(input, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); 475 476 if (tokens.size() >= 4) 477 { 478 if (!ConvertValue(&(output->w), tokens[0])) 479 return false; 480 if (!ConvertValue(&(output->x), tokens[1])) 481 return false; 482 if (!ConvertValue(&(output->y), tokens[2])) 483 return false; 484 if (!ConvertValue(&(output->z), tokens[3])) 485 return false; 486 487 return true; 488 } 489 490 return false; 491 } 492 }; 493 494 // std::string to ColourValue 495 template <> 496 class Converter<std::string, orxonox::ColourValue> 497 { 498 public: 499 bool operator()(orxonox::ColourValue* output, const std::string& input) const 500 { 501 SubString tokens(input, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); 502 503 if (tokens.size() >= 4) 504 { 505 if (!ConvertValue(&(output->r), tokens[0])) 506 return false; 507 if (!ConvertValue(&(output->g), tokens[1])) 508 return false; 509 if (!ConvertValue(&(output->b), tokens[2])) 510 return false; 511 if (!ConvertValue(&(output->a), tokens[3])) 512 return false; 513 514 return true; 515 } 516 517 return false; 518 } 519 }; 520 198 521 #endif /* _Convert_H__ */
Note: See TracChangeset
for help on using the changeset viewer.