make_unsigned.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // (C) Copyright John Maddock 2007.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. //
  6. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  7. #ifndef BOOST_TT_MAKE_UNSIGNED_HPP_INCLUDED
  8. #define BOOST_TT_MAKE_UNSIGNED_HPP_INCLUDED
  9. #include <boost/type_traits/conditional.hpp>
  10. #include <boost/type_traits/is_integral.hpp>
  11. #include <boost/type_traits/is_signed.hpp>
  12. #include <boost/type_traits/is_unsigned.hpp>
  13. #include <boost/type_traits/is_enum.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/remove_cv.hpp>
  16. #include <boost/type_traits/is_const.hpp>
  17. #include <boost/type_traits/is_volatile.hpp>
  18. #include <boost/type_traits/add_const.hpp>
  19. #include <boost/type_traits/add_volatile.hpp>
  20. #include <boost/static_assert.hpp>
  21. namespace boost {
  22. template <class T>
  23. struct make_unsigned
  24. {
  25. private:
  26. BOOST_STATIC_ASSERT_MSG((::boost::is_integral<T>::value || ::boost::is_enum<T>::value), "The template argument to make_unsigned must be an integer or enum type.");
  27. BOOST_STATIC_ASSERT_MSG((! ::boost::is_same<typename remove_cv<T>::type, bool>::value), "The template argument to make_unsigned must not be the type bool");
  28. typedef typename remove_cv<T>::type t_no_cv;
  29. typedef typename conditional<
  30. (::boost::is_unsigned<T>::value && ::boost::is_integral<T>::value
  31. && ! ::boost::is_same<t_no_cv, char>::value
  32. && ! ::boost::is_same<t_no_cv, wchar_t>::value
  33. && ! ::boost::is_same<t_no_cv, bool>::value),
  34. T,
  35. typename conditional<
  36. (::boost::is_integral<T>::value
  37. && ! ::boost::is_same<t_no_cv, char>::value
  38. && ! ::boost::is_same<t_no_cv, wchar_t>::value
  39. && ! ::boost::is_same<t_no_cv, bool>::value),
  40. typename conditional<
  41. is_same<t_no_cv, signed char>::value,
  42. unsigned char,
  43. typename conditional<
  44. is_same<t_no_cv, short>::value,
  45. unsigned short,
  46. typename conditional<
  47. is_same<t_no_cv, int>::value,
  48. unsigned int,
  49. typename conditional<
  50. is_same<t_no_cv, long>::value,
  51. unsigned long,
  52. #if defined(BOOST_HAS_LONG_LONG)
  53. #ifdef BOOST_HAS_INT128
  54. typename conditional<
  55. sizeof(t_no_cv) == sizeof(boost::ulong_long_type),
  56. boost::ulong_long_type,
  57. boost::uint128_type
  58. >::type
  59. #else
  60. boost::ulong_long_type
  61. #endif
  62. #elif defined(BOOST_HAS_MS_INT64)
  63. unsigned __int64
  64. #else
  65. unsigned long
  66. #endif
  67. >::type
  68. >::type
  69. >::type
  70. >::type,
  71. // Not a regular integer type:
  72. typename conditional<
  73. sizeof(t_no_cv) == sizeof(unsigned char),
  74. unsigned char,
  75. typename conditional<
  76. sizeof(t_no_cv) == sizeof(unsigned short),
  77. unsigned short,
  78. typename conditional<
  79. sizeof(t_no_cv) == sizeof(unsigned int),
  80. unsigned int,
  81. typename conditional<
  82. sizeof(t_no_cv) == sizeof(unsigned long),
  83. unsigned long,
  84. #if defined(BOOST_HAS_LONG_LONG)
  85. #ifdef BOOST_HAS_INT128
  86. typename conditional<
  87. sizeof(t_no_cv) == sizeof(boost::ulong_long_type),
  88. boost::ulong_long_type,
  89. boost::uint128_type
  90. >::type
  91. #else
  92. boost::ulong_long_type
  93. #endif
  94. #elif defined(BOOST_HAS_MS_INT64)
  95. unsigned __int64
  96. #else
  97. unsigned long
  98. #endif
  99. >::type
  100. >::type
  101. >::type
  102. >::type
  103. >::type
  104. >::type base_integer_type;
  105. // Add back any const qualifier:
  106. typedef typename conditional<
  107. is_const<T>::value,
  108. typename add_const<base_integer_type>::type,
  109. base_integer_type
  110. >::type const_base_integer_type;
  111. public:
  112. // Add back any volatile qualifier:
  113. typedef typename conditional<
  114. is_volatile<T>::value,
  115. typename add_volatile<const_base_integer_type>::type,
  116. const_base_integer_type
  117. >::type type;
  118. };
  119. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  120. template <class T> using make_unsigned_t = typename make_unsigned<T>::type;
  121. #endif
  122. } // namespace boost
  123. #endif // BOOST_TT_ADD_REFERENCE_HPP_INCLUDED