| [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 |  | 
|---|
| [1779] | 137 | // Default template. No conversion available at all. | 
|---|
| [1778] | 138 | template <class ToType, class FromType> | 
|---|
| [1779] | 139 | struct ConverterFallback | 
|---|
| [1777] | 140 | { | 
|---|
| [1778] | 141 |     static bool convert(ToType* output, const FromType& input) | 
|---|
 | 142 |     { | 
|---|
| [1779] | 143 |         COUT(2) << "Could not convert value of type " << typeid(FromType).name() | 
|---|
 | 144 |                 << " to type " << typeid(ToType).name() << std::endl; | 
|---|
 | 145 |         return false; | 
|---|
| [1778] | 146 |     } | 
|---|
 | 147 | }; | 
|---|
 | 148 |  | 
|---|
| [1779] | 149 |  | 
|---|
 | 150 | /////////////////////// | 
|---|
 | 151 | // ConverterFallback // | 
|---|
 | 152 | /////////////////////// | 
|---|
 | 153 |  | 
|---|
| [1778] | 154 | // Default template for stringstream | 
|---|
 | 155 | template <class ToType, class FromType> | 
|---|
 | 156 | struct ConverterStringStream | 
|---|
 | 157 | { | 
|---|
 | 158 |     static bool convert(ToType* output, const FromType& input) | 
|---|
 | 159 |     { | 
|---|
| [1779] | 160 |         return ConverterFallback<ToType, FromType>::convert(output, input); | 
|---|
| [1778] | 161 |     } | 
|---|
 | 162 | }; | 
|---|
 | 163 |  | 
|---|
 | 164 |  | 
|---|
| [1777] | 165 | ///////////// | 
|---|
 | 166 | // OStream // | 
|---|
 | 167 | ///////////// | 
|---|
| [1743] | 168 |  | 
|---|
| [1777] | 169 | namespace fallbackTemplates | 
|---|
 | 170 | { | 
|---|
| [1778] | 171 |     template <class FromType> | 
|---|
 | 172 |     inline bool operator <<(std::ostream& outstream,  const FromType& input) | 
|---|
| [1505] | 173 |     { | 
|---|
| [1778] | 174 |         std::string temp; | 
|---|
| [1779] | 175 |         if (ConverterFallback<std::string, FromType>::convert(&temp, input)) | 
|---|
| [1778] | 176 |         { | 
|---|
 | 177 |             std::operator <<(outstream, temp); | 
|---|
 | 178 |             return true; | 
|---|
 | 179 |         } | 
|---|
 | 180 |         else | 
|---|
 | 181 |             return false; | 
|---|
| [1505] | 182 |     } | 
|---|
| [1777] | 183 | } | 
|---|
| [1505] | 184 |  | 
|---|
| [1778] | 185 | // template that evaluates whether OStringStream is possible for conversions to std::string | 
|---|
 | 186 | template <class FromType> | 
|---|
 | 187 | struct ConverterStringStream<std::string, FromType> | 
|---|
| [1777] | 188 | { | 
|---|
| [1778] | 189 |     static bool convert(std::string* output, const FromType& input) | 
|---|
| [1743] | 190 |     { | 
|---|
| [1778] | 191 |         using namespace fallbackTemplates; | 
|---|
 | 192 |         std::ostringstream oss; | 
|---|
 | 193 |         if (oss << input) | 
|---|
| [1743] | 194 |         { | 
|---|
| [1778] | 195 |             (*output) = oss.str(); | 
|---|
 | 196 |             return true; | 
|---|
| [1743] | 197 |         } | 
|---|
| [1778] | 198 |         else | 
|---|
 | 199 |             return false; | 
|---|
 | 200 |     } | 
|---|
 | 201 | }; | 
|---|
| [1743] | 202 |  | 
|---|
 | 203 |  | 
|---|
| [1777] | 204 | ///////////// | 
|---|
 | 205 | // IStream // | 
|---|
 | 206 | ///////////// | 
|---|
 | 207 |  | 
|---|
 | 208 | namespace fallbackTemplates | 
|---|
 | 209 | { | 
|---|
| [1778] | 210 |     template <class ToType> | 
|---|
 | 211 |     inline bool operator >>(std::istream& instream, ToType& output) | 
|---|
| [1743] | 212 |     { | 
|---|
| [1779] | 213 |         return ConverterFallback<ToType, std::string> | 
|---|
| [1778] | 214 |             ::convert(&output, static_cast<std::istringstream&>(instream).str()); | 
|---|
| [1743] | 215 |     } | 
|---|
| [1777] | 216 | } | 
|---|
| [1743] | 217 |  | 
|---|
| [1778] | 218 | // template that evaluates whether IStringStream is possible for conversions from std::string | 
|---|
 | 219 | template <class ToType> | 
|---|
 | 220 | struct ConverterStringStream<ToType, std::string> | 
|---|
| [1777] | 221 | { | 
|---|
| [1778] | 222 |     static bool convert(ToType* output, const std::string& input) | 
|---|
| [1743] | 223 |     { | 
|---|
| [1778] | 224 |         using namespace fallbackTemplates; | 
|---|
 | 225 |         std::istringstream iss(input); | 
|---|
 | 226 |         if (iss >> (*output)) | 
|---|
| [1743] | 227 |         { | 
|---|
| [1778] | 228 |             return true; | 
|---|
| [1743] | 229 |         } | 
|---|
| [1778] | 230 |         else | 
|---|
 | 231 |             return false; | 
|---|
 | 232 |     } | 
|---|
 | 233 | }; | 
|---|
| [1743] | 234 |  | 
|---|
 | 235 |  | 
|---|
| [1777] | 236 | /////////////////// | 
|---|
 | 237 | // Implicit Cast // | 
|---|
 | 238 | /////////////////// | 
|---|
| [1753] | 239 |  | 
|---|
| [1779] | 240 | // static cast no possible, try stringstream conversion next | 
|---|
 | 241 | template <class ToType, class FromType> | 
|---|
 | 242 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>) | 
