[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 |
---|
[1777] | 25 | * Reto Grieder (direct conversion tests) |
---|
[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 | |
---|
[1753] | 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 the warnings. |
---|
| 49 | // They only occur in when using the ImplicitConversion template. |
---|
[1777] | 50 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_GNUC |
---|
[1753] | 51 | # pragma GCC system_header |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | /////////////////////////////////////////////// |
---|
| 55 | // Static detection for conversion functions // |
---|
| 56 | /////////////////////////////////////////////// |
---|
[1743] | 57 | /* The idea to use the sizeof() operator on return functions to determine function existance |
---|
| 58 | is described in 'Moder C++ design' by Alexandrescu (2001). */ |
---|
| 59 | |
---|
[1778] | 60 | // disable warnings about possible loss of data |
---|
| 61 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC |
---|
| 62 | # pragma warning(push) |
---|
| 63 | # pragma warning(disable:4244) |
---|
| 64 | #endif |
---|
| 65 | |
---|
[1743] | 66 | namespace conversionTests |
---|
| 67 | { |
---|
[1753] | 68 | // A struct that is guaranteed to be larger than any return type of our conversion functions. |
---|
| 69 | // So we simply add all the sizes of the return types plus a little bit more. |
---|
| 70 | struct VeryBigStruct |
---|
| 71 | { |
---|
| 72 | char intSize[sizeof(int)]; |
---|
| 73 | char addingMore[4096]; // just to be sure ;) |
---|
| 74 | }; |
---|
[1505] | 75 | |
---|
[1743] | 76 | template <class FromType, class ToType> |
---|
| 77 | class ImplicitConversion |
---|
| 78 | { |
---|
| 79 | private: |
---|
[1753] | 80 | ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion(); |
---|
| 81 | // Gets chosen only if there is an implicit conversion from FromType to ToType. |
---|
| 82 | static int test(ToType); |
---|
| 83 | // Accepts any argument. Why do we not use a template? The reason is that with templates, |
---|
| 84 | // the function above is only taken iff it is an exact type match. But since we want to |
---|
| 85 | // check for implicit conversion, we have to use the ellipsis. |
---|
| 86 | static VeryBigStruct test(...); |
---|
[1743] | 87 | static FromType object; // helper object to handle private c'tor and d'tor |
---|
| 88 | public: |
---|
[1777] | 89 | // test(object) only has 'VerySmallStruct' return type iff the compiler doesn't choose test(...) |
---|
[1753] | 90 | enum { exists = !(sizeof(test(object)) == sizeof(VeryBigStruct)) }; |
---|
[1743] | 91 | }; |
---|
[1778] | 92 | } |
---|
[1505] | 93 | |
---|
[1777] | 94 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC |
---|
| 95 | # pragma warning(pop) |
---|
| 96 | #endif |
---|
[1505] | 97 | |
---|
| 98 | |
---|
[1753] | 99 | //////////////////////////////////// |
---|
| 100 | //// ACTUAL CONVERSION SEQUENCE //// |
---|
| 101 | //////////////////////////////////// |
---|
| 102 | /* |
---|
| 103 | There is a distinct priority when choosing the right conversion function: |
---|
| 104 | Overwrites: |
---|
| 105 | 1. (Partial) template specialisation of ConverterExplicit::convert |
---|
[1778] | 106 | 2. Global functions convertValue(ToType* output, const FromType input) |
---|
[1753] | 107 | Fallbacks: |
---|
| 108 | 3. Any possible implicit conversion. This includes FooBar --> int if FooBar defines operator float(). |
---|
| 109 | 4. Global or member operators for stringstream when converting from or to std::string (or FROM const char*) |
---|
| 110 | 5. Function that simply displays "Could not convert value" with information obtained from typeid(). |
---|
| 111 | |
---|
| 112 | A note: There has to be an exact type match (or according to the rules of template spec.) except for 3. |
---|
| 113 | |
---|
| 114 | There are obviously a lot of ways to specifiy a user defined conversion. What should I use? |
---|
[1778] | 115 | When using any non-template function based conversion (implicit conversion, convertValue, << or >>) |
---|
[1753] | 116 | then you should consider that this function has to be defined prior to including this file. |
---|
| 117 | If you do not whish do do that, you will have to spcecialsize the ConverterExplicit template. |
---|
| 118 | There is a not so obvious advantage of the other way (non-template): You could declare each conversion function |
---|
| 119 | in the Prereqs file of the corresponding library. Then you can use the conversion everywhere. |
---|
| 120 | This is not possible with template specialisations even when defining them in this file (You would create |
---|
| 121 | a circular dependency when using a class from Core for instance, because complete template specialisations |
---|
| 122 | get compiled anyway (there is no template parameter)). |
---|
| 123 | */ |
---|
| 124 | |
---|
[1777] | 125 | namespace |
---|
[1505] | 126 | { |
---|
[1777] | 127 | // little template that maps to ints to entire types (Alexandrescu 2001) |
---|
| 128 | template <int I> |
---|
| 129 | struct Int2Type { }; |
---|
| 130 | } |
---|
[1743] | 131 | |
---|
[1753] | 132 | |
---|
[1777] | 133 | /////////////////// |
---|
| 134 | // No Conversion // |
---|
| 135 | /////////////////// |
---|
[1753] | 136 | |
---|
[1778] | 137 | // Default template for fallbackConvert, no conversion possible |
---|
| 138 | template <class ToType, class FromType> |
---|
| 139 | inline bool fallbackConversion(ToType* output, FromType input) |
---|
[1777] | 140 | { |
---|
[1778] | 141 | COUT(2) << "Could not convert value of type " << typeid(FromType).name() |
---|
| 142 | << " to type " << typeid(ToType).name() << std::endl; |
---|
| 143 | return false; |
---|
[1777] | 144 | } |
---|
[1743] | 145 | |
---|
| 146 | |
---|
[1778] | 147 | ///////////////////// |
---|
| 148 | // fallbackConvert // |
---|
| 149 | ///////////////////// |
---|
| 150 | |
---|
| 151 | // Class template used when << or >> was not available with string conversions |
---|
| 152 | // It is required to call not yet declared fallbackConvert functions. |
---|
| 153 | template <class ToType, class FromType> |
---|
| 154 | struct ConverterFallbackTemplate |
---|
| 155 | { |
---|
| 156 | static bool convert(ToType* output, const FromType& input) |
---|
| 157 | { |
---|
| 158 | return fallbackConversion(output, input); |
---|
| 159 | } |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | // Default template for stringstream |
---|
| 163 | template <class ToType, class FromType> |
---|
| 164 | struct ConverterStringStream |
---|
| 165 | { |
---|
| 166 | static bool convert(ToType* output, const FromType& input) |
---|
| 167 | { |
---|
| 168 | return fallbackConversion(output, input); |
---|
| 169 | } |
---|
| 170 | }; |
---|
| 171 | |
---|
| 172 | |
---|
[1777] | 173 | ///////////// |
---|
| 174 | // OStream // |
---|
| 175 | ///////////// |
---|
[1743] | 176 | |
---|
[1777] | 177 | namespace fallbackTemplates |
---|
| 178 | { |
---|
[1778] | 179 | template <class FromType> |
---|
| 180 | inline bool operator <<(std::ostream& outstream, const FromType& input) |
---|
[1505] | 181 | { |
---|
[1778] | 182 | std::string temp; |
---|
| 183 | if (ConverterFallbackTemplate<std::string, FromType>::convert(&temp, input)) |
---|
| 184 | { |
---|
| 185 | std::operator <<(outstream, temp); |
---|
| 186 | return true; |
---|
| 187 | } |
---|
| 188 | else |
---|
| 189 | return false; |
---|
[1505] | 190 | } |
---|
[1777] | 191 | } |
---|
[1505] | 192 | |
---|
[1778] | 193 | // template that evaluates whether OStringStream is possible for conversions to std::string |
---|
| 194 | template <class FromType> |
---|
| 195 | struct ConverterStringStream<std::string, FromType> |
---|
[1777] | 196 | { |
---|
[1778] | 197 | static bool convert(std::string* output, const FromType& input) |
---|
[1743] | 198 | { |
---|
[1778] | 199 | using namespace fallbackTemplates; |
---|
| 200 | std::ostringstream oss; |
---|
| 201 | if (oss << input) |
---|
[1743] | 202 | { |
---|
[1778] | 203 | (*output) = oss.str(); |
---|
| 204 | return true; |
---|
[1743] | 205 | } |
---|
[1778] | 206 | else |
---|
| 207 | return false; |
---|
| 208 | } |
---|
| 209 | }; |
---|
[1743] | 210 | |
---|
| 211 | |
---|
[1777] | 212 | ///////////// |
---|
| 213 | // IStream // |
---|
| 214 | ///////////// |
---|
| 215 | |
---|
| 216 | namespace fallbackTemplates |
---|
| 217 | { |
---|
[1778] | 218 | template <class ToType> |
---|
| 219 | inline bool operator >>(std::istream& instream, ToType& output) |
---|
[1743] | 220 | { |
---|
[1778] | 221 | return ConverterFallbackTemplate<ToType, std::string> |
---|
| 222 | ::convert(&output, static_cast<std::istringstream&>(instream).str()); |
---|
[1743] | 223 | } |
---|
[1777] | 224 | } |
---|
[1743] | 225 | |
---|
[1778] | 226 | // template that evaluates whether IStringStream is possible for conversions from std::string |
---|
| 227 | template <class ToType> |
---|
| 228 | struct ConverterStringStream<ToType, std::string> |
---|
[1777] | 229 | { |
---|
[1778] | 230 | static bool convert(ToType* output, const std::string& input) |
---|
[1743] | 231 | { |
---|
[1778] | 232 | using namespace fallbackTemplates; |
---|
| 233 | std::istringstream iss(input); |
---|
| 234 | if (iss >> (*output)) |
---|
[1743] | 235 | { |
---|
[1778] | 236 | return true; |
---|
[1743] | 237 | } |
---|
[1778] | 238 | else |
---|
| 239 | return false; |
---|
| 240 | } |
---|
| 241 | }; |
---|
[1743] | 242 | |
---|
| 243 | |
---|
[1777] | 244 | /////////////////// |
---|
| 245 | // Implicit Cast // |
---|
| 246 | /////////////////// |
---|
[1753] | 247 | |
---|
[1777] | 248 | // We can cast implicitely |
---|
| 249 | template <class ToType, class FromType> |
---|
| 250 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<true>) |
---|
| 251 | { |
---|
| 252 | (*output) = static_cast<ToType>(input); |
---|
| 253 | return true; |
---|
| 254 | } |
---|
[1778] | 255 | |
---|
[1777] | 256 | // static cast no possible, try stringstream conversion next |
---|
| 257 | template <class ToType, class FromType> |
---|
| 258 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) |
---|
| 259 | { |
---|
[1778] | 260 | return ConverterStringStream<ToType, FromType>::convert(output, input); |
---|
[1777] | 261 | } |
---|
[1753] | 262 | |
---|
| 263 | |
---|
[1778] | 264 | /////////////////////// |
---|
| 265 | // Explicit Fallback // |
---|
| 266 | /////////////////////// |
---|
[1753] | 267 | |
---|
[1778] | 268 | template <class ToType, class FromType> |
---|
| 269 | inline bool explicitConversion(ToType* output, const FromType& input) |
---|
[1777] | 270 | { |
---|
[1778] | 271 | // try implict conversion by probing first because we use '...' instead of a template |
---|
| 272 | const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; |
---|
| 273 | return convertImplicitely(output, input, ::Int2Type<probe>()); |
---|
[1754] | 274 | } |
---|
[1753] | 275 | |
---|
[1754] | 276 | |
---|
[1778] | 277 | // Indirect calls over a class template so we can call functions not yet declared. |
---|
[1754] | 278 | template <class ToType, class FromType> |
---|
[1778] | 279 | struct ConverterExplicitTemplate |
---|
[1754] | 280 | { |
---|
| 281 | static bool convert(ToType* output, const FromType& input) |
---|
[1753] | 282 | { |
---|
[1777] | 283 | return explicitConversion(output, input); |
---|
[1754] | 284 | } |
---|
| 285 | }; |
---|
[1505] | 286 | |
---|
| 287 | |
---|
[1753] | 288 | ////////////////////// |
---|
| 289 | // Public Functions // |
---|
| 290 | ////////////////////// |
---|
[1505] | 291 | |
---|
[1743] | 292 | /** |
---|
| 293 | @brief |
---|
[1778] | 294 | Converts any value to any other as long as there exists a conversion. |
---|
[1743] | 295 | Otherwise, the conversion will generate a runtime warning. |
---|
[1753] | 296 | For information about the different conversion methods (user defined too), see the section |
---|
| 297 | 'Actual conversion sequence' in this file above. |
---|
[1778] | 298 | @note |
---|
| 299 | This function is only a fallback if there is no appropriate 'convertValue' function. |
---|
[1743] | 300 | */ |
---|
| 301 | template <class ToType, class FromType> |
---|
| 302 | inline bool convertValue(ToType* output, const FromType& input) |
---|
| 303 | { |
---|
[1778] | 304 | return ConverterExplicitTemplate<ToType, FromType>::convert(output, input); |
---|
[1743] | 305 | } |
---|
| 306 | |
---|
[1753] | 307 | // For compatibility reasons. The same, but with capital ConvertValue |
---|
[1505] | 308 | template<class FromType, class ToType> |
---|
[1753] | 309 | inline bool ConvertValue(ToType* output, const FromType& input) |
---|
[1505] | 310 | { |
---|
[1743] | 311 | return convertValue(output, input); |
---|
[1505] | 312 | } |
---|
[1753] | 313 | |
---|
| 314 | // Calls convertValue and returns true if the conversion was successful. |
---|
| 315 | // Otherwise the fallback is used. |
---|
[1505] | 316 | template<class FromType, class ToType> |
---|
[1753] | 317 | inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
[1505] | 318 | { |
---|
[1743] | 319 | if (convertValue(output, input)) |
---|
[1505] | 320 | return true; |
---|
| 321 | |
---|
| 322 | (*output) = fallback; |
---|
| 323 | return false; |
---|
| 324 | } |
---|
| 325 | |
---|
[1753] | 326 | // Directly returns the converted value, even if the conversion was not successful. |
---|
[1505] | 327 | template<class FromType, class ToType> |
---|
[1753] | 328 | inline ToType getConvertedValue(const FromType& input) |
---|
[1505] | 329 | { |
---|
[1753] | 330 | ToType output; |
---|
[1743] | 331 | ConvertValue(&output, input); |
---|
[1505] | 332 | return output; |
---|
| 333 | } |
---|
[1753] | 334 | |
---|
| 335 | // Directly returns the converted value, but uses the fallback on failure. |
---|
[1505] | 336 | template<class FromType, class ToType> |
---|
[1753] | 337 | inline ToType getConvertedValue(const FromType& input, const ToType& fallback) |
---|
[1505] | 338 | { |
---|
[1754] | 339 | ToType output; |
---|
[1743] | 340 | ConvertValue(&output, input, fallback); |
---|
[1505] | 341 | return output; |
---|
| 342 | } |
---|
| 343 | |
---|
[1753] | 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 | // Like conversion_cast above, but uses a fallback on failure. |
---|
| 355 | template<class ToType, class FromType> |
---|
| 356 | inline ToType conversion_cast(const FromType& input, const ToType& fallback) |
---|
| 357 | { |
---|
[1754] | 358 | ToType output; |
---|
[1753] | 359 | ConvertValue(&output, input, fallback); |
---|
| 360 | return output; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | |
---|
[1777] | 364 | //////////////////////////////// |
---|
| 365 | // Special string conversions // |
---|
| 366 | //////////////////////////////// |
---|
[1753] | 367 | |
---|
[1766] | 368 | // delegate conversion from const char* to std::string |
---|
[1753] | 369 | template <class ToType> |
---|
[1777] | 370 | inline bool explicitConversion(ToType* output, const char* input) |
---|
[1753] | 371 | { |
---|
[1778] | 372 | return convertValue<ToType, std::string>(output, input); |
---|
[1777] | 373 | } |
---|
[1753] | 374 | |
---|
| 375 | // These conversions would exhibit ambiguous << or >> operators when using stringstream |
---|
[1777] | 376 | inline bool explicitConversion(std::string* output, const char input) |
---|
[1753] | 377 | { |
---|
[1777] | 378 | *output = std::string(1, input); |
---|
| 379 | return true; |
---|
| 380 | } |
---|
| 381 | inline bool explicitConversion(std::string* output, const unsigned char input) |
---|
[1753] | 382 | { |
---|
[1777] | 383 | *output = std::string(1, input); |
---|
| 384 | return true; |
---|
| 385 | } |
---|
| 386 | inline bool explicitConversion(char* output, const std::string input) |
---|
[1753] | 387 | { |
---|
[1777] | 388 | if (input != "") |
---|
| 389 | *output = input[0]; |
---|
| 390 | else |
---|
| 391 | *output = '\0'; |
---|
| 392 | return true; |
---|
| 393 | } |
---|
| 394 | inline bool explicitConversion(unsigned char* output, const std::string input) |
---|
[1753] | 395 | { |
---|
[1777] | 396 | if (input != "") |
---|
| 397 | *output = input[0]; |
---|
| 398 | else |
---|
| 399 | *output = '\0'; |
---|
| 400 | return true; |
---|
| 401 | } |
---|
[1753] | 402 | |
---|
[1052] | 403 | #endif /* _Convert_H__ */ |
---|