[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 | |
---|
| 144 | |
---|
[2016] | 145 | /////////////////////// |
---|
| 146 | // ConverterFallback // |
---|
| 147 | /////////////////////// |
---|
| 148 | |
---|
| 149 | // Default template for stringstream |
---|
| 150 | template <class FromType, class ToType> |
---|
| 151 | struct ConverterStringStream |
---|
[1505] | 152 | { |
---|
| 153 | static bool convert(ToType* output, const FromType& input) |
---|
| 154 | { |
---|
[2016] | 155 | return ConverterFallback<FromType, ToType>::convert(output, input); |
---|
[1505] | 156 | } |
---|
| 157 | }; |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | ///////////// |
---|
[2016] | 161 | // OStream // |
---|
[1505] | 162 | ///////////// |
---|
| 163 | |
---|
[2016] | 164 | namespace fallbackTemplates |
---|
[1505] | 165 | { |
---|
[2016] | 166 | template <class FromType> |
---|
| 167 | inline bool operator <<(std::ostream& outstream, const FromType& input) |
---|
| 168 | { |
---|
| 169 | std::string temp; |
---|
| 170 | if (ConverterFallback<FromType, std::string>::convert(&temp, input)) |
---|
| 171 | { |
---|
| 172 | std::operator <<(outstream, temp); |
---|
| 173 | return true; |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | } |
---|
[1505] | 179 | |
---|
[2016] | 180 | // template that evaluates whether we can convert to std::string via ostringstream |
---|
[1505] | 181 | template <class FromType> |
---|
[2016] | 182 | struct ConverterStringStream<FromType, std::string> |
---|
[1505] | 183 | { |
---|
| 184 | static bool convert(std::string* output, const FromType& input) |
---|
| 185 | { |
---|
[2016] | 186 | using namespace fallbackTemplates; |
---|
| 187 | // this operator call only chooses fallbackTemplates::operator<< if there's no other fitting function |
---|
[1505] | 188 | std::ostringstream oss; |
---|
| 189 | if (oss << input) |
---|
| 190 | { |
---|
| 191 | (*output) = oss.str(); |
---|
| 192 | return true; |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | return false; |
---|
| 196 | } |
---|
| 197 | }; |
---|
| 198 | |
---|
[2016] | 199 | |
---|
| 200 | ///////////// |
---|
| 201 | // IStream // |
---|
| 202 | ///////////// |
---|
| 203 | |
---|
| 204 | namespace fallbackTemplates |
---|
[1625] | 205 | { |
---|
[2016] | 206 | template <class ToType> |
---|
| 207 | inline bool operator >>(std::istream& instream, ToType& output) |
---|
| 208 | { |
---|
| 209 | return ConverterFallback<std::string, ToType> |
---|
| 210 | ::convert(&output, static_cast<std::istringstream&>(instream).str()); |
---|
| 211 | } |
---|
[1625] | 212 | } |
---|
| 213 | |
---|
[2016] | 214 | // template that evaluates whether we can convert from std::string via ostringstream |
---|
[1505] | 215 | template <class ToType> |
---|
[2016] | 216 | struct ConverterStringStream<std::string, ToType> |
---|
[1505] | 217 | { |
---|
| 218 | static bool convert(ToType* output, const std::string& input) |
---|
| 219 | { |
---|
[2016] | 220 | using namespace fallbackTemplates; |
---|
[1505] | 221 | std::istringstream iss(input); |
---|
[2016] | 222 | // this operator call only chooses fallbackTemplates::operator>> if there's no other fitting function |
---|
[1505] | 223 | if (iss >> (*output)) |
---|
[2016] | 224 | { |
---|
[1505] | 225 | return true; |
---|
[2016] | 226 | } |
---|
[1505] | 227 | else |
---|
| 228 | return false; |
---|
| 229 | } |
---|
| 230 | }; |
---|
| 231 | |
---|
[2016] | 232 | |
---|
| 233 | /////////////////// |
---|
| 234 | // Implicit Cast // |
---|
| 235 | /////////////////// |
---|
| 236 | |
---|
| 237 | // implicit cast not possible, try stringstream conversion next |
---|
| 238 | template <class FromType, class ToType> |
---|
| 239 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) |
---|
[1625] | 240 | { |
---|
[2016] | 241 | return ConverterStringStream<FromType, ToType>::convert(output, input); |
---|
[1625] | 242 | } |
---|
[1505] | 243 | |
---|
[2016] | 244 | // We can cast implicitely |
---|
| 245 | template <class FromType, class ToType> |
---|
| 246 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<true>) |
---|
| 247 | { |
---|
| 248 | (*output) = static_cast<ToType>(input); |
---|
| 249 | return true; |
---|
| 250 | } |
---|
[1625] | 251 | |
---|
[2016] | 252 | |
---|
| 253 | //////////////////////////////// |
---|
| 254 | // ConverterExplicit Fallback // |
---|
| 255 | //////////////////////////////// |
---|
| 256 | |
---|
| 257 | // Default template if no specialisation is available |
---|
| 258 | template <class FromType, class ToType> |
---|
| 259 | struct ConverterExplicit |
---|
[1505] | 260 | { |
---|
[2016] | 261 | static bool convert(ToType* output, const FromType& input) |
---|
[1505] | 262 | { |
---|
[2016] | 263 | // Try implict cast and probe first. If a simple cast is not possible, it will not compile |
---|
| 264 | // We therefore have to out source it into another template function |
---|
| 265 | const bool probe = ImplicitConversion<FromType, ToType>::exists; |
---|
| 266 | return convertImplicitely(output, input, ::Int2Type<probe>()); |
---|
[1505] | 267 | } |
---|
| 268 | }; |
---|
| 269 | |
---|
[2016] | 270 | |
---|
| 271 | ////////////////////// |
---|
| 272 | // Public Functions // |
---|
| 273 | ////////////////////// |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | @brief |
---|
| 277 | Converts any value to any other as long as there exists a conversion. |
---|
| 278 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 279 | For information about the different conversion methods (user defined too), see the section |
---|
| 280 | 'Actual conversion sequence' in this file above. |
---|
| 281 | */ |
---|
| 282 | template <class FromType, class ToType> |
---|
| 283 | inline bool convertValue(ToType* output, const FromType& input) |
---|
[1505] | 284 | { |
---|
[2016] | 285 | return ConverterExplicit<FromType, ToType>::convert(output, input); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | // For compatibility reasons. The same, but with capital ConvertValue |
---|
| 289 | template<class FromType, class ToType> |
---|
| 290 | inline bool ConvertValue(ToType* output, const FromType& input) |
---|
| 291 | { |
---|
| 292 | return convertValue(output, input); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | // Calls convertValue and returns true if the conversion was successful. |
---|
| 296 | // Otherwise the fallback is used. |
---|
| 297 | /** |
---|
| 298 | @brief |
---|
| 299 | Converts any value to any other as long as there exists a conversion. |
---|
| 300 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 301 | For information about the different conversion methods (user defined too), see the section |
---|
| 302 | 'Actual conversion sequence' in this file above. |
---|
| 303 | If the conversion doesn't succeed, 'fallback' is written to '*output'. |
---|
| 304 | @param fallback |
---|
| 305 | A default value that gets written to '*output' if there is no conversion. |
---|
| 306 | */ |
---|
| 307 | template<class FromType, class ToType> |
---|
| 308 | inline bool convertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
| 309 | { |
---|
| 310 | if (convertValue(output, input)) |
---|
| 311 | return true; |
---|
| 312 | else |
---|
[1505] | 313 | { |
---|
[2016] | 314 | (*output) = fallback; |
---|
| 315 | return false; |
---|
[1505] | 316 | } |
---|
[2016] | 317 | } |
---|
[1505] | 318 | |
---|
[2016] | 319 | // for compatibility reason. (capital 'c' in ConvertValue) |
---|
| 320 | template<class FromType, class ToType> |
---|
| 321 | inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
| 322 | { |
---|
| 323 | return convertValue(output, input, fallback); |
---|
| 324 | } |
---|
[1505] | 325 | |
---|
[2016] | 326 | // Directly returns the converted value, even if the conversion was not successful. |
---|
| 327 | template<class FromType, class ToType> |
---|
| 328 | inline ToType getConvertedValue(const FromType& input) |
---|
| 329 | { |
---|
| 330 | ToType output; |
---|
| 331 | convertValue(&output, input); |
---|
| 332 | return output; |
---|
| 333 | } |
---|
[1505] | 334 | |
---|
[2016] | 335 | // Directly returns the converted value, but uses the fallback on failure. |
---|
| 336 | template<class FromType, class ToType> |
---|
| 337 | inline ToType getConvertedValue(const FromType& input, const ToType& fallback) |
---|
[1625] | 338 | { |
---|
[2016] | 339 | ToType output; |
---|
| 340 | convertValue(&output, input, fallback); |
---|
| 341 | return output; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | // Like getConvertedValue, but the template argument order is in reverse. |
---|
| 345 | // That means you can call it exactly like static_cast<ToType>(fromTypeValue). |
---|
| 346 | template<class ToType, class FromType> |
---|
| 347 | inline ToType conversion_cast(const FromType& input) |
---|
| 348 | { |
---|
| 349 | ToType output; |
---|
| 350 | convertValue(&output, input); |
---|
| 351 | return output; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | // convert to string Shortcut |
---|
| 355 | template <class FromType> |
---|
| 356 | std::string convertToString(FromType value) |
---|
| 357 | { |
---|
| 358 | return getConvertedValue<FromType, std::string>(value); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | // convert from string Shortcut |
---|
| 362 | template <class ToType> |
---|
| 363 | ToType convertFromString(std::string str) |
---|
| 364 | { |
---|
| 365 | return getConvertedValue<std::string, ToType>(str); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | //////////////////////////////// |
---|
| 369 | // Special string conversions // |
---|
| 370 | //////////////////////////////// |
---|
| 371 | |
---|
| 372 | // Delegate conversion from const char* to std::string |
---|
| 373 | template <class ToType> |
---|
| 374 | struct ConverterExplicit<const char*, ToType> |
---|
| 375 | { |
---|
| 376 | static bool convert(ToType* output, const char* input) |
---|
[1625] | 377 | { |
---|
[2016] | 378 | return convertValue<std::string, ToType>(output, input); |
---|
[1625] | 379 | } |
---|
| 380 | }; |
---|
| 381 | |
---|
[2016] | 382 | // These conversions would exhibit ambiguous << or >> operators when using stringstream |
---|
[1505] | 383 | template <> |
---|
[2016] | 384 | struct ConverterExplicit<char, std::string> |
---|
[1505] | 385 | { |
---|
[2016] | 386 | static bool convert(std::string* output, const char input) |
---|
[1505] | 387 | { |
---|
[2016] | 388 | *output = std::string(1, input); |
---|
| 389 | return true; |
---|
[1505] | 390 | } |
---|
| 391 | }; |
---|
| 392 | template <> |
---|
[2016] | 393 | struct ConverterExplicit<unsigned char, std::string> |
---|
[1505] | 394 | { |
---|
[2016] | 395 | static bool convert(std::string* output, const unsigned char input) |
---|
[1505] | 396 | { |
---|
[2016] | 397 | *output = std::string(1, input); |
---|
| 398 | return true; |
---|
[1505] | 399 | } |
---|
| 400 | }; |
---|
| 401 | template <> |
---|
[2016] | 402 | struct ConverterExplicit<std::string, char> |
---|
[1505] | 403 | { |
---|
[2016] | 404 | static bool convert(char* output, const std::string input) |
---|
[1505] | 405 | { |
---|
[2016] | 406 | if (input != "") |
---|
| 407 | *output = input[0]; |
---|
| 408 | else |
---|
| 409 | *output = '\0'; |
---|
| 410 | return true; |
---|
[1505] | 411 | } |
---|
| 412 | }; |
---|
| 413 | template <> |
---|
[2016] | 414 | struct ConverterExplicit<std::string, unsigned char> |
---|
[1505] | 415 | { |
---|
[2016] | 416 | static bool convert(unsigned char* output, const std::string input) |
---|
[1505] | 417 | { |
---|
[2016] | 418 | if (input != "") |
---|
| 419 | *output = input[0]; |
---|
| 420 | else |
---|
| 421 | *output = '\0'; |
---|
| 422 | return true; |
---|
[1505] | 423 | } |
---|
| 424 | }; |
---|
| 425 | |
---|
[2016] | 426 | |
---|
| 427 | // bool to std::string |
---|
[1505] | 428 | template <> |
---|
[2016] | 429 | struct ConverterExplicit<bool, std::string> |
---|
[1505] | 430 | { |
---|
[2016] | 431 | static bool convert(std::string* output, const bool& input) |
---|
[1505] | 432 | { |
---|
[2016] | 433 | if (input) |
---|
| 434 | *output = "true"; |
---|
| 435 | else |
---|
| 436 | *output = "false"; |
---|
[1505] | 437 | return false; |
---|
| 438 | } |
---|
| 439 | }; |
---|
| 440 | |
---|
[1625] | 441 | // std::string to bool |
---|
| 442 | template <> |
---|
[2016] | 443 | struct ConverterExplicit<std::string, bool> |
---|
[1625] | 444 | { |
---|
| 445 | static bool convert(bool* output, const std::string& input) |
---|
| 446 | { |
---|
| 447 | std::string stripped = getLowercase(removeTrailingWhitespaces(input)); |
---|
| 448 | if (stripped == "true" || stripped == "on" || stripped == "yes") |
---|
| 449 | { |
---|
| 450 | *output = true; |
---|
| 451 | return true; |
---|
| 452 | } |
---|
| 453 | else if (stripped == "false" || stripped == "off" || stripped == "no") |
---|
| 454 | { |
---|
| 455 | *output = false; |
---|
| 456 | return true; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | std::istringstream iss(input); |
---|
| 460 | if (iss >> (*output)) |
---|
| 461 | return true; |
---|
| 462 | else |
---|
| 463 | return false; |
---|
| 464 | } |
---|
| 465 | }; |
---|
| 466 | |
---|
[1052] | 467 | #endif /* _Convert_H__ */ |
---|