blob: 46cf11819ed5f55da1bd0d2b5ec382e1db92789f [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// 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 <ndnboost/config.hpp>
10
11#include <ndnboost/mpl/eval_if.hpp>
12#include <ndnboost/mpl/identity.hpp>
13#include <ndnboost/type_traits/integral_constant.hpp>
14#include <ndnboost/type_traits/is_const.hpp>
15#include <ndnboost/type_traits/is_enum.hpp>
16#include <ndnboost/type_traits/is_volatile.hpp>
17#include <ndnboost/type_traits/remove_cv.hpp>
18
19// Should be the last #include
20#include <ndnboost/type_traits/detail/type_trait_def.hpp>
21
22namespace ndnboost {
23
24namespace type_traits { namespace detail {
25
26// 4.5/2
27template <class T> struct need_promotion : public ndnboost::is_enum<T> {};
28
29// 4.5/1
30template<> struct need_promotion<char > : public true_type {};
31template<> struct need_promotion<signed char > : public true_type {};
32template<> struct need_promotion<unsigned char > : public true_type {};
33template<> struct need_promotion<signed short int > : public true_type {};
34template<> struct need_promotion<unsigned short int> : public 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 : public 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.
50BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int8 )
51BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int8 )
52BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int16 )
53BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int16)
54BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int32 )
55BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int32)
56#ifdef __BORLANDC__
57BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
58BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
59#endif
60#endif
61
62#if defined(BOOST_HAS_LONG_LONG)
63BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(ndnboost::ulong_long_type)
64BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(ndnboost::long_long_type )
65#elif defined(BOOST_HAS_MS_INT64)
66BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
67BOOST_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
75template<> struct need_promotion<wchar_t> : public true_type {};
76#endif
77
78// 4.5/3 (integral bit-field) is not supported.
79
80// 4.5/4
81template<> struct need_promotion<bool> : public true_type {};
82
83
84// Get promoted type by index and cv qualifiers.
85
86template<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
95BOOST_TT_AUX_PROMOTE_FROM_INDEX(1, int )
96BOOST_TT_AUX_PROMOTE_FROM_INDEX(2, unsigned int )
97BOOST_TT_AUX_PROMOTE_FROM_INDEX(3, long )
98BOOST_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)
107BOOST_TT_AUX_PROMOTE_FROM_INDEX(5, ndnboost::long_long_type )
108BOOST_TT_AUX_PROMOTE_FROM_INDEX(6, ndnboost::ulong_long_type)
109#elif defined(BOOST_HAS_MS_INT64)
110BOOST_TT_AUX_PROMOTE_FROM_INDEX(7, __int64 )
111BOOST_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
120template<int N>
121struct 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
136BOOST_TT_AUX_PROMOTED_INDEX_TESTER(1, int )
137BOOST_TT_AUX_PROMOTED_INDEX_TESTER(2, unsigned int )
138BOOST_TT_AUX_PROMOTED_INDEX_TESTER(3, long )
139BOOST_TT_AUX_PROMOTED_INDEX_TESTER(4, unsigned long)
140
141#if defined(BOOST_HAS_LONG_LONG)
142BOOST_TT_AUX_PROMOTED_INDEX_TESTER(5, ndnboost::long_long_type )
143BOOST_TT_AUX_PROMOTED_INDEX_TESTER(6, ndnboost::ulong_long_type)
144#elif defined(BOOST_HAS_MS_INT64)
145BOOST_TT_AUX_PROMOTED_INDEX_TESTER(7, __int64 )
146BOOST_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>
154template<class T>
155struct 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
162template<class T>
163struct integral_promotion_impl
164{
165 typedef BOOST_DEDUCED_TYPENAME promote_from_index<
166 (ndnboost::type_traits::detail::promoted_index<T>::value)
167 , (ndnboost::is_const<T>::value)
168 , (ndnboost::is_volatile<T>::value)
169 >::type type;
170};
171
172template<class T>
173struct integral_promotion
174 : public ndnboost::mpl::eval_if<
175 need_promotion<BOOST_DEDUCED_TYPENAME remove_cv<T>::type>
176 , integral_promotion_impl<T>
177 , ndnboost::mpl::identity<T>
178 >
179{
180};
181
182} }
183
184BOOST_TT_AUX_TYPE_TRAIT_DEF1(
185 integral_promotion
186 , T
187 , BOOST_DEDUCED_TYPENAME
188 ndnboost::type_traits::detail::integral_promotion<T>::type
189 )
190}
191
192#include <ndnboost/type_traits/detail/type_trait_undef.hpp>
193
194#endif // #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED
195