[1052] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 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: |
---|
[2087] | 23 | * Reto Grieder |
---|
[1791] | 24 | * Fabian 'x3n' Landau |
---|
[1052] | 25 | * Benjamin Grauer |
---|
[1505] | 26 | * Co-authors: |
---|
[1052] | 27 | * ... |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | /*! |
---|
[2087] | 31 | @file |
---|
[1052] | 32 | @brief Definition and Implementation of the Convert class. |
---|
| 33 | */ |
---|
| 34 | |
---|
[2087] | 35 | #ifndef _Converter_H__ |
---|
| 36 | #define _Converter_H__ |
---|
[1052] | 37 | |
---|
[1062] | 38 | #include "UtilPrereqs.h" |
---|
| 39 | |
---|
[1052] | 40 | #include <string> |
---|
| 41 | #include <sstream> |
---|
[1837] | 42 | #include <typeinfo> |
---|
[1052] | 43 | |
---|
[1747] | 44 | #include "Debug.h" |
---|
[3232] | 45 | #include "TemplateUtils.h" |
---|
[1064] | 46 | |
---|
[2087] | 47 | //////////////////////////////////// |
---|
| 48 | //// ACTUAL CONVERSION SEQUENCE //// |
---|
| 49 | //////////////////////////////////// |
---|
| 50 | /* |
---|
| 51 | There is a distinct priority when choosing the right conversion function: |
---|
| 52 | Overwrite: |
---|
| 53 | 1. (Partial) template specialisation of ConverterExplicit::convert() |
---|
| 54 | Fallbacks: |
---|
| 55 | 2. Any possible implicit conversion. This includes 'FooBar' --> 'int' if FooBar defines operator float(). |
---|
| 56 | 3. Global or member operators for stringstream when converting from or to std::string (or FROM const char*) |
---|
| 57 | 4. (Partial) template specialisation of ConverterFallback::convert() |
---|
| 58 | 5. Function that simply displays "Could not convert value" with type information obtained from typeid(). |
---|
[1505] | 59 | |
---|
[2087] | 60 | Notes: |
---|
| 61 | There has to be an exact type match when using template specialisations. |
---|
| 62 | Template specialisations can be defined after including this file. Any implicit cast function or iostream |
---|
| 63 | operator has to be declared BEFORE this file gets parsed. |
---|
[1505] | 64 | |
---|
[2087] | 65 | Defining your own functions: |
---|
| 66 | There are obviously 4 ways to specifiy a user defined conversion. What should I use? |
---|
[1505] | 67 | |
---|
[2087] | 68 | Usually, ConverterFallback fits quite well. You won't have to deal with the conversion from |
---|
| 69 | 'MyClass' to 'MyClass' by using another explicit template specialisation to avoid ambiguities. |
---|
[1505] | 70 | |
---|
[2087] | 71 | However if you want to overwrite an implicit conversion or an iostream operator, you really need to |
---|
| 72 | make use of ConverterExplicit. |
---|
| 73 | */ |
---|
| 74 | |
---|
[2171] | 75 | namespace orxonox |
---|
[1505] | 76 | { |
---|
[3196] | 77 | namespace detail |
---|
[2171] | 78 | { |
---|
| 79 | //! Little template that maps integers to entire types (Alexandrescu 2001) |
---|
| 80 | template <int I> |
---|
| 81 | struct Int2Type { }; |
---|
| 82 | } |
---|
[1505] | 83 | |
---|
| 84 | |
---|
[2171] | 85 | /////////////////// |
---|
| 86 | // No Conversion // |
---|
| 87 | /////////////////// |
---|
[1505] | 88 | |
---|
[2171] | 89 | // Default template. No conversion available at all. |
---|
| 90 | template <class FromType, class ToType> |
---|
| 91 | struct ConverterFallback |
---|
[1505] | 92 | { |
---|
[3196] | 93 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[2171] | 94 | { |
---|
| 95 | COUT(2) << "Could not convert value of type " << typeid(FromType).name() |
---|
| 96 | << " to type " << typeid(ToType).name() << std::endl; |
---|
| 97 | return false; |
---|
| 98 | } |
---|
| 99 | }; |
---|
[2087] | 100 | |
---|
[2171] | 101 | // If all else fails, try a dynamic_cast for pointer types. |
---|
| 102 | template <class FromType, class ToType> |
---|
| 103 | struct ConverterFallback<FromType*, ToType*> |
---|
[1505] | 104 | { |
---|
[3196] | 105 | FORCEINLINE static bool convert(ToType** output, FromType* const input) |
---|
[2087] | 106 | { |
---|
[2171] | 107 | ToType* temp = dynamic_cast<ToType*>(input); |
---|
| 108 | if (temp) |
---|
| 109 | { |
---|
| 110 | *output = temp; |
---|
| 111 | return true; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | return false; |
---|
[2087] | 115 | } |
---|
[2171] | 116 | }; |
---|
[6452] | 117 | |
---|
| 118 | //////////// |
---|
| 119 | // upcast // |
---|
| 120 | //////////// |
---|
| 121 | namespace detail |
---|
| 122 | { |
---|
| 123 | // perform a static cast if ToType is a base of FromType |
---|
| 124 | template<class ToType, class FromType> |
---|
| 125 | FORCEINLINE ToType upcast(FromType input, detail::Int2Type<true>) |
---|
| 126 | { |
---|
| 127 | return static_cast<ToType>(input); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // return zero if ToType is not a base of FromType |
---|
| 131 | template<class ToType, class FromType> |
---|
| 132 | FORCEINLINE ToType upcast(FromType input, detail::Int2Type<false>) |
---|
| 133 | { |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // performs an upcast if ToType is a base of FromType, returns zero otherwise |
---|
| 139 | template <class ToType, class FromType> |
---|
| 140 | FORCEINLINE ToType upcast(FromType input) |
---|
| 141 | { |
---|
| 142 | enum { probe = ImplicitConversion<FromType, ToType>::exists }; |
---|
| 143 | return detail::upcast<ToType, FromType>(input, detail::Int2Type<probe>()); |
---|
| 144 | } |
---|
[2171] | 145 | } |
---|
[1505] | 146 | |
---|
| 147 | |
---|
[2087] | 148 | /////////////////////// |
---|
| 149 | // ConverterFallback // |
---|
| 150 | /////////////////////// |
---|
| 151 | |
---|
| 152 | // Default template for stringstream |
---|
| 153 | template <class FromType, class ToType> |
---|
| 154 | struct ConverterStringStream |
---|
[1505] | 155 | { |
---|
[3196] | 156 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[1505] | 157 | { |
---|
[2171] | 158 | return orxonox::ConverterFallback<FromType, ToType>::convert(output, input); |
---|
[1505] | 159 | } |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | ///////////// |
---|
[2087] | 164 | // OStream // |
---|
[1505] | 165 | ///////////// |
---|
| 166 | |
---|
[2087] | 167 | namespace fallbackTemplates |
---|
[1505] | 168 | { |
---|
[2087] | 169 | template <class FromType> |
---|
[3196] | 170 | FORCEINLINE bool operator <<(std::ostream& outstream, const FromType& input) |
---|
[2087] | 171 | { |
---|
| 172 | std::string temp; |
---|
[2171] | 173 | if (orxonox::ConverterFallback<FromType, std::string>::convert(&temp, input)) |
---|
[2087] | 174 | { |
---|
| 175 | std::operator <<(outstream, temp); |
---|
| 176 | return true; |
---|
| 177 | } |
---|
| 178 | else |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | } |
---|
[1505] | 182 | |
---|
[2087] | 183 | // template that evaluates whether we can convert to std::string via ostringstream |
---|
[1505] | 184 | template <class FromType> |
---|
[2087] | 185 | struct ConverterStringStream<FromType, std::string> |
---|
[1505] | 186 | { |
---|
[3196] | 187 | FORCEINLINE static bool convert(std::string* output, const FromType& input) |
---|
[1505] | 188 | { |
---|
[2087] | 189 | using namespace fallbackTemplates; |
---|
| 190 | // this operator call only chooses fallbackTemplates::operator<< if there's no other fitting function |
---|
[1505] | 191 | std::ostringstream oss; |
---|
| 192 | if (oss << input) |
---|
| 193 | { |
---|
| 194 | (*output) = oss.str(); |
---|
| 195 | return true; |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | return false; |
---|
| 199 | } |
---|
| 200 | }; |
---|
| 201 | |
---|
[2087] | 202 | |
---|
| 203 | ///////////// |
---|
| 204 | // IStream // |
---|
| 205 | ///////////// |
---|
| 206 | |
---|
| 207 | namespace fallbackTemplates |
---|
[1625] | 208 | { |
---|
[2087] | 209 | template <class ToType> |
---|
[3196] | 210 | FORCEINLINE bool operator >>(std::istream& instream, ToType& output) |
---|
[2087] | 211 | { |
---|
[2171] | 212 | return orxonox::ConverterFallback<std::string, ToType> |
---|
[2087] | 213 | ::convert(&output, static_cast<std::istringstream&>(instream).str()); |
---|
| 214 | } |
---|
[1625] | 215 | } |
---|
| 216 | |
---|
[2087] | 217 | // template that evaluates whether we can convert from std::string via ostringstream |
---|
[1505] | 218 | template <class ToType> |
---|
[2087] | 219 | struct ConverterStringStream<std::string, ToType> |
---|
[1505] | 220 | { |
---|
[3196] | 221 | FORCEINLINE static bool convert(ToType* output, const std::string& input) |
---|
[1505] | 222 | { |
---|
[2087] | 223 | using namespace fallbackTemplates; |
---|
[1505] | 224 | std::istringstream iss(input); |
---|
[2087] | 225 | // this operator call only chooses fallbackTemplates::operator>> if there's no other fitting function |
---|
[1505] | 226 | if (iss >> (*output)) |
---|
[2087] | 227 | { |
---|
[1505] | 228 | return true; |
---|
[2087] | 229 | } |
---|
[1505] | 230 | else |
---|
| 231 | return false; |
---|
| 232 | } |
---|
| 233 | }; |
---|
| 234 | |
---|
[2171] | 235 | namespace orxonox |
---|
[1625] | 236 | { |
---|
[1505] | 237 | |
---|
[2171] | 238 | /////////////////// |
---|
| 239 | // Implicit Cast // |
---|
| 240 | /////////////////// |
---|
[1625] | 241 | |
---|
[2171] | 242 | // implicit cast not possible, try stringstream conversion next |
---|
| 243 | template <class FromType, class ToType> |
---|
[3196] | 244 | FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, detail::Int2Type<false>) |
---|
[1505] | 245 | { |
---|
[2171] | 246 | return ConverterStringStream<FromType, ToType>::convert(output, input); |
---|
[1505] | 247 | } |
---|
| 248 | |
---|
[2171] | 249 | // We can cast implicitely |
---|
| 250 | template <class FromType, class ToType> |
---|
[3196] | 251 | FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, detail::Int2Type<true>) |
---|
[2171] | 252 | { |
---|
| 253 | (*output) = static_cast<ToType>(input); |
---|
[2087] | 254 | return true; |
---|
[1505] | 255 | } |
---|
| 256 | |
---|
| 257 | |
---|
[2171] | 258 | //////////////////////////////// |
---|
| 259 | // ConverterExplicit Fallback // |
---|
| 260 | //////////////////////////////// |
---|
[1505] | 261 | |
---|
[2171] | 262 | // Default template if no specialisation is available |
---|
| 263 | template <class FromType, class ToType> |
---|
| 264 | struct ConverterExplicit |
---|
| 265 | { |
---|
[3234] | 266 | enum { probe = ImplicitConversion<FromType, ToType>::exists }; |
---|
[3196] | 267 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[2171] | 268 | { |
---|
| 269 | // Try implict cast and probe first. If a simple cast is not possible, it will not compile |
---|
| 270 | // We therefore have to out source it into another template function |
---|
[3196] | 271 | return convertImplicitely(output, input, detail::Int2Type<probe>()); |
---|
[2171] | 272 | } |
---|
| 273 | }; |
---|
[2087] | 274 | |
---|
| 275 | |
---|
[2171] | 276 | ////////////////////// |
---|
| 277 | // Public Functions // |
---|
| 278 | ////////////////////// |
---|
[2087] | 279 | |
---|
[2171] | 280 | /** |
---|
| 281 | @brief |
---|
| 282 | Converts any value to any other as long as there exists a conversion. |
---|
| 283 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 284 | For information about the different conversion methods (user defined too), see the section |
---|
| 285 | 'Actual conversion sequence' in this file above. |
---|
| 286 | */ |
---|
| 287 | template <class FromType, class ToType> |
---|
[3196] | 288 | FORCEINLINE bool convertValue(ToType* output, const FromType& input) |
---|
[2171] | 289 | { |
---|
| 290 | return ConverterExplicit<FromType, ToType>::convert(output, input); |
---|
| 291 | } |
---|
[2087] | 292 | |
---|
[2171] | 293 | // Calls convertValue and returns true if the conversion was successful. |
---|
| 294 | // Otherwise the fallback is used. |
---|
| 295 | /** |
---|
| 296 | @brief |
---|
| 297 | Converts any value to any other as long as there exists a conversion. |
---|
| 298 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 299 | For information about the different conversion methods (user defined too), see the section |
---|
| 300 | 'Actual conversion sequence' in this file above. |
---|
| 301 | If the conversion doesn't succeed, 'fallback' is written to '*output'. |
---|
| 302 | @param fallback |
---|
| 303 | A default value that gets written to '*output' if there is no conversion. |
---|
| 304 | */ |
---|
| 305 | template<class FromType, class ToType> |
---|
[3196] | 306 | FORCEINLINE bool convertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
[1625] | 307 | { |
---|
[2171] | 308 | if (convertValue(output, input)) |
---|
| 309 | return true; |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | (*output) = fallback; |
---|
| 313 | return false; |
---|
| 314 | } |
---|
[1625] | 315 | } |
---|
| 316 | |
---|
[2171] | 317 | // Directly returns the converted value, even if the conversion was not successful. |
---|
| 318 | template<class FromType, class ToType> |
---|
[3196] | 319 | FORCEINLINE ToType getConvertedValue(const FromType& input) |
---|
[1505] | 320 | { |
---|
[2171] | 321 | ToType output; |
---|
| 322 | convertValue(&output, input); |
---|
| 323 | return output; |
---|
[1505] | 324 | } |
---|
[2171] | 325 | |
---|
| 326 | // Directly returns the converted value, but uses the fallback on failure. |
---|
| 327 | template<class FromType, class ToType> |
---|
[3196] | 328 | FORCEINLINE ToType getConvertedValue(const FromType& input, const ToType& fallback) |
---|
[1505] | 329 | { |
---|
[2171] | 330 | ToType output; |
---|
| 331 | convertValue(&output, input, fallback); |
---|
| 332 | return output; |
---|
[1505] | 333 | } |
---|
[2171] | 334 | |
---|
| 335 | // Like getConvertedValue, but the template argument order is in reverse. |
---|
| 336 | // That means you can call it exactly like static_cast<ToType>(fromTypeValue). |
---|
| 337 | template<class ToType, class FromType> |
---|
[3196] | 338 | FORCEINLINE ToType multi_cast(const FromType& input) |
---|
[1505] | 339 | { |
---|
[2171] | 340 | ToType output; |
---|
| 341 | convertValue(&output, input); |
---|
| 342 | return output; |
---|
[1505] | 343 | } |
---|
| 344 | |
---|
[2171] | 345 | //////////////////////////////// |
---|
| 346 | // Special string conversions // |
---|
| 347 | //////////////////////////////// |
---|
| 348 | |
---|
| 349 | // Delegate conversion from const char* to std::string |
---|
| 350 | template <class ToType> |
---|
| 351 | struct ConverterExplicit<const char*, ToType> |
---|
[1625] | 352 | { |
---|
[3196] | 353 | FORCEINLINE static bool convert(ToType* output, const char* input) |
---|
[1625] | 354 | { |
---|
[2171] | 355 | return convertValue<std::string, ToType>(output, input); |
---|
[1625] | 356 | } |
---|
[2171] | 357 | }; |
---|
| 358 | |
---|
| 359 | // These conversions would exhibit ambiguous << or >> operators when using stringstream |
---|
| 360 | template <> |
---|
| 361 | struct ConverterExplicit<char, std::string> |
---|
| 362 | { |
---|
[3196] | 363 | FORCEINLINE static bool convert(std::string* output, const char input) |
---|
[1625] | 364 | { |
---|
[6417] | 365 | *output = input; |
---|
[2171] | 366 | return true; |
---|
[1625] | 367 | } |
---|
[2171] | 368 | }; |
---|
| 369 | template <> |
---|
| 370 | struct ConverterExplicit<unsigned char, std::string> |
---|
| 371 | { |
---|
[3196] | 372 | FORCEINLINE static bool convert(std::string* output, const unsigned char input) |
---|
[2171] | 373 | { |
---|
[6417] | 374 | *output = input; |
---|
[2171] | 375 | return true; |
---|
| 376 | } |
---|
| 377 | }; |
---|
| 378 | template <> |
---|
| 379 | struct ConverterExplicit<std::string, char> |
---|
| 380 | { |
---|
[6417] | 381 | FORCEINLINE static bool convert(char* output, const std::string& input) |
---|
[2171] | 382 | { |
---|
[6417] | 383 | if (!input.empty()) |
---|
[2171] | 384 | *output = input[0]; |
---|
| 385 | else |
---|
| 386 | *output = '\0'; |
---|
| 387 | return true; |
---|
| 388 | } |
---|
| 389 | }; |
---|
| 390 | template <> |
---|
| 391 | struct ConverterExplicit<std::string, unsigned char> |
---|
| 392 | { |
---|
[6417] | 393 | FORCEINLINE static bool convert(unsigned char* output, const std::string& input) |
---|
[2171] | 394 | { |
---|
[6417] | 395 | if (!input.empty()) |
---|
[2171] | 396 | *output = input[0]; |
---|
| 397 | else |
---|
| 398 | *output = '\0'; |
---|
| 399 | return true; |
---|
| 400 | } |
---|
| 401 | }; |
---|
[1625] | 402 | |
---|
[2171] | 403 | |
---|
| 404 | // bool to std::string |
---|
| 405 | template <> |
---|
| 406 | struct ConverterExplicit<bool, std::string> |
---|
| 407 | { |
---|
[3196] | 408 | FORCEINLINE static bool convert(std::string* output, const bool& input) |
---|
[2171] | 409 | { |
---|
| 410 | if (input) |
---|
| 411 | *output = "true"; |
---|
| 412 | else |
---|
| 413 | *output = "false"; |
---|
[2662] | 414 | return true; |
---|
[2171] | 415 | } |
---|
| 416 | }; |
---|
[1625] | 417 | |
---|
[6417] | 418 | // Declarations to avoid StringUtils.h include |
---|
| 419 | _UtilExport std::string removeTrailingWhitespaces(const std::string& str); |
---|
| 420 | _UtilExport std::string getLowercase(const std::string& str); |
---|
| 421 | |
---|
[2171] | 422 | // std::string to bool |
---|
| 423 | template <> |
---|
| 424 | struct ConverterExplicit<std::string, bool> |
---|
| 425 | { |
---|
| 426 | static bool convert(bool* output, const std::string& input) |
---|
| 427 | { |
---|
[6417] | 428 | const std::string& stripped = getLowercase(removeTrailingWhitespaces(input)); |
---|
[2171] | 429 | if (stripped == "true" || stripped == "on" || stripped == "yes") |
---|
| 430 | { |
---|
[6417] | 431 | *output = true; |
---|
| 432 | return true; |
---|
[2171] | 433 | } |
---|
| 434 | else if (stripped == "false" || stripped == "off" || stripped == "no") |
---|
| 435 | { |
---|
[6417] | 436 | *output = false; |
---|
| 437 | return true; |
---|
[2171] | 438 | } |
---|
| 439 | |
---|
| 440 | std::istringstream iss(input); |
---|
| 441 | if (iss >> (*output)) |
---|
| 442 | return true; |
---|
| 443 | else |
---|
| 444 | return false; |
---|
| 445 | } |
---|
| 446 | }; |
---|
| 447 | } |
---|
| 448 | |
---|
[1052] | 449 | #endif /* _Convert_H__ */ |
---|