williamr@2: // boost integer.hpp header file -------------------------------------------// williamr@2: williamr@2: // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost williamr@2: // Software License, Version 1.0. (See accompanying file williamr@2: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) williamr@2: williamr@2: // See http://www.boost.org/libs/integer for documentation. williamr@2: williamr@2: // Revision History williamr@2: // 22 Sep 01 Added value-based integer templates. (Daryle Walker) williamr@2: // 01 Apr 01 Modified to use new header. (John Maddock) williamr@2: // 30 Jul 00 Add typename syntax fix (Jens Maurer) williamr@2: // 28 Aug 99 Initial version williamr@2: williamr@2: #ifndef BOOST_INTEGER_HPP williamr@2: #define BOOST_INTEGER_HPP williamr@2: williamr@2: #include // self include williamr@2: williamr@2: #include // for boost::integer_traits williamr@2: #include // for std::numeric_limits williamr@2: williamr@2: namespace boost williamr@2: { williamr@2: williamr@2: // Helper templates ------------------------------------------------------// williamr@2: williamr@2: // fast integers from least integers williamr@2: // int_fast_t<> works correctly for unsigned too, in spite of the name. williamr@2: template< typename LeastInt > williamr@2: struct int_fast_t { typedef LeastInt fast; }; // imps may specialize williamr@2: williamr@2: // convert category to type williamr@2: template< int Category > struct int_least_helper {}; // default is empty williamr@2: williamr@2: // specializatons: 1=long, 2=int, 3=short, 4=signed char, williamr@2: // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned long williamr@2: // no specializations for 0 and 5: requests for a type > long are in error williamr@2: template<> struct int_least_helper<1> { typedef long least; }; williamr@2: template<> struct int_least_helper<2> { typedef int least; }; williamr@2: template<> struct int_least_helper<3> { typedef short least; }; williamr@2: template<> struct int_least_helper<4> { typedef signed char least; }; williamr@2: template<> struct int_least_helper<6> { typedef unsigned long least; }; williamr@2: template<> struct int_least_helper<7> { typedef unsigned int least; }; williamr@2: template<> struct int_least_helper<8> { typedef unsigned short least; }; williamr@2: template<> struct int_least_helper<9> { typedef unsigned char least; }; williamr@2: williamr@2: // integer templates specifying number of bits ---------------------------// williamr@2: williamr@2: // signed williamr@2: template< int Bits > // bits (including sign) required williamr@2: struct int_t williamr@2: { williamr@2: typedef typename int_least_helper williamr@2: < williamr@2: (Bits-1 <= std::numeric_limits::digits) + williamr@2: (Bits-1 <= std::numeric_limits::digits) + williamr@2: (Bits-1 <= std::numeric_limits::digits) + williamr@2: (Bits-1 <= std::numeric_limits::digits) williamr@2: >::least least; williamr@2: typedef typename int_fast_t::fast fast; williamr@2: }; williamr@2: williamr@2: // unsigned williamr@2: template< int Bits > // bits required williamr@2: struct uint_t williamr@2: { williamr@2: typedef typename int_least_helper williamr@2: < williamr@2: 5 + williamr@2: (Bits <= std::numeric_limits::digits) + williamr@2: (Bits <= std::numeric_limits::digits) + williamr@2: (Bits <= std::numeric_limits::digits) + williamr@2: (Bits <= std::numeric_limits::digits) williamr@2: >::least least; williamr@2: typedef typename int_fast_t::fast fast; williamr@2: // int_fast_t<> works correctly for unsigned too, in spite of the name. williamr@2: }; williamr@2: williamr@2: // integer templates specifying extreme value ----------------------------// williamr@2: williamr@2: // signed williamr@2: template< long MaxValue > // maximum value to require support williamr@2: struct int_max_value_t williamr@2: { williamr@2: typedef typename int_least_helper williamr@2: < williamr@2: (MaxValue <= integer_traits::const_max) + williamr@2: (MaxValue <= integer_traits::const_max) + williamr@2: (MaxValue <= integer_traits::const_max) + williamr@2: (MaxValue <= integer_traits::const_max) williamr@2: >::least least; williamr@2: typedef typename int_fast_t::fast fast; williamr@2: }; williamr@2: williamr@2: template< long MinValue > // minimum value to require support williamr@2: struct int_min_value_t williamr@2: { williamr@2: typedef typename int_least_helper williamr@2: < williamr@2: (MinValue >= integer_traits::const_min) + williamr@2: (MinValue >= integer_traits::const_min) + williamr@2: (MinValue >= integer_traits::const_min) + williamr@2: (MinValue >= integer_traits::const_min) williamr@2: >::least least; williamr@2: typedef typename int_fast_t::fast fast; williamr@2: }; williamr@2: williamr@2: // unsigned williamr@2: template< unsigned long Value > // maximum value to require support williamr@2: struct uint_value_t williamr@2: { williamr@2: typedef typename int_least_helper williamr@2: < williamr@2: 5 + williamr@2: (Value <= integer_traits::const_max) + williamr@2: (Value <= integer_traits::const_max) + williamr@2: (Value <= integer_traits::const_max) + williamr@2: (Value <= integer_traits::const_max) williamr@2: >::least least; williamr@2: typedef typename int_fast_t::fast fast; williamr@2: }; williamr@2: williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: #endif // BOOST_INTEGER_HPP