Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox/src/libraries/util/TemplateUtils.h @ 5910

Last change on this file since 5910 was 5738, checked in by landauf, 15 years ago

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