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