File: implicitconversion.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (102 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/meta/implicitconversion.h
// Purpose:     Determine resulting type from implicit conversion
// Author:      Vaclav Slavik
// Created:     2010-10-22
// Copyright:   (c) 2010 Vaclav Slavik
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_META_IMPLICITCONVERSION_H_
#define _WX_META_IMPLICITCONVERSION_H_

#include "wx/defs.h"
#include "wx/meta/if.h"

// C++ hierarchy of data types is:
//
//   Long double (highest)
//   Double
//   Float
//   Unsigned long int
//   Long int
//   Unsigned int
//   Int (lowest)
//
// Types lower in the hierarchy are converted into ones higher up if both are
// involved e.g. in arithmetic expressions.

namespace wxPrivate
{

template<typename T>
struct TypeHierarchy
{
    // consider unknown types (e.g. objects, pointers) to be of highest
    // level, always convert to them if they occur
    static const int level = 9999;
};

#define WX_TYPE_HIERARCHY_LEVEL(level_num, type)        \
    template<> struct TypeHierarchy<type>               \
    {                                                   \
        static const int level = level_num;             \
    }

WX_TYPE_HIERARCHY_LEVEL( 1, char);
WX_TYPE_HIERARCHY_LEVEL( 2, unsigned char);
WX_TYPE_HIERARCHY_LEVEL( 3, short);
WX_TYPE_HIERARCHY_LEVEL( 4, unsigned short);
WX_TYPE_HIERARCHY_LEVEL( 5, int);
WX_TYPE_HIERARCHY_LEVEL( 6, unsigned int);
WX_TYPE_HIERARCHY_LEVEL( 7, long);
WX_TYPE_HIERARCHY_LEVEL( 8, unsigned long);
#ifdef wxLongLong_t
WX_TYPE_HIERARCHY_LEVEL( 9, wxLongLong_t);
WX_TYPE_HIERARCHY_LEVEL(10, wxULongLong_t);
#endif
WX_TYPE_HIERARCHY_LEVEL(11, float);
WX_TYPE_HIERARCHY_LEVEL(12, double);
WX_TYPE_HIERARCHY_LEVEL(13, long double);

#if wxWCHAR_T_IS_REAL_TYPE
    #if SIZEOF_WCHAR_T == SIZEOF_SHORT
      template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<short> {};
    #elif SIZEOF_WCHAR_T == SIZEOF_INT
      template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<int> {};
    #elif SIZEOF_WCHAR_T == SIZEOF_LONG
      template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<long> {};
    #else
      #error "weird wchar_t size, please update this code"
    #endif
#endif

#undef WX_TYPE_HIERARCHY_LEVEL

} // namespace wxPrivate

// Helper to determine resulting type of implicit conversion in
// an expression with two arithmetic types.
template<typename T1, typename T2>
struct wxImplicitConversionType
{
    typedef typename wxIf
            <
                // if T2 is "higher" type, convert to it
                (int)(wxPrivate::TypeHierarchy<T1>::level) < (int)(wxPrivate::TypeHierarchy<T2>::level),
                T2,
                // otherwise use T1
                T1
            >::value
            value;
};


template<typename T1, typename T2, typename T3>
struct wxImplicitConversionType3 : public wxImplicitConversionType<
                        T1,
                        typename wxImplicitConversionType<T2,T3>::value>
{
};

#endif // _WX_META_IMPLICITCONVERSION_H_