integer.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // boost integer.hpp header file -------------------------------------------//
  2. // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/integer for documentation.
  6. // Revision History
  7. // 22 Sep 01 Added value-based integer templates. (Daryle Walker)
  8. // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
  9. // 30 Jul 00 Add typename syntax fix (Jens Maurer)
  10. // 28 Aug 99 Initial version
  11. #ifndef BOOST_INTEGER_HPP
  12. #define BOOST_INTEGER_HPP
  13. #include <boost/integer_fwd.hpp> // self include
  14. #include <boost/integer_traits.hpp> // for boost::integer_traits
  15. #include <boost/limits.hpp> // for std::numeric_limits
  16. namespace boost
  17. {
  18. // Helper templates ------------------------------------------------------//
  19. // fast integers from least integers
  20. // int_fast_t<> works correctly for unsigned too, in spite of the name.
  21. template< typename LeastInt >
  22. struct int_fast_t { typedef LeastInt fast; }; // imps may specialize
  23. // convert category to type
  24. template< int Category > struct int_least_helper {}; // default is empty
  25. // specializatons: 1=long, 2=int, 3=short, 4=signed char,
  26. // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
  27. // no specializations for 0 and 5: requests for a type > long are in error
  28. template<> struct int_least_helper<1> { typedef long least; };
  29. template<> struct int_least_helper<2> { typedef int least; };
  30. template<> struct int_least_helper<3> { typedef short least; };
  31. template<> struct int_least_helper<4> { typedef signed char least; };
  32. template<> struct int_least_helper<6> { typedef unsigned long least; };
  33. template<> struct int_least_helper<7> { typedef unsigned int least; };
  34. template<> struct int_least_helper<8> { typedef unsigned short least; };
  35. template<> struct int_least_helper<9> { typedef unsigned char least; };
  36. // integer templates specifying number of bits ---------------------------//
  37. // signed
  38. template< int Bits > // bits (including sign) required
  39. struct int_t
  40. {
  41. typedef typename int_least_helper
  42. <
  43. (Bits-1 <= std::numeric_limits<long>::digits) +
  44. (Bits-1 <= std::numeric_limits<int>::digits) +
  45. (Bits-1 <= std::numeric_limits<short>::digits) +
  46. (Bits-1 <= std::numeric_limits<signed char>::digits)
  47. >::least least;
  48. typedef typename int_fast_t<least>::fast fast;
  49. };
  50. // unsigned
  51. template< int Bits > // bits required
  52. struct uint_t
  53. {
  54. typedef typename int_least_helper
  55. <
  56. 5 +
  57. (Bits <= std::numeric_limits<unsigned long>::digits) +
  58. (Bits <= std::numeric_limits<unsigned int>::digits) +
  59. (Bits <= std::numeric_limits<unsigned short>::digits) +
  60. (Bits <= std::numeric_limits<unsigned char>::digits)
  61. >::least least;
  62. typedef typename int_fast_t<least>::fast fast;
  63. // int_fast_t<> works correctly for unsigned too, in spite of the name.
  64. };
  65. // integer templates specifying extreme value ----------------------------//
  66. // signed
  67. template< long MaxValue > // maximum value to require support
  68. struct int_max_value_t
  69. {
  70. typedef typename int_least_helper
  71. <
  72. (MaxValue <= integer_traits<long>::const_max) +
  73. (MaxValue <= integer_traits<int>::const_max) +
  74. (MaxValue <= integer_traits<short>::const_max) +
  75. (MaxValue <= integer_traits<signed char>::const_max)
  76. >::least least;
  77. typedef typename int_fast_t<least>::fast fast;
  78. };
  79. template< long MinValue > // minimum value to require support
  80. struct int_min_value_t
  81. {
  82. typedef typename int_least_helper
  83. <
  84. (MinValue >= integer_traits<long>::const_min) +
  85. (MinValue >= integer_traits<int>::const_min) +
  86. (MinValue >= integer_traits<short>::const_min) +
  87. (MinValue >= integer_traits<signed char>::const_min)
  88. >::least least;
  89. typedef typename int_fast_t<least>::fast fast;
  90. };
  91. // unsigned
  92. template< unsigned long Value > // maximum value to require support
  93. struct uint_value_t
  94. {
  95. typedef typename int_least_helper
  96. <
  97. 5 +
  98. (Value <= integer_traits<unsigned long>::const_max) +
  99. (Value <= integer_traits<unsigned int>::const_max) +
  100. (Value <= integer_traits<unsigned short>::const_max) +
  101. (Value <= integer_traits<unsigned char>::const_max)
  102. >::least least;
  103. typedef typename int_fast_t<least>::fast fast;
  104. };
  105. } // namespace boost
  106. #endif // BOOST_INTEGER_HPP