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 | @ingroup Util |
---|
32 | @brief Declaration of various helper templates. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _TemplateUtils_H__ |
---|
36 | #define _TemplateUtils_H__ |
---|
37 | |
---|
38 | #include "UtilPrereqs.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | /////////////////////////////////////////////////// |
---|
43 | // Static detection of implicit type conversions // |
---|
44 | /////////////////////////////////////////////////// |
---|
45 | |
---|
46 | // disable warnings about possible loss of data |
---|
47 | #ifdef ORXONOX_COMPILER_MSVC |
---|
48 | # pragma warning(push) |
---|
49 | # pragma warning(disable:4244) |
---|
50 | #endif |
---|
51 | // GCC generates warnings when implicitely casting from float to int for instance. |
---|
52 | #ifdef ORXONOX_COMPILER_GCC |
---|
53 | # pragma GCC system_header |
---|
54 | #endif |
---|
55 | |
---|
56 | /** |
---|
57 | @brief |
---|
58 | Detects whether you can implicitely cast FromType to ToType. |
---|
59 | |
---|
60 | Usage: ImplicitConversion<FromType, ToType>::exists |
---|
61 | This gives you a compile time constant boolean in the form of an enum value. |
---|
62 | @note |
---|
63 | The idea to use the sizeof() operator on return values to determine function existence |
---|
64 | is described in 'Modern C++ design' by Alexandrescu (2001). |
---|
65 | */ |
---|
66 | template <class FromType, class ToType> |
---|
67 | class ImplicitConversion |
---|
68 | { |
---|
69 | private: |
---|
70 | // static class, no instances allowed: |
---|
71 | ImplicitConversion() = delete; |
---|
72 | ImplicitConversion(const ImplicitConversion&) = delete; |
---|
73 | ImplicitConversion& operator=(const ImplicitConversion&) = delete; |
---|
74 | ~ImplicitConversion() = delete; |
---|
75 | |
---|
76 | // Gets chosen only if there is an implicit conversion from FromType to ToType. |
---|
77 | static char test(ToType); |
---|
78 | // Accepts any argument. Why do we not use a template? The reason is that with templates, |
---|
79 | // the function above is only taken iff it is an exact type match. But since we want to |
---|
80 | // check for implicit conversion, we have to use the ellipsis. |
---|
81 | static long long test(...); |
---|
82 | static FromType object; // helper object to handle private c'tor and d'tor |
---|
83 | public: |
---|
84 | // test(object) only has 'long long' return type iff the compiler doesn't choose test(...) |
---|
85 | enum { exists = (sizeof(test(object)) == sizeof(char)) }; |
---|
86 | }; |
---|
87 | |
---|
88 | #ifdef ORXONOX_COMPILER_MSVC |
---|
89 | # pragma warning(pop) |
---|
90 | #endif |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | #endif /* _TemplateUtils_H__ */ |
---|