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