1 | // Copyright 2005 Alexander Nasonov. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED |
---|
7 | #define FILE_boost_type_traits_integral_promotion_hpp_INCLUDED |
---|
8 | |
---|
9 | #include <boost/config.hpp> |
---|
10 | |
---|
11 | #include <boost/mpl/eval_if.hpp> |
---|
12 | #include <boost/mpl/identity.hpp> |
---|
13 | #include <boost/type_traits/integral_constant.hpp> |
---|
14 | #include <boost/type_traits/is_const.hpp> |
---|
15 | #include <boost/type_traits/is_enum.hpp> |
---|
16 | #include <boost/type_traits/is_volatile.hpp> |
---|
17 | #include <boost/type_traits/remove_cv.hpp> |
---|
18 | |
---|
19 | // Should be the last #include |
---|
20 | #include <boost/type_traits/detail/type_trait_def.hpp> |
---|
21 | |
---|
22 | namespace boost { |
---|
23 | |
---|
24 | namespace type_traits { namespace detail { |
---|
25 | |
---|
26 | // 4.5/2 |
---|
27 | template <class T> struct need_promotion : boost::is_enum<T> {}; |
---|
28 | |
---|
29 | // 4.5/1 |
---|
30 | template<> struct need_promotion<char > : true_type {}; |
---|
31 | template<> struct need_promotion<signed char > : true_type {}; |
---|
32 | template<> struct need_promotion<unsigned char > : true_type {}; |
---|
33 | template<> struct need_promotion<signed short int > : true_type {}; |
---|
34 | template<> struct need_promotion<unsigned short int> : true_type {}; |
---|
35 | |
---|
36 | |
---|
37 | // Specializations for non-standard types. |
---|
38 | // Type is promoted if it's smaller then int. |
---|
39 | |
---|
40 | #define BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(T) \ |
---|
41 | template<> struct need_promotion<T> \ |
---|
42 | : integral_constant<bool, (sizeof(T) < sizeof(int))> {}; |
---|
43 | |
---|
44 | // Same set of integral types as in boost/type_traits/is_integral.hpp. |
---|
45 | // Please, keep in sync. |
---|
46 | #if (defined(BOOST_MSVC) && (BOOST_MSVC < 1300)) \ |
---|
47 | || (defined(BOOST_INTEL_CXX_VERSION) && defined(_MSC_VER) && (BOOST_INTEL_CXX_VERSION <= 600)) \ |
---|
48 | || (defined(__BORLANDC__) && (__BORLANDC__ == 0x600) && (_MSC_VER < 1300)) |
---|
49 | // TODO: common macro for this #if. Or better yet, PP SEQ of non-standard types. |
---|
50 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int8 ) |
---|
51 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int8 ) |
---|
52 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int16 ) |
---|
53 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int16) |
---|
54 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int32 ) |
---|
55 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int32) |
---|
56 | #ifdef __BORLANDC__ |
---|
57 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64) |
---|
58 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64) |
---|
59 | #endif |
---|
60 | #endif |
---|
61 | |
---|
62 | #if defined(BOOST_HAS_LONG_LONG) |
---|
63 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::ulong_long_type) |
---|
64 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::long_long_type ) |
---|
65 | #elif defined(BOOST_HAS_MS_INT64) |
---|
66 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64) |
---|
67 | BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64) |
---|
68 | #endif |
---|
69 | |
---|
70 | #undef BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE |
---|
71 | |
---|
72 | |
---|
73 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T |
---|
74 | // 4.5/2 |
---|
75 | template<> struct need_promotion<wchar_t> : true_type {}; |
---|
76 | #endif |
---|
77 | |
---|
78 | // 4.5/3 (integral bit-field) is not supported. |
---|
79 | |
---|
80 | // 4.5/4 |
---|
81 | template<> struct need_promotion<bool> : true_type {}; |
---|
82 | |
---|
83 | |
---|
84 | // Get promoted type by index and cv qualifiers. |
---|
85 | |
---|
86 | template<int Index, int IsConst, int IsVolatile> struct promote_from_index; |
---|
87 | |
---|
88 | #define BOOST_TT_AUX_PROMOTE_FROM_INDEX(N,T) \ |
---|
89 | template<> struct promote_from_index<N,0,0> { typedef T type; }; \ |
---|
90 | template<> struct promote_from_index<N,0,1> { typedef T volatile type; }; \ |
---|
91 | template<> struct promote_from_index<N,1,0> { typedef T const type; }; \ |
---|
92 | template<> struct promote_from_index<N,1,1> { typedef T const volatile type; }; |
---|
93 | |
---|
94 | |
---|
95 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(1, int ) |
---|
96 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(2, unsigned int ) |
---|
97 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(3, long ) |
---|
98 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(4, unsigned long) |
---|
99 | |
---|
100 | |
---|
101 | // WARNING: integral promotions to non-standard types |
---|
102 | // long long and __int64 are not defined by the standard. |
---|
103 | // Additional specialisations and overloads shouldn't |
---|
104 | // introduce ambiguity, though. |
---|
105 | |
---|
106 | #if defined(BOOST_HAS_LONG_LONG) |
---|
107 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(5, boost::long_long_type ) |
---|
108 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(6, boost::ulong_long_type) |
---|
109 | #elif defined(BOOST_HAS_MS_INT64) |
---|
110 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(7, __int64 ) |
---|
111 | BOOST_TT_AUX_PROMOTE_FROM_INDEX(8, unsigned __int64) |
---|
112 | #endif |
---|
113 | |
---|
114 | #undef BOOST_TT_AUX_PROMOTE_FROM_INDEX |
---|
115 | |
---|
116 | |
---|
117 | // Define BOOST_TT_AUX_PROMOTED_INDEX_TESTER: |
---|
118 | #if !defined(BOOST_MSVC) |
---|
119 | |
---|
120 | template<int N> |
---|
121 | struct sized_type_for_promotion |
---|
122 | { |
---|
123 | typedef char (&type)[N]; |
---|
124 | }; |
---|
125 | |
---|
126 | #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \ |
---|
127 | sized_type_for_promotion<I>::type promoted_index_tester(T); |
---|
128 | |
---|
129 | #else |
---|
130 | |
---|
131 | #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \ |
---|
132 | char (&promoted_index_tester(T))[I]; |
---|
133 | |
---|
134 | #endif |
---|
135 | |
---|
136 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(1, int ) |
---|
137 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(2, unsigned int ) |
---|
138 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(3, long ) |
---|
139 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(4, unsigned long) |
---|
140 | |
---|
141 | #if defined(BOOST_HAS_LONG_LONG) |
---|
142 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(5, boost::long_long_type ) |
---|
143 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(6, boost::ulong_long_type) |
---|
144 | #elif defined(BOOST_HAS_MS_INT64) |
---|
145 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(7, __int64 ) |
---|
146 | BOOST_TT_AUX_PROMOTED_INDEX_TESTER(8, unsigned __int64) |
---|
147 | #endif |
---|
148 | |
---|
149 | #undef BOOST_TT_AUX_PROMOTED_INDEX_TESTER |
---|
150 | |
---|
151 | |
---|
152 | // Get an index of promoted type for type T. |
---|
153 | // Precondition: need_promotion<T> |
---|
154 | template<class T> |
---|
155 | struct promoted_index |
---|
156 | { |
---|
157 | static T testee; // undefined |
---|
158 | BOOST_STATIC_CONSTANT(int, value = sizeof(promoted_index_tester(+testee)) ); |
---|
159 | // Unary plus promotes testee LOOK HERE ---> ^ |
---|
160 | }; |
---|
161 | |
---|
162 | template<class T> |
---|
163 | struct integral_promotion_impl |
---|
164 | { |
---|
165 | typedef BOOST_DEDUCED_TYPENAME promote_from_index< |
---|
166 | (boost::type_traits::detail::promoted_index<T>::value) |
---|
167 | , (boost::is_const<T>::value) |
---|
168 | , (boost::is_volatile<T>::value) |
---|
169 | >::type type; |
---|
170 | }; |
---|
171 | |
---|
172 | template<class T> |
---|
173 | struct integral_promotion |
---|
174 | : boost::mpl::eval_if< |
---|
175 | need_promotion<BOOST_DEDUCED_TYPENAME remove_cv<T>::type> |
---|
176 | , integral_promotion_impl<T> |
---|
177 | , boost::mpl::identity<T> |
---|
178 | > |
---|
179 | { |
---|
180 | }; |
---|
181 | |
---|
182 | } } |
---|
183 | |
---|
184 | BOOST_TT_AUX_TYPE_TRAIT_DEF1( |
---|
185 | integral_promotion |
---|
186 | , T |
---|
187 | , BOOST_DEDUCED_TYPENAME |
---|
188 | boost::type_traits::detail::integral_promotion<T>::type |
---|
189 | ) |
---|
190 | } |
---|
191 | |
---|
192 | #include <boost/type_traits/detail/type_trait_undef.hpp> |
---|
193 | |
---|
194 | #endif // #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED |
---|
195 | |
---|