Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/util/ImplicitConversion.h @ 8974

Last change on this file since 8974 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
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
40namespace 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 existance
64        is described in 'Modern C++ design' by Alexandrescu (2001).
65    */
66    template <class FromType, class ToType>
67    class ImplicitConversion
68    {
69    private:
70        ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion();
71        // Gets chosen only if there is an implicit conversion from FromType to ToType.
72        static char test(ToType);
73        // Accepts any argument. Why do we not use a template? The reason is that with templates,
74        // the function above is only taken iff it is an exact type match. But since we want to
75        // check for implicit conversion, we have to use the ellipsis.
76        static long long test(...);
77        static FromType object; // helper object to handle private c'tor and d'tor
78    public:
79        // test(object) only has 'long long' return type iff the compiler doesn't choose test(...)
80        enum { exists = (sizeof(test(object)) == sizeof(char)) };
81    };
82
83#ifdef ORXONOX_COMPILER_MSVC
84#  pragma warning(pop)
85#endif
86
87}
88
89#endif /* _TemplateUtils_H__ */
Note: See TracBrowser for help on using the repository browser.