1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/meta/implicitconversion.h
3 // Purpose: Determine resulting type from implicit conversion
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2010 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_META_IMPLICITCONVERSION_H_
12 #define _WX_META_IMPLICITCONVERSION_H_
15 #include "wx/meta/if.h"
17 // C++ hierarchy of data types is:
19 // Long double (highest)
27 // Types lower in the hierarchy are converted into ones higher up if both are
28 // involved e.g. in arithmetic expressions.
33 // Helper macro to define a constant inside a template class: it's needed
34 // because MSVC6 doesn't support initializing static integer members but the
35 // usual workaround of using enums instead doesn't work for Borland (at least
36 // in template classes).
38 #define wxDEFINE_CLASS_INT_CONST(name, value) enum { name = value }
40 #define wxDEFINE_CLASS_INT_CONST(name, value) static const int name = value
46 // consider unknown types (e.g. objects, pointers) to be of highest
47 // level, always convert to them if they occur
48 wxDEFINE_CLASS_INT_CONST( level
, 9999 );
51 #define WX_TYPE_HIERARCHY_LEVEL(level_num, type) \
52 template<> struct TypeHierarchy<type> \
54 wxDEFINE_CLASS_INT_CONST( level, level_num ); \
57 WX_TYPE_HIERARCHY_LEVEL( 1, char);
58 WX_TYPE_HIERARCHY_LEVEL( 2, unsigned char);
59 WX_TYPE_HIERARCHY_LEVEL( 3, short);
60 WX_TYPE_HIERARCHY_LEVEL( 4, unsigned short);
61 WX_TYPE_HIERARCHY_LEVEL( 5, int);
62 WX_TYPE_HIERARCHY_LEVEL( 6, unsigned int);
63 WX_TYPE_HIERARCHY_LEVEL( 7, long);
64 WX_TYPE_HIERARCHY_LEVEL( 8, unsigned long);
66 WX_TYPE_HIERARCHY_LEVEL( 9, wxLongLong_t
);
67 WX_TYPE_HIERARCHY_LEVEL(10, wxULongLong_t
);
69 WX_TYPE_HIERARCHY_LEVEL(11, float);
70 WX_TYPE_HIERARCHY_LEVEL(12, double);
71 WX_TYPE_HIERARCHY_LEVEL(13, long double);
73 #if wxWCHAR_T_IS_REAL_TYPE
74 #if SIZEOF_WCHAR_T == SIZEOF_SHORT
75 template<> struct TypeHierarchy
<wchar_t> : public TypeHierarchy
<short> {};
76 #elif SIZEOF_WCHAR_T == SIZEOF_INT
77 template<> struct TypeHierarchy
<wchar_t> : public TypeHierarchy
<int> {};
78 #elif SIZEOF_WCHAR_T == SIZEOF_LONG
79 template<> struct TypeHierarchy
<wchar_t> : public TypeHierarchy
<long> {};
81 #error "weird wchar_t size, please update this code"
85 #undef WX_TYPE_HIERARCHY_LEVEL
87 } // namespace wxPrivate
89 // Helper to determine resulting type of implicit conversion in
90 // an expression with two arithmetic types.
91 template<typename T1
, typename T2
>
92 struct wxImplicitConversionType
96 // if T2 is "higher" type, convert to it
97 (int)(wxPrivate::TypeHierarchy
<T1
>::level
) < (int)(wxPrivate::TypeHierarchy
<T2
>::level
),
106 template<typename T1
, typename T2
, typename T3
>
107 struct wxImplicitConversionType3
: public wxImplicitConversionType
<
109 typename wxImplicitConversionType
<T2
,T3
>::value
>
113 #endif // _WX_META_IMPLICITCONVERSION_H_