blob: 0871147bde0fe50b2b37ab0dcc59537178b15bcf [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
Jeff Thompson2491bd62013-10-15 17:10:24 -07006#ifndef FILE_ndnboost_type_traits_integral_promotion_hpp_INCLUDED
7#define FILE_ndnboost_type_traits_integral_promotion_hpp_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -07008
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070040#define NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(T) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070041 template<> struct need_promotion<T> \
42 : public integral_constant<bool, (sizeof(T) < sizeof(int))> {};
43
Jeff Thompson9939dcd2013-10-15 15:12:24 -070044// Same set of integral types as in ndnboost/type_traits/is_integral.hpp.
Jeff Thompsona28eed82013-08-22 16:21:10 -070045// Please, keep in sync.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070046#if (defined(NDNBOOST_MSVC) && (NDNBOOST_MSVC < 1300)) \
47 || (defined(NDNBOOST_INTEL_CXX_VERSION) && defined(_MSC_VER) && (NDNBOOST_INTEL_CXX_VERSION <= 600)) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070048 || (defined(__BORLANDC__) && (__BORLANDC__ == 0x600) && (_MSC_VER < 1300))
49// TODO: common macro for this #if. Or better yet, PP SEQ of non-standard types.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int8 )
51NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int8 )
52NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int16 )
53NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int16)
54NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int32 )
55NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int32)
Jeff Thompsona28eed82013-08-22 16:21:10 -070056#ifdef __BORLANDC__
Jeff Thompson3d613fd2013-10-15 15:39:04 -070057NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
58NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
Jeff Thompsona28eed82013-08-22 16:21:10 -070059#endif
60#endif
61
Jeff Thompson3d613fd2013-10-15 15:39:04 -070062#if defined(NDNBOOST_HAS_LONG_LONG)
63NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(ndnboost::ulong_long_type)
64NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(ndnboost::long_long_type )
65#elif defined(NDNBOOST_HAS_MS_INT64)
66NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
67NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
Jeff Thompsona28eed82013-08-22 16:21:10 -070068#endif
69
Jeff Thompson3d613fd2013-10-15 15:39:04 -070070#undef NDNBOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE
Jeff Thompsona28eed82013-08-22 16:21:10 -070071
72
Jeff Thompson3d613fd2013-10-15 15:39:04 -070073#ifndef NDNBOOST_NO_INTRINSIC_WCHAR_T
Jeff Thompsona28eed82013-08-22 16:21:10 -070074// 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070088#define NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(N,T) \
Jeff Thompsona28eed82013-08-22 16:21:10 -070089 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070095NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(1, int )
96NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(2, unsigned int )
97NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(3, long )
98NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(4, unsigned long)
Jeff Thompsona28eed82013-08-22 16:21:10 -070099
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700106#if defined(NDNBOOST_HAS_LONG_LONG)
107NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(5, ndnboost::long_long_type )
108NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(6, ndnboost::ulong_long_type)
109#elif defined(NDNBOOST_HAS_MS_INT64)
110NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(7, __int64 )
111NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX(8, unsigned __int64)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700112#endif
113
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700114#undef NDNBOOST_TT_AUX_PROMOTE_FROM_INDEX
Jeff Thompsona28eed82013-08-22 16:21:10 -0700115
116
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700117// Define NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER:
118#if !defined(NDNBOOST_MSVC)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700119
120template<int N>
121struct sized_type_for_promotion
122{
123 typedef char (&type)[N];
124};
125
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700126#define NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700127 sized_type_for_promotion<I>::type promoted_index_tester(T);
128
129#else
130
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700131#define NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700132 char (&promoted_index_tester(T))[I];
133
134#endif
135
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(1, int )
137NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(2, unsigned int )
138NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(3, long )
139NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(4, unsigned long)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700140
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700141#if defined(NDNBOOST_HAS_LONG_LONG)
142NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(5, ndnboost::long_long_type )
143NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(6, ndnboost::ulong_long_type)
144#elif defined(NDNBOOST_HAS_MS_INT64)
145NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(7, __int64 )
146NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER(8, unsigned __int64)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700147#endif
148
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700149#undef NDNBOOST_TT_AUX_PROMOTED_INDEX_TESTER
Jeff Thompsona28eed82013-08-22 16:21:10 -0700150
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700158 NDNBOOST_STATIC_CONSTANT(int, value = sizeof(promoted_index_tester(+testee)) );
Jeff Thompsona28eed82013-08-22 16:21:10 -0700159 // Unary plus promotes testee LOOK HERE ---> ^
160};
161
162template<class T>
163struct integral_promotion_impl
164{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700165 typedef NDNBOOST_DEDUCED_TYPENAME promote_from_index<
Jeff Thompsona28eed82013-08-22 16:21:10 -0700166 (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<
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700175 need_promotion<NDNBOOST_DEDUCED_TYPENAME remove_cv<T>::type>
Jeff Thompsona28eed82013-08-22 16:21:10 -0700176 , integral_promotion_impl<T>
177 , ndnboost::mpl::identity<T>
178 >
179{
180};
181
182} }
183
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700184NDNBOOST_TT_AUX_TYPE_TRAIT_DEF1(
Jeff Thompsona28eed82013-08-22 16:21:10 -0700185 integral_promotion
186 , T
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700187 , NDNBOOST_DEDUCED_TYPENAME
Jeff Thompsona28eed82013-08-22 16:21:10 -0700188 ndnboost::type_traits::detail::integral_promotion<T>::type
189 )
190}
191
192#include <ndnboost/type_traits/detail/type_trait_undef.hpp>
193
Jeff Thompson2491bd62013-10-15 17:10:24 -0700194#endif // #ifndef FILE_ndnboost_type_traits_integral_promotion_hpp_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -0700195