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