|---|
 | 243 | { | 
|---|
 | 244 |     return ConverterStringStream<ToType, FromType>::convert(output, input); | 
|---|
 | 245 | } | 
|---|
 | 246 |  | 
|---|
| [1777] | 247 | // We can cast implicitely | 
|---|
 | 248 | template <class ToType, class FromType> | 
|---|
 | 249 | inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<true>) | 
|---|
 | 250 | { | 
|---|
 | 251 |     (*output) = static_cast<ToType>(input); | 
|---|
 | 252 |     return true; | 
|---|
 | 253 | } | 
|---|
| [1778] | 254 |  | 
|---|
| [1753] | 255 |  | 
|---|
| [1778] | 256 | /////////////////////// | 
|---|
 | 257 | // Explicit Fallback // | 
|---|
 | 258 | /////////////////////// | 
|---|
| [1753] | 259 |  | 
|---|
| [1779] | 260 | // Default template if no specialisation is available | 
|---|
| [1778] | 261 | template <class ToType, class FromType> | 
|---|
| [1779] | 262 | struct ConverterExplicit | 
|---|
| [1777] | 263 | { | 
|---|
| [1754] | 264 |     static bool convert(ToType* output, const FromType& input) | 
|---|
| [1753] | 265 |     { | 
|---|
| [1779] | 266 |         // try implict conversion by probing first because we use '...' instead of a template | 
|---|
 | 267 |         const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists; | 
|---|
 | 268 |         return convertImplicitely(output, input, ::Int2Type<probe>()); | 
|---|
| [1754] | 269 |     } | 
|---|
 | 270 | }; | 
|---|
| [1505] | 271 |  | 
|---|
 | 272 |  | 
|---|
| [1753] | 273 | ////////////////////// | 
|---|
 | 274 | // Public Functions // | 
