123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #ifndef BOOST_GIL_PROMOTE_INTEGRAL_HPP
- #define BOOST_GIL_PROMOTE_INTEGRAL_HPP
- #include <boost/mp11/list.hpp>
- #include <climits>
- #include <cstddef>
- #include <type_traits>
- namespace boost { namespace gil
- {
- namespace detail { namespace promote_integral
- {
- template
- <
- typename T,
- bool IsFundamental = std::is_fundamental<T>::value
- >
- struct bit_size {};
- template <typename T>
- struct bit_size<T, true> : std::integral_constant<std::size_t, (CHAR_BIT * sizeof(T))> {};
- template
- <
- typename T,
- typename IntegralTypes,
- std::size_t MinSize
- >
- struct promote_to_larger
- {
- using current_type = boost::mp11::mp_first<IntegralTypes>;
- using list_after_front = boost::mp11::mp_rest<IntegralTypes>;
- using type = typename std::conditional
- <
- (bit_size<current_type>::value >= MinSize),
- current_type,
- typename promote_to_larger
- <
- T,
- list_after_front,
- MinSize
- >::type
- >::type;
- };
- template <typename T, std::size_t MinSize>
- struct promote_to_larger<T, boost::mp11::mp_list<>, MinSize>
- {
-
-
- using type = T;
- };
- }}
- template
- <
- typename T,
- bool PromoteUnsignedToUnsigned = false,
- bool UseCheckedInteger = false,
- bool IsIntegral = std::is_integral<T>::value
- >
- class promote_integral
- {
- private:
- static bool const is_unsigned = std::is_unsigned<T>::value;
- using bit_size_type = detail::promote_integral::bit_size<T>;
-
-
-
-
-
-
-
- using min_bit_size_type = typename std::conditional
- <
- (PromoteUnsignedToUnsigned && is_unsigned),
- std::integral_constant<std::size_t, (2 * bit_size_type::value)>,
- typename std::conditional
- <
- is_unsigned,
- std::integral_constant<std::size_t, (2 * bit_size_type::value + 1)>,
- std::integral_constant<std::size_t, (2 * bit_size_type::value - 1)>
- >::type
- >::type;
-
-
- using signed_integral_types = boost::mp11::mp_list
- <
- short, int, long
- #if defined(BOOST_HAS_LONG_LONG)
- , boost::long_long_type
- #endif
- >;
-
-
- using unsigned_integral_types = boost::mp11::mp_list
- <
- unsigned short, unsigned int, unsigned long, std::size_t
- #if defined(BOOST_HAS_LONG_LONG)
- , boost::ulong_long_type
- #endif
- >;
-
-
-
- using integral_types = typename std::conditional
- <
- (is_unsigned && PromoteUnsignedToUnsigned),
- unsigned_integral_types,
- signed_integral_types
- >::type;
- public:
- using type = typename detail::promote_integral::promote_to_larger
- <
- T,
- integral_types,
- min_bit_size_type::value
- >::type;
- };
- template <typename T, bool PromoteUnsignedToUnsigned, bool UseCheckedInteger>
- class promote_integral
- <
- T, PromoteUnsignedToUnsigned, UseCheckedInteger, false
- >
- {
- public:
- using type = T;
- };
- }}
- #endif
|