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