|---|
 | 275 | ////////////////////// | 
|---|
| [1505] | 276 |  | 
|---|
| [1743] | 277 | /** | 
|---|
 | 278 | @brief | 
|---|
| [1778] | 279 |     Converts any value to any other as long as there exists a conversion. | 
|---|
| [1743] | 280 |     Otherwise, the conversion will generate a runtime warning. | 
|---|
| [1753] | 281 |     For information about the different conversion methods (user defined too), see the section | 
|---|
 | 282 |     'Actual conversion sequence' in this file above. | 
|---|
| [1778] | 283 | @note | 
|---|
 | 284 |     This function is only a fallback if there is no appropriate 'convertValue' function. | 
|---|
| [1743] | 285 | */ | 
|---|
 | 286 | template <class ToType, class FromType> | 
|---|
 | 287 | inline bool convertValue(ToType* output, const FromType& input) | 
|---|
 | 288 | { | 
|---|
| [1779] | 289 |     return ConverterExplicit<ToType, FromType>::convert(output, input); | 
|---|
| [1743] | 290 | } | 
|---|
 | 291 |  | 
|---|
| [1753] | 292 | // For compatibility reasons. The same, but with capital ConvertValue | 
|---|
| [1505] | 293 | template<class FromType, class ToType> | 
|---|
| [1753] | 294 | inline bool ConvertValue(ToType* output, const FromType& input) | 
|---|
| [1505] | 295 | { | 
|---|
| [1743] | 296 |     return convertValue(output, input); | 
|---|
| [1505] | 297 | } | 
|---|
| [1753] | 298 |  | 
|---|
 | 299 | // Calls convertValue and returns true if the conversion was successful. | 
|---|
 | 300 | // Otherwise the fallback is used. | 
|---|
| [1505] | 301 | template<class FromType, class ToType> | 
|---|
| [1753] | 302 | inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback) | 
|---|
| [1505] | 303 | { | 
|---|
| [1743] | 304 |     if (convertValue(output, input)) | 
|---|
| [1505] | 305 |         return true; | 
|---|
 | 306 |  | 
|---|
 | 307 |     (*output) = fallback; | 
|---|
 | 308 |     return false; | 
|---|
 | 309 | } | 
|---|
 | 310 |  | 
|---|
| [1753] | 311 | // Directly returns the converted value, even if the conversion was not successful. | 
|---|
| [1505] | 312 | template<class FromType, class ToType> | 
|---|
| [1753] | 313 | inline ToType getConvertedValue(const FromType& input) | 
|---|
| [1505] | 314 | { | 
|---|
| [1753] | 315 |     ToType output; | 
|---|
| [1743] | 316 |     ConvertValue(&output, input); | 
|---|
| [1505] | 317 |     return output; | 
|---|
 | 318 | } | 
|---|
| [1753] | 319 |  | 
|---|
 | 320 | // Directly returns the converted value, but uses the fallback on failure. | 
|---|
| [1505] | 321 | template<class FromType, class ToType> | 
|---|
| [1753] | 322 | inline ToType getConvertedValue(const FromType& input, const ToType& fallback) | 
|---|
| [1505] | 323 | { | 
|---|
| [1754] | 324 |     ToType output; | 
|---|
| [1743] | 325 |     ConvertValue(&output, input, fallback); | 
|---|
| [1505] | 326 |     return output; | 
|---|
 | 327 | } | 
|---|
 | 328 |  | 
|---|
| [1753] | 329 | // Like getConvertedValue, but the template argument order is in reverse. | 
|---|
 | 330 | // That means you can call it exactly like static_cast<ToType>(fromTypeValue). | 
|---|
 | 331 | template<class ToType, class FromType> | 
|---|
 | 332 | inline ToType conversion_cast(const FromType& input) | 
|---|
 | 333 | { | 
|---|
 | 334 |     ToType output; | 
|---|
 | 335 |     ConvertValue(&output, input); | 
|---|
 | 336 |     return output; | 
|---|
 | 337 | } | 
