1 // boost integer.hpp header file -------------------------------------------//
3 // (C) Copyright Beman Dawes 1999. Permission to copy, use, modify, sell
4 // and distribute this software is granted provided this copyright
5 // notice appears in all copies. This software is provided "as is" without
6 // express or implied warranty, and with no claim as to its suitability for
9 // See http://www.boost.org for most recent version including documentation.
12 // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
13 // 30 Jul 00 Add typename syntax fix (Jens Maurer)
14 // 28 Aug 99 Initial version
16 #ifndef BOOST_INTEGER_HPP
17 #define BOOST_INTEGER_HPP
19 #include <boost/limits.hpp>
24 // Helper templates ------------------------------------------------------//
26 // fast integers from least integers
27 // int_fast_t<> works correctly for unsigned too, in spite of the name.
28 template< typename LeastInt >
29 struct int_fast_t { typedef LeastInt fast; }; // imps may specialize
31 // convert category to type
32 template< int Category > struct int_least_helper {}; // default is empty
34 // specializatons: 1=long, 2=int, 3=short, 4=signed char,
35 // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned long
36 // no specializations for 0 and 5: requests for a type > long are in error
37 template<> struct int_least_helper<1> { typedef long least; };
38 template<> struct int_least_helper<2> { typedef int least; };
39 template<> struct int_least_helper<3> { typedef short least; };
40 template<> struct int_least_helper<4> { typedef signed char least; };
41 template<> struct int_least_helper<6> { typedef unsigned long least; };
42 template<> struct int_least_helper<7> { typedef unsigned int least; };
43 template<> struct int_least_helper<8> { typedef unsigned short least; };
44 template<> struct int_least_helper<9> { typedef unsigned char least; };
46 // integer templates specifying number of bits ---------------------------//
49 template< int Bits > // bits (including sign) required
52 typedef typename int_least_helper
54 (Bits-1 <= std::numeric_limits<long>::digits) +
55 (Bits-1 <= std::numeric_limits<int>::digits) +
56 (Bits-1 <= std::numeric_limits<short>::digits) +
57 (Bits-1 <= std::numeric_limits<signed char>::digits)
59 typedef typename int_fast_t<least>::fast fast;
63 template< int Bits > // bits required
66 typedef typename int_least_helper
69 (Bits <= std::numeric_limits<unsigned long>::digits) +
70 (Bits <= std::numeric_limits<unsigned int>::digits) +
71 (Bits <= std::numeric_limits<unsigned short>::digits) +
72 (Bits <= std::numeric_limits<unsigned char>::digits)
74 typedef typename int_fast_t<least>::fast fast;
75 // int_fast_t<> works correctly for unsigned too, in spite of the name.
78 // The same dispatching technique can be used to select types based on
79 // values. That will be added once boost::integer_traits is available.
84 #endif // BOOST_INTEGER_HPP