[3232] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Declaration of various helper templates. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #ifndef _TemplateUtils_H__ |
---|
| 35 | #define _TemplateUtils_H__ |
---|
| 36 | |
---|
| 37 | #include "UtilPrereqs.h" |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[3233] | 41 | /** |
---|
| 42 | @brief |
---|
| 43 | Use TypeStripper to get rid of the const and the reference of type T |
---|
| 44 | @note |
---|
| 45 | Main use of this is when trying to instantiate type T as T(). |
---|
| 46 | */ |
---|
| 47 | template <class T> struct TypeStripper |
---|
| 48 | { typedef T RawType; }; |
---|
| 49 | template <class T> struct TypeStripper<const T> |
---|
| 50 | { typedef T RawType; }; |
---|
| 51 | template <class T> struct TypeStripper<const T&> |
---|
| 52 | { typedef T RawType; }; |
---|
| 53 | |
---|
| 54 | |
---|
[3232] | 55 | /////////////////////////////////////////////////// |
---|
| 56 | // Static detection of implicit type conversions // |
---|
| 57 | /////////////////////////////////////////////////// |
---|
| 58 | |
---|
| 59 | // disable warnings about possible loss of data |
---|
| 60 | #ifdef ORXONOX_COMPILER_MSVC |
---|
| 61 | # pragma warning(push) |
---|
| 62 | # pragma warning(disable:4244) |
---|
| 63 | #endif |
---|
| 64 | // GCC generates warnings when implicitely casting from float to int for instance. |
---|
| 65 | #ifdef ORXONOX_COMPILER_GCC |
---|
| 66 | # pragma GCC system_header |
---|
| 67 | #endif |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | @brief |
---|
| 71 | Detects whether you can implicitely cast FromType to ToType. |
---|
| 72 | |
---|
| 73 | Usage: ImplicitConversion<FromType, ToType>::exists |
---|
| 74 | This gives you a compile time constant boolean in the form of an enum value. |
---|
| 75 | @note |
---|
| 76 | The idea to use the sizeof() operator on return values to determine function existance |
---|
| 77 | is described in 'Modern C++ design' by Alexandrescu (2001). |
---|
| 78 | */ |
---|
| 79 | template <class FromType, class ToType> |
---|
| 80 | class ImplicitConversion |
---|
| 81 | { |
---|
| 82 | private: |
---|
| 83 | ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion(); |
---|
| 84 | // Gets chosen only if there is an implicit conversion from FromType to ToType. |
---|
| 85 | static char test(ToType); |
---|
| 86 | // Accepts any argument. Why do we not use a template? The reason is that with templates, |
---|
| 87 | // the function above is only taken iff it is an exact type match. But since we want to |
---|
| 88 | // check for implicit conversion, we have to use the ellipsis. |
---|
| 89 | static long long test(...); |
---|
| 90 | static FromType object; // helper object to handle private c'tor and d'tor |
---|
| 91 | public: |
---|
| 92 | // test(object) only has 'long long' return type iff the compiler doesn't choose test(...) |
---|
| 93 | enum { exists = (sizeof(test(object)) == sizeof(char)) }; |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | #ifdef ORXONOX_COMPILER_MSVC |
---|
| 97 | # pragma warning(pop) |
---|
| 98 | #endif |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | #endif /* _TemplateUtils_H__ */ |
---|