Make wxMin, wxMax and wxClip template functions.
[wxWidgets.git] / include / wx / meta / implicitconversion.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/meta/implicitconversion.h
3 // Purpose: Determine resulting type from implicit conversion
4 // Author: Vaclav Slavik
5 // Created: 2010-10-22
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_META_IMPLICITCONVERSION_H_
12 #define _WX_META_IMPLICITCONVERSION_H_
13
14 #include "wx/defs.h"
15 #include "wx/meta/if.h"
16
17 // C++ hierarchy of data types is:
18 //
19 // Long double (highest)
20 // Double
21 // Float
22 // Unsigned long int
23 // Long int
24 // Unsigned int
25 // Int (lowest)
26 //
27 // Types lower in the hierarchy are converted into ones higher up if both are
28 // involved e.g. in arithmetic expressions.
29
30 namespace wxPrivate
31 {
32
33 template<typename T>
34 struct TypeHierarchy
35 {
36 // consider unknown types (e.g. objects, pointers) to be of highest
37 // level, always convert to them if they occur
38 enum { level = 9999 };
39 };
40
41 #define WX_TYPE_HIERARCHY_LEVEL(level_num, type) \
42 template<> struct TypeHierarchy<type> \
43 { \
44 enum { level = level_num }; \
45 };
46
47 WX_TYPE_HIERARCHY_LEVEL( 1, char);
48 WX_TYPE_HIERARCHY_LEVEL( 2, unsigned char);
49 WX_TYPE_HIERARCHY_LEVEL( 3, short);
50 WX_TYPE_HIERARCHY_LEVEL( 4, unsigned short);
51 WX_TYPE_HIERARCHY_LEVEL( 5, int);
52 WX_TYPE_HIERARCHY_LEVEL( 6, unsigned int);
53 WX_TYPE_HIERARCHY_LEVEL( 7, long);
54 WX_TYPE_HIERARCHY_LEVEL( 8, unsigned long);
55 #ifdef wxLongLong_t
56 WX_TYPE_HIERARCHY_LEVEL( 9, wxLongLong_t);
57 WX_TYPE_HIERARCHY_LEVEL(10, wxULongLong_t);
58 #endif
59 WX_TYPE_HIERARCHY_LEVEL(11, float);
60 WX_TYPE_HIERARCHY_LEVEL(12, double);
61 WX_TYPE_HIERARCHY_LEVEL(13, long double);
62
63 #if wxWCHAR_T_IS_REAL_TYPE
64 #if SIZEOF_WCHAR_T == SIZEOF_SHORT
65 template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<short> {};
66 #elif SIZEOF_WCHAR_T == SIZEOF_INT
67 template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<int> {};
68 #elif SIZEOF_WCHAR_T == SIZEOF_LONG
69 template<> struct TypeHierarchy<wchar_t> : public TypeHierarchy<long> {};
70 #else
71 #error "weird wchar_t size, please update this code"
72 #endif
73 #endif
74
75 #undef WX_TYPE_HIERARCHY_LEVEL
76
77 } // namespace wxPrivate
78
79 // Helper to determine resulting type of implicit conversion in
80 // an expression with two arithmetic types.
81 template<typename T1, typename T2>
82 struct wxImplicitConversionType
83 {
84 typedef typename wxIf
85 <
86 // if T2 is "higher" type, convert to it
87 (int)wxPrivate::TypeHierarchy<T1>::level < (int)wxPrivate::TypeHierarchy<T2>::level,
88 T2,
89 // otherwise use T1
90 T1
91 >::value
92 value;
93 };
94
95
96 template<typename T1, typename T2, typename T3>
97 struct wxImplicitConversionType3 : public wxImplicitConversionType<
98 T1,
99 typename wxImplicitConversionType<T2,T3>::value>
100 {
101 };
102
103 #endif // _WX_META_IMPLICITCONVERSION_H_