Changeset 3196 for code/trunk/src/util/Convert.h
- Timestamp:
- Jun 20, 2009, 9:20:47 AM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/pch (added) merged: 3114-3118,3124-3125,3127-3131,3133,3138-3194
- Property svn:mergeinfo changed
-
code/trunk/src/util/Convert.h
r2710 r3196 40 40 #include <string> 41 41 #include <sstream> 42 #include <istream>43 #include <ostream>44 42 #include <typeinfo> 45 43 … … 123 121 namespace orxonox 124 122 { 125 namespace 123 namespace detail 126 124 { 127 125 //! Little template that maps integers to entire types (Alexandrescu 2001) … … 139 137 struct ConverterFallback 140 138 { 141 static bool convert(ToType* output, const FromType& input)139 FORCEINLINE static bool convert(ToType* output, const FromType& input) 142 140 { 143 141 COUT(2) << "Could not convert value of type " << typeid(FromType).name() … … 151 149 struct ConverterFallback<FromType*, ToType*> 152 150 { 153 static bool convert(ToType** output, FromType* const input)151 FORCEINLINE static bool convert(ToType** output, FromType* const input) 154 152 { 155 153 ToType* temp = dynamic_cast<ToType*>(input); … … 174 172 struct ConverterStringStream 175 173 { 176 static bool convert(ToType* output, const FromType& input)174 FORCEINLINE static bool convert(ToType* output, const FromType& input) 177 175 { 178 176 return orxonox::ConverterFallback<FromType, ToType>::convert(output, input); … … 188 186 { 189 187 template <class FromType> 190 inlinebool operator <<(std::ostream& outstream, const FromType& input)188 FORCEINLINE bool operator <<(std::ostream& outstream, const FromType& input) 191 189 { 192 190 std::string temp; … … 205 203 struct ConverterStringStream<FromType, std::string> 206 204 { 207 static bool convert(std::string* output, const FromType& input)205 FORCEINLINE static bool convert(std::string* output, const FromType& input) 208 206 { 209 207 using namespace fallbackTemplates; … … 228 226 { 229 227 template <class ToType> 230 inlinebool operator >>(std::istream& instream, ToType& output)228 FORCEINLINE bool operator >>(std::istream& instream, ToType& output) 231 229 { 232 230 return orxonox::ConverterFallback<std::string, ToType> … … 239 237 struct ConverterStringStream<std::string, ToType> 240 238 { 241 static bool convert(ToType* output, const std::string& input)239 FORCEINLINE static bool convert(ToType* output, const std::string& input) 242 240 { 243 241 using namespace fallbackTemplates; … … 262 260 // implicit cast not possible, try stringstream conversion next 263 261 template <class FromType, class ToType> 264 inline bool convertImplicitely(ToType* output, const FromType& input, orxonox::Int2Type<false>)262 FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, detail::Int2Type<false>) 265 263 { 266 264 return ConverterStringStream<FromType, ToType>::convert(output, input); … … 269 267 // We can cast implicitely 270 268 template <class FromType, class ToType> 271 inline bool convertImplicitely(ToType* output, const FromType& input, orxonox::Int2Type<true>)269 FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, detail::Int2Type<true>) 272 270 { 273 271 (*output) = static_cast<ToType>(input); … … 284 282 struct ConverterExplicit 285 283 { 286 static bool convert(ToType* output, const FromType& input)284 FORCEINLINE static bool convert(ToType* output, const FromType& input) 287 285 { 288 286 // Try implict cast and probe first. If a simple cast is not possible, it will not compile 289 287 // We therefore have to out source it into another template function 290 288 const bool probe = ImplicitConversion<FromType, ToType>::exists; 291 return convertImplicitely(output, input, orxonox::Int2Type<probe>());289 return convertImplicitely(output, input, detail::Int2Type<probe>()); 292 290 } 293 291 }; … … 306 304 */ 307 305 template <class FromType, class ToType> 308 inlinebool convertValue(ToType* output, const FromType& input)306 FORCEINLINE bool convertValue(ToType* output, const FromType& input) 309 307 { 310 308 return ConverterExplicit<FromType, ToType>::convert(output, input); 311 }312 313 // For compatibility reasons. The same, but with capital ConvertValue314 template<class FromType, class ToType>315 inline bool ConvertValue(ToType* output, const FromType& input)316 {317 return convertValue(output, input);318 309 } 319 310 … … 331 322 */ 332 323 template<class FromType, class ToType> 333 inlinebool convertValue(ToType* output, const FromType& input, const ToType& fallback)324 FORCEINLINE bool convertValue(ToType* output, const FromType& input, const ToType& fallback) 334 325 { 335 326 if (convertValue(output, input)) … … 342 333 } 343 334 344 // for compatibility reason. (capital 'c' in ConvertValue)345 template<class FromType, class ToType>346 inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback)347 {348 return convertValue(output, input, fallback);349 }350 351 335 // Directly returns the converted value, even if the conversion was not successful. 352 336 template<class FromType, class ToType> 353 inlineToType getConvertedValue(const FromType& input)337 FORCEINLINE ToType getConvertedValue(const FromType& input) 354 338 { 355 339 ToType output; … … 360 344 // Directly returns the converted value, but uses the fallback on failure. 361 345 template<class FromType, class ToType> 362 inlineToType getConvertedValue(const FromType& input, const ToType& fallback)346 FORCEINLINE ToType getConvertedValue(const FromType& input, const ToType& fallback) 363 347 { 364 348 ToType output; … … 370 354 // That means you can call it exactly like static_cast<ToType>(fromTypeValue). 371 355 template<class ToType, class FromType> 372 inline ToType omni_cast(const FromType& input)356 FORCEINLINE ToType multi_cast(const FromType& input) 373 357 { 374 358 ToType output; … … 379 363 // convert to string Shortcut 380 364 template <class FromType> 381 inlinestd::string convertToString(FromType value)382 { 383 return getConvertedValue<FromType, std::string>(value);365 FORCEINLINE std::string convertToString(FromType value) 366 { 367 return getConvertedValue<FromType, std::string>(value); 384 368 } 385 369 386 370 // convert from string Shortcut 387 371 template <class ToType> 388 inlineToType convertFromString(std::string str)389 { 390 return getConvertedValue<std::string, ToType>(str);372 FORCEINLINE ToType convertFromString(std::string str) 373 { 374 return getConvertedValue<std::string, ToType>(str); 391 375 } 392 376 … … 399 383 struct ConverterExplicit<const char*, ToType> 400 384 { 401 static bool convert(ToType* output, const char* input)385 FORCEINLINE static bool convert(ToType* output, const char* input) 402 386 { 403 387 return convertValue<std::string, ToType>(output, input); … … 409 393 struct ConverterExplicit<char, std::string> 410 394 { 411 static bool convert(std::string* output, const char input)395 FORCEINLINE static bool convert(std::string* output, const char input) 412 396 { 413 397 *output = std::string(1, input); … … 418 402 struct ConverterExplicit<unsigned char, std::string> 419 403 { 420 static bool convert(std::string* output, const unsigned char input)404 FORCEINLINE static bool convert(std::string* output, const unsigned char input) 421 405 { 422 406 *output = std::string(1, input); … … 427 411 struct ConverterExplicit<std::string, char> 428 412 { 429 static bool convert(char* output, const std::string input)413 FORCEINLINE static bool convert(char* output, const std::string input) 430 414 { 431 415 if (input != "") … … 439 423 struct ConverterExplicit<std::string, unsigned char> 440 424 { 441 static bool convert(unsigned char* output, const std::string input)425 FORCEINLINE static bool convert(unsigned char* output, const std::string input) 442 426 { 443 427 if (input != "") … … 454 438 struct ConverterExplicit<bool, std::string> 455 439 { 456 static bool convert(std::string* output, const bool& input)440 FORCEINLINE static bool convert(std::string* output, const bool& input) 457 441 { 458 442 if (input)
Note: See TracChangeset
for help on using the changeset viewer.