Overview   API Reference  

integer.hpp

00001 //  boost integer.hpp header file  -------------------------------------------//
00002 
00003 //  Copyright Beman Dawes and Daryle Walker 1999.  Distributed under the Boost
00004 //  Software License, Version 1.0. (See accompanying file
00005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00006 
00007 //  See http://www.boost.org/libs/integer for documentation.
00008 
00009 //  Revision History
00010 //   22 Sep 01  Added value-based integer templates. (Daryle Walker)
00011 //   01 Apr 01  Modified to use new <boost/limits.hpp> header. (John Maddock)
00012 //   30 Jul 00  Add typename syntax fix (Jens Maurer)
00013 //   28 Aug 99  Initial version
00014 
00015 #ifndef BOOST_INTEGER_HPP
00016 #define BOOST_INTEGER_HPP
00017 
00018 #include <boost/integer_fwd.hpp>  // self include
00019 
00020 #include <boost/integer_traits.hpp>  // for boost::integer_traits
00021 #include <boost/limits.hpp>          // for std::numeric_limits
00022 
00023 namespace boost
00024 {
00025 
00026   //  Helper templates  ------------------------------------------------------//
00027 
00028   //  fast integers from least integers
00029   //  int_fast_t<> works correctly for unsigned too, in spite of the name.
00030   template< typename LeastInt >
00031   struct int_fast_t { typedef LeastInt fast; }; // imps may specialize
00032 
00033   //  convert category to type 
00034   template< int Category > struct int_least_helper {}; // default is empty
00035 
00036   //  specializatons: 1=long, 2=int, 3=short, 4=signed char,
00037   //     6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned long
00038   //  no specializations for 0 and 5: requests for a type > long are in error
00039   template<> struct int_least_helper<1> { typedef long least; };
00040   template<> struct int_least_helper<2> { typedef int least; };
00041   template<> struct int_least_helper<3> { typedef short least; };
00042   template<> struct int_least_helper<4> { typedef signed char least; };
00043   template<> struct int_least_helper<6> { typedef unsigned long least; };
00044   template<> struct int_least_helper<7> { typedef unsigned int least; };
00045   template<> struct int_least_helper<8> { typedef unsigned short least; };
00046   template<> struct int_least_helper<9> { typedef unsigned char least; };
00047 
00048   //  integer templates specifying number of bits  ---------------------------//
00049 
00050   //  signed
00051   template< int Bits >   // bits (including sign) required
00052   struct int_t 
00053   {
00054       typedef typename int_least_helper
00055         <
00056           (Bits-1 <= std::numeric_limits<long>::digits) +
00057           (Bits-1 <= std::numeric_limits<int>::digits) +
00058           (Bits-1 <= std::numeric_limits<short>::digits) +
00059           (Bits-1 <= std::numeric_limits<signed char>::digits)
00060         >::least  least;
00061       typedef typename int_fast_t<least>::fast  fast;
00062   };
00063 
00064   //  unsigned
00065   template< int Bits >   // bits required
00066   struct uint_t 
00067   {
00068       typedef typename int_least_helper
00069         < 
00070           5 +
00071           (Bits <= std::numeric_limits<unsigned long>::digits) +
00072           (Bits <= std::numeric_limits<unsigned int>::digits) +
00073           (Bits <= std::numeric_limits<unsigned short>::digits) +
00074           (Bits <= std::numeric_limits<unsigned char>::digits)
00075         >::least  least;
00076       typedef typename int_fast_t<least>::fast  fast;
00077       // int_fast_t<> works correctly for unsigned too, in spite of the name.
00078   };
00079 
00080   //  integer templates specifying extreme value  ----------------------------//
00081 
00082   //  signed
00083   template< long MaxValue >   // maximum value to require support
00084   struct int_max_value_t 
00085   {
00086       typedef typename int_least_helper
00087         <
00088           (MaxValue <= integer_traits<long>::const_max) +
00089           (MaxValue <= integer_traits<int>::const_max) +
00090           (MaxValue <= integer_traits<short>::const_max) +
00091           (MaxValue <= integer_traits<signed char>::const_max)
00092         >::least  least;
00093       typedef typename int_fast_t<least>::fast  fast;
00094   };
00095 
00096   template< long MinValue >   // minimum value to require support
00097   struct int_min_value_t 
00098   {
00099       typedef typename int_least_helper
00100         <
00101           (MinValue >= integer_traits<long>::const_min) +
00102           (MinValue >= integer_traits<int>::const_min) +
00103           (MinValue >= integer_traits<short>::const_min) +
00104           (MinValue >= integer_traits<signed char>::const_min)
00105         >::least  least;
00106       typedef typename int_fast_t<least>::fast  fast;
00107   };
00108 
00109   //  unsigned
00110   template< unsigned long Value >   // maximum value to require support
00111   struct uint_value_t 
00112   {
00113       typedef typename int_least_helper
00114         < 
00115           5 +
00116           (Value <= integer_traits<unsigned long>::const_max) +
00117           (Value <= integer_traits<unsigned int>::const_max) +
00118           (Value <= integer_traits<unsigned short>::const_max) +
00119           (Value <= integer_traits<unsigned char>::const_max)
00120         >::least  least;
00121       typedef typename int_fast_t<least>::fast  fast;
00122   };
00123 
00124 
00125 } // namespace boost
00126 
00127 #endif  // BOOST_INTEGER_HPP

L4vmm Reference Manual, written by Mario Schwalbe  © 2006-2008