[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: |
---|
[2087] | 23 | * Reto Grieder |
---|
[1791] | 24 | * Fabian 'x3n' Landau |
---|
[1052] | 25 | * Benjamin Grauer |
---|
[1505] | 26 | * Co-authors: |
---|
[1052] | 27 | * ... |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | /*! |
---|
[2087] | 31 | @file |
---|
[1052] | 32 | @brief Definition and Implementation of the Convert class. |
---|
| 33 | */ |
---|
| 34 | |
---|
[2087] | 35 | #ifndef _Converter_H__ |
---|
| 36 | #define _Converter_H__ |
---|
[1052] | 37 | |
---|
[1062] | 38 | #include "UtilPrereqs.h" |
---|
| 39 | |
---|
[1052] | 40 | #include <string> |
---|
| 41 | #include <sstream> |
---|
[1837] | 42 | #include <typeinfo> |
---|
[7266] | 43 | #include <loki/TypeManip.h> |
---|
[1052] | 44 | |
---|
[1747] | 45 | #include "Debug.h" |
---|
[3232] | 46 | #include "TemplateUtils.h" |
---|
[1064] | 47 | |
---|
[2087] | 48 | //////////////////////////////////// |
---|
| 49 | //// ACTUAL CONVERSION SEQUENCE //// |
---|
| 50 | //////////////////////////////////// |
---|
| 51 | /* |
---|
| 52 | There is a distinct priority when choosing the right conversion function: |
---|
| 53 | Overwrite: |
---|
| 54 | 1. (Partial) template specialisation of ConverterExplicit::convert() |
---|
| 55 | Fallbacks: |
---|
| 56 | 2. Any possible implicit conversion. This includes 'FooBar' --> 'int' if FooBar defines operator float(). |
---|
| 57 | 3. Global or member operators for stringstream when converting from or to std::string (or FROM const char*) |
---|
| 58 | 4. (Partial) template specialisation of ConverterFallback::convert() |
---|
| 59 | 5. Function that simply displays "Could not convert value" with type information obtained from typeid(). |
---|
[1505] | 60 | |
---|
[2087] | 61 | Notes: |
---|
| 62 | There has to be an exact type match when using template specialisations. |
---|
| 63 | Template specialisations can be defined after including this file. Any implicit cast function or iostream |
---|
| 64 | operator has to be declared BEFORE this file gets parsed. |
---|
[1505] | 65 | |
---|
[2087] | 66 | Defining your own functions: |
---|
[7163] | 67 | There are obviously 4 ways to specify a user defined conversion. What should you use? |
---|
[1505] | 68 | |
---|
[2087] | 69 | Usually, ConverterFallback fits quite well. You won't have to deal with the conversion from |
---|
| 70 | 'MyClass' to 'MyClass' by using another explicit template specialisation to avoid ambiguities. |
---|
[1505] | 71 | |
---|
[2087] | 72 | However if you want to overwrite an implicit conversion or an iostream operator, you really need to |
---|
[7163] | 73 | make use of ConverterExplicit. We have to do this for the Ogre classes for instance because they |
---|
| 74 | define stream operators we don't particulary like. |
---|
[2087] | 75 | */ |
---|
| 76 | |
---|
[2171] | 77 | namespace orxonox |
---|
[1505] | 78 | { |
---|
[2171] | 79 | /////////////////// |
---|
| 80 | // No Conversion // |
---|
| 81 | /////////////////// |
---|
[1505] | 82 | |
---|
[2171] | 83 | // Default template. No conversion available at all. |
---|
| 84 | template <class FromType, class ToType> |
---|
| 85 | struct ConverterFallback |
---|
[1505] | 86 | { |
---|
[3196] | 87 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[2171] | 88 | { |
---|
| 89 | COUT(2) << "Could not convert value of type " << typeid(FromType).name() |
---|
| 90 | << " to type " << typeid(ToType).name() << std::endl; |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | }; |
---|
[2087] | 94 | |
---|
[2171] | 95 | // If all else fails, try a dynamic_cast for pointer types. |
---|
| 96 | template <class FromType, class ToType> |
---|
| 97 | struct ConverterFallback<FromType*, ToType*> |
---|
[1505] | 98 | { |
---|
[3196] | 99 | FORCEINLINE static bool convert(ToType** output, FromType* const input) |
---|
[2087] | 100 | { |
---|
[2171] | 101 | ToType* temp = dynamic_cast<ToType*>(input); |
---|
| 102 | if (temp) |
---|
| 103 | { |
---|
| 104 | *output = temp; |
---|
| 105 | return true; |
---|
| 106 | } |
---|
| 107 | else |
---|
| 108 | return false; |
---|
[2087] | 109 | } |
---|
[2171] | 110 | }; |
---|
[7284] | 111 | |
---|
| 112 | //////////// |
---|
| 113 | // upcast // |
---|
| 114 | //////////// |
---|
| 115 | namespace detail |
---|
| 116 | { |
---|
| 117 | // perform a static cast if ToType is a base of FromType |
---|
| 118 | template<class ToType, class FromType> |
---|
| 119 | FORCEINLINE ToType upcast(FromType input, Loki::Int2Type<true>) |
---|
| 120 | { |
---|
| 121 | return static_cast<ToType>(input); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // return zero if ToType is not a base of FromType |
---|
| 125 | template<class ToType, class FromType> |
---|
| 126 | FORCEINLINE ToType upcast(FromType input, Loki::Int2Type<false>) |
---|
| 127 | { |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // performs an upcast if ToType is a base of FromType, returns zero otherwise |
---|
| 133 | template <class ToType, class FromType> |
---|
| 134 | FORCEINLINE ToType upcast(FromType input) |
---|
| 135 | { |
---|
| 136 | enum { probe = ImplicitConversion<FromType, ToType>::exists }; |
---|
| 137 | return detail::upcast<ToType, FromType>(input, Loki::Int2Type<probe>()); |
---|
| 138 | } |
---|
[2171] | 139 | } |
---|
[1505] | 140 | |
---|
| 141 | |
---|
[2087] | 142 | /////////////////////// |
---|
| 143 | // ConverterFallback // |
---|
| 144 | /////////////////////// |
---|
| 145 | |
---|
| 146 | // Default template for stringstream |
---|
| 147 | template <class FromType, class ToType> |
---|
| 148 | struct ConverterStringStream |
---|
[1505] | 149 | { |
---|
[3196] | 150 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[1505] | 151 | { |
---|
[2171] | 152 | return orxonox::ConverterFallback<FromType, ToType>::convert(output, input); |
---|
[1505] | 153 | } |
---|
| 154 | }; |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | ///////////// |
---|
[2087] | 158 | // OStream // |
---|
[1505] | 159 | ///////////// |
---|
| 160 | |
---|
[2087] | 161 | namespace fallbackTemplates |
---|
[1505] | 162 | { |
---|
[2087] | 163 | template <class FromType> |
---|
[3196] | 164 | FORCEINLINE bool operator <<(std::ostream& outstream, const FromType& input) |
---|
[2087] | 165 | { |
---|
| 166 | std::string temp; |
---|
[2171] | 167 | if (orxonox::ConverterFallback<FromType, std::string>::convert(&temp, input)) |
---|
[2087] | 168 | { |
---|
| 169 | std::operator <<(outstream, temp); |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | return false; |
---|
| 174 | } |
---|
| 175 | } |
---|
[1505] | 176 | |
---|
[2087] | 177 | // template that evaluates whether we can convert to std::string via ostringstream |
---|
[1505] | 178 | template <class FromType> |
---|
[2087] | 179 | struct ConverterStringStream<FromType, std::string> |
---|
[1505] | 180 | { |
---|
[3196] | 181 | FORCEINLINE static bool convert(std::string* output, const FromType& input) |
---|
[1505] | 182 | { |
---|
[2087] | 183 | using namespace fallbackTemplates; |
---|
| 184 | // this operator call only chooses fallbackTemplates::operator<< if there's no other fitting function |
---|
[1505] | 185 | std::ostringstream oss; |
---|
| 186 | if (oss << input) |
---|
| 187 | { |
---|
| 188 | (*output) = oss.str(); |
---|
| 189 | return true; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | return false; |
---|
| 193 | } |
---|
| 194 | }; |
---|
| 195 | |
---|
[2087] | 196 | |
---|
| 197 | ///////////// |
---|
| 198 | // IStream // |
---|
| 199 | ///////////// |
---|
| 200 | |
---|
| 201 | namespace fallbackTemplates |
---|
[1625] | 202 | { |
---|
[2087] | 203 | template <class ToType> |
---|
[3196] | 204 | FORCEINLINE bool operator >>(std::istream& instream, ToType& output) |
---|
[2087] | 205 | { |
---|
[2171] | 206 | return orxonox::ConverterFallback<std::string, ToType> |
---|
[2087] | 207 | ::convert(&output, static_cast<std::istringstream&>(instream).str()); |
---|
| 208 | } |
---|
[1625] | 209 | } |
---|
| 210 | |
---|
[2087] | 211 | // template that evaluates whether we can convert from std::string via ostringstream |
---|
[1505] | 212 | template <class ToType> |
---|
[2087] | 213 | struct ConverterStringStream<std::string, ToType> |
---|
[1505] | 214 | { |
---|
[3196] | 215 | FORCEINLINE static bool convert(ToType* output, const std::string& input) |
---|
[1505] | 216 | { |
---|
[2087] | 217 | using namespace fallbackTemplates; |
---|
[1505] | 218 | std::istringstream iss(input); |
---|
[2087] | 219 | // this operator call only chooses fallbackTemplates::operator>> if there's no other fitting function |
---|
[1505] | 220 | if (iss >> (*output)) |
---|
[2087] | 221 | { |
---|
[1505] | 222 | return true; |
---|
[2087] | 223 | } |
---|
[1505] | 224 | else |
---|
| 225 | return false; |
---|
| 226 | } |
---|
| 227 | }; |
---|
| 228 | |
---|
[2171] | 229 | namespace orxonox |
---|
[1625] | 230 | { |
---|
[1505] | 231 | |
---|
[2171] | 232 | /////////////////// |
---|
| 233 | // Implicit Cast // |
---|
| 234 | /////////////////// |
---|
[1625] | 235 | |
---|
[2171] | 236 | // implicit cast not possible, try stringstream conversion next |
---|
| 237 | template <class FromType, class ToType> |
---|
[7266] | 238 | FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, Loki::Int2Type<false>) |
---|
[1505] | 239 | { |
---|
[2171] | 240 | return ConverterStringStream<FromType, ToType>::convert(output, input); |
---|
[1505] | 241 | } |
---|
| 242 | |
---|
[2171] | 243 | // We can cast implicitely |
---|
| 244 | template <class FromType, class ToType> |
---|
[7266] | 245 | FORCEINLINE bool convertImplicitely(ToType* output, const FromType& input, Loki::Int2Type<true>) |
---|
[2171] | 246 | { |
---|
| 247 | (*output) = static_cast<ToType>(input); |
---|
[2087] | 248 | return true; |
---|
[1505] | 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
[2171] | 252 | //////////////////////////////// |
---|
| 253 | // ConverterExplicit Fallback // |
---|
| 254 | //////////////////////////////// |
---|
[1505] | 255 | |
---|
[2171] | 256 | // Default template if no specialisation is available |
---|
| 257 | template <class FromType, class ToType> |
---|
| 258 | struct ConverterExplicit |
---|
| 259 | { |
---|
[3234] | 260 | enum { probe = ImplicitConversion<FromType, ToType>::exists }; |
---|
[3196] | 261 | FORCEINLINE static bool convert(ToType* output, const FromType& input) |
---|
[2171] | 262 | { |
---|
| 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 |
---|
[7266] | 265 | return convertImplicitely(output, input, Loki::Int2Type<probe>()); |
---|
[2171] | 266 | } |
---|
| 267 | }; |
---|
[2087] | 268 | |
---|
| 269 | |
---|
[2171] | 270 | ////////////////////// |
---|
| 271 | // Public Functions // |
---|
| 272 | ////////////////////// |
---|
[2087] | 273 | |
---|
[2171] | 274 | /** |
---|
| 275 | @brief |
---|
| 276 | Converts any value to any other as long as there exists a conversion. |
---|
| 277 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 278 | For information about the different conversion methods (user defined too), see the section |
---|
| 279 | 'Actual conversion sequence' in this file above. |
---|
[7297] | 280 | @param output |
---|
| 281 | A pointer to the variable where the converted value will be stored |
---|
| 282 | @param input |
---|
| 283 | The original value |
---|
[2171] | 284 | */ |
---|
| 285 | template <class FromType, class ToType> |
---|
[3196] | 286 | FORCEINLINE bool convertValue(ToType* output, const FromType& input) |
---|
[2171] | 287 | { |
---|
| 288 | return ConverterExplicit<FromType, ToType>::convert(output, input); |
---|
| 289 | } |
---|
[2087] | 290 | |
---|
[2171] | 291 | // Calls convertValue and returns true if the conversion was successful. |
---|
| 292 | // Otherwise the fallback is used. |
---|
| 293 | /** |
---|
| 294 | @brief |
---|
| 295 | Converts any value to any other as long as there exists a conversion. |
---|
| 296 | Otherwise, the conversion will generate a runtime warning and return false. |
---|
| 297 | For information about the different conversion methods (user defined too), see the section |
---|
| 298 | 'Actual conversion sequence' in this file above. |
---|
| 299 | If the conversion doesn't succeed, 'fallback' is written to '*output'. |
---|
[7297] | 300 | @param output |
---|
| 301 | A pointer to the variable where the converted value will be stored |
---|
| 302 | @param input |
---|
| 303 | The original value |
---|
[2171] | 304 | @param fallback |
---|
| 305 | A default value that gets written to '*output' if there is no conversion. |
---|
| 306 | */ |
---|
| 307 | template<class FromType, class ToType> |
---|
[3196] | 308 | FORCEINLINE bool convertValue(ToType* output, const FromType& input, const ToType& fallback) |
---|
[1625] | 309 | { |
---|
[2171] | 310 | if (convertValue(output, input)) |
---|
| 311 | return true; |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | (*output) = fallback; |
---|
| 315 | return false; |
---|
| 316 | } |
---|
[1625] | 317 | } |
---|
| 318 | |
---|
[2171] | 319 | // Directly returns the converted value, even if the conversion was not successful. |
---|
| 320 | template<class FromType, class ToType> |
---|
[3196] | 321 | FORCEINLINE ToType getConvertedValue(const FromType& input) |
---|
[1505] | 322 | { |
---|
[2171] | 323 | ToType output; |
---|
| 324 | convertValue(&output, input); |
---|
| 325 | return output; |
---|
[1505] | 326 | } |
---|
[2171] | 327 | |
---|
| 328 | // Directly returns the converted value, but uses the fallback on failure. |
---|
| 329 | template<class FromType, class ToType> |
---|
[3196] | 330 | FORCEINLINE ToType getConvertedValue(const FromType& input, const ToType& fallback) |
---|
[1505] | 331 | { |
---|
[2171] | 332 | ToType output; |
---|
| 333 | convertValue(&output, input, fallback); |
---|
| 334 | return output; |
---|
[1505] | 335 | } |
---|
[2171] | 336 | |
---|
| 337 | // Like getConvertedValue, but the template argument order is in reverse. |
---|
| 338 | // That means you can call it exactly like static_cast<ToType>(fromTypeValue). |
---|
| 339 | template<class ToType, class FromType> |
---|
[3196] | 340 | FORCEINLINE ToType multi_cast(const FromType& input) |
---|
[1505] | 341 | { |
---|
[2171] | 342 | ToType output; |
---|
| 343 | convertValue(&output, input); |
---|
| 344 | return output; |
---|
[1505] | 345 | } |
---|
| 346 | |
---|
[2171] | 347 | //////////////////////////////// |
---|
| 348 | // Special string conversions // |
---|
| 349 | //////////////////////////////// |
---|
| 350 | |
---|
| 351 | // Delegate conversion from const char* to std::string |
---|
| 352 | template <class ToType> |
---|
| 353 | struct ConverterExplicit<const char*, ToType> |
---|
[1625] | 354 | { |
---|
[3196] | 355 | FORCEINLINE static bool convert(ToType* output, const char* input) |
---|
[1625] | 356 | { |
---|
[2171] | 357 | return convertValue<std::string, ToType>(output, input); |
---|
[1625] | 358 | } |
---|
[2171] | 359 | }; |
---|
| 360 | |
---|
| 361 | // These conversions would exhibit ambiguous << or >> operators when using stringstream |
---|
| 362 | template <> |
---|
| 363 | struct ConverterExplicit<char, std::string> |
---|
| 364 | { |
---|
[3196] | 365 | FORCEINLINE static bool convert(std::string* output, const char input) |
---|
[1625] | 366 | { |
---|
[6417] | 367 | *output = input; |
---|
[2171] | 368 | return true; |
---|
[1625] | 369 | } |
---|
[2171] | 370 | }; |
---|
| 371 | template <> |
---|
| 372 | struct ConverterExplicit<unsigned char, std::string> |
---|
| 373 | { |
---|
[3196] | 374 | FORCEINLINE static bool convert(std::string* output, const unsigned char input) |
---|
[2171] | 375 | { |
---|
[6417] | 376 | *output = input; |
---|
[2171] | 377 | return true; |
---|
| 378 | } |
---|
| 379 | }; |
---|
| 380 | template <> |
---|
| 381 | struct ConverterExplicit<std::string, char> |
---|
| 382 | { |
---|
[6417] | 383 | FORCEINLINE static bool convert(char* output, const std::string& input) |
---|
[2171] | 384 | { |
---|
[6417] | 385 | if (!input.empty()) |
---|
[2171] | 386 | *output = input[0]; |
---|
| 387 | else |
---|
| 388 | *output = '\0'; |
---|
| 389 | return true; |
---|
| 390 | } |
---|
| 391 | }; |
---|
| 392 | template <> |
---|
| 393 | struct ConverterExplicit<std::string, unsigned char> |
---|
| 394 | { |
---|
[6417] | 395 | FORCEINLINE static bool convert(unsigned char* output, const std::string& input) |
---|
[2171] | 396 | { |
---|
[6417] | 397 | if (!input.empty()) |
---|
[2171] | 398 | *output = input[0]; |
---|
| 399 | else |
---|
| 400 | *output = '\0'; |
---|
| 401 | return true; |
---|
| 402 | } |
---|
| 403 | }; |
---|
[1625] | 404 | |
---|
[2171] | 405 | |
---|
| 406 | // bool to std::string |
---|
| 407 | template <> |
---|
| 408 | struct ConverterExplicit<bool, std::string> |
---|
| 409 | { |
---|
[3196] | 410 | FORCEINLINE static bool convert(std::string* output, const bool& input) |
---|
[2171] | 411 | { |
---|
| 412 | if (input) |
---|
| 413 | *output = "true"; |
---|
| 414 | else |
---|
| 415 | *output = "false"; |
---|
[2662] | 416 | return true; |
---|
[2171] | 417 | } |
---|
| 418 | }; |
---|
[1625] | 419 | |
---|
[2171] | 420 | // std::string to bool |
---|
| 421 | template <> |
---|
[7163] | 422 | struct _UtilExport ConverterExplicit<std::string, bool> |
---|
[2171] | 423 | { |
---|
[7163] | 424 | static bool convert(bool* output, const std::string& input); |
---|
[2171] | 425 | }; |
---|
| 426 | } |
---|
| 427 | |
---|
[1052] | 428 | #endif /* _Convert_H__ */ |
---|