|---|
 | 338 |  | 
|---|
 | 339 | // Like conversion_cast above, but uses a fallback on failure. | 
|---|
 | 340 | template<class ToType, class FromType> | 
|---|
 | 341 | inline ToType conversion_cast(const FromType& input, const ToType& fallback) | 
|---|
 | 342 | { | 
|---|
| [1754] | 343 |     ToType output; | 
|---|
| [1753] | 344 |     ConvertValue(&output, input, fallback); | 
|---|
 | 345 |     return output; | 
|---|
 | 346 | } | 
|---|
 | 347 |  | 
|---|
 | 348 |  | 
|---|
| [1777] | 349 | //////////////////////////////// | 
|---|
 | 350 | // Special string conversions // | 
|---|
 | 351 | //////////////////////////////// | 
|---|
| [1753] | 352 |  | 
|---|
| [1766] | 353 | // delegate conversion from const char* to std::string | 
|---|
| [1753] | 354 | template <class ToType> | 
|---|
| [1779] | 355 | struct ConverterExplicit<ToType, const char*> | 
|---|
| [1753] | 356 | { | 
|---|
| [1779] | 357 |     static bool convert(ToType* output, const char* input) | 
|---|
 | 358 |     { | 
|---|
 | 359 |         return convertValue<ToType, std::string>(output, input); | 
|---|
 | 360 |     } | 
|---|
 | 361 | }; | 
|---|
| [1753] | 362 |  | 
|---|
 | 363 | // These conversions would exhibit ambiguous << or >> operators when using stringstream | 
|---|
| [1779] | 364 | template <> | 
|---|
 | 365 | struct ConverterExplicit<std::string, char> | 
|---|
| [1753] | 366 | { | 
|---|
| [1779] | 367 |     static bool convert(std::string* output, const char input) | 
|---|
 | 368 |     { | 
|---|
 | 369 |         *output = std::string(1, input); | 
|---|
 | 370 |         return true; | 
|---|
 | 371 |     } | 
|---|
 | 372 | }; | 
|---|
 | 373 | template <> | 
|---|
 | 374 | struct ConverterExplicit<std::string, unsigned char> | 
|---|
| [1753] | 375 | { | 
|---|
| [1779] | 376 |     static bool convert(std::string* output, const unsigned char input) | 
|---|
 | 377 |     { | 
|---|
 | 378 |         *output = std::string(1, input); | 
|---|
 | 379 |         return true; | 
|---|
 | 380 |     } | 
|---|
 | 381 | }; | 
|---|
 | 382 | template <> | 
|---|
 | 383 | struct ConverterExplicit<char, std::string> | 
|---|
| [1753] | 384 | { | 
|---|
| [1779] | 385 |     static bool convert(char* output, const std::string input) | 
|---|
 | 386 |     { | 
|---|
 | 387 |         if (input != "") | 
|---|
 | 388 |             *output = input[0]; | 
|---|
 | 389 |         else | 
|---|
 | 390 |             *output = '\0'; | 
|---|
 | 391 |         return true; | 
|---|
 | 392 |     } | 
|---|
 | 393 | }; | 
|---|
 | 394 | template <> | 
|---|
 | 395 | struct ConverterExplicit<unsigned char, std::string> | 
|---|
| [1753] | 396 | { | 
|---|
| [1779] | 397 |     static bool convert(unsigned char* output, const std::string input) | 
|---|
 | 398 |     { | 
|---|
 | 399 |         if (input != "") | 
|---|
 | 400 |             *output = input[0]; | 
|---|
 | 401 |         else | 
|---|
 | 402 |             *output = '\0'; | 
|---|
 | 403 |         return true; | 
|---|
 | 404 |     } | 
|---|
 | 405 | }; | 
|---|
| [1753] | 406 |  | 
|---|
| [1052] | 407 | #endif /* _Convert_H__ */ | 
|---|