Overview   API Reference  

integer_traits.hpp

00001 /* boost integer_traits.hpp header file
00002  *
00003  * Copyright Jens Maurer 2000
00004  * Distributed under the Boost Software License, Version 1.0. (See
00005  * accompanying file LICENSE_1_0.txt or copy at
00006  * http://www.boost.org/LICENSE_1_0.txt)
00007  *
00008  * $Id: integer_traits.hpp,v 1.30 2006/02/05 10:19:42 johnmaddock Exp $
00009  *
00010  * Idea by Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers
00011  */
00012 
00013 //  See http://www.boost.org/libs/integer for documentation.
00014 
00015 
00016 #ifndef BOOST_INTEGER_TRAITS_HPP
00017 #define BOOST_INTEGER_TRAITS_HPP
00018 
00019 #include <boost/config.hpp>
00020 #include <boost/limits.hpp>
00021 
00022 // These are an implementation detail and not part of the interface
00023 #include <limits.h>
00024 // we need wchar.h for WCHAR_MAX/MIN but not all platforms provide it,
00025 // and some may have <wchar.h> but not <cwchar> ...
00026 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && (!defined(BOOST_NO_CWCHAR) || defined(sun) || defined(__sun) || defined(__QNX__))
00027 #include <wchar.h>
00028 #endif
00029 
00030 
00031 namespace boost {
00032 template<class T>
00033 class integer_traits : public std::numeric_limits<T>
00034 {
00035 public:
00036   BOOST_STATIC_CONSTANT(bool, is_integral = false);
00037 };
00038 
00039 namespace detail {
00040 template<class T, T min_val, T max_val>
00041 class integer_traits_base
00042 {
00043 public:
00044   BOOST_STATIC_CONSTANT(bool, is_integral = true);
00045   BOOST_STATIC_CONSTANT(T, const_min = min_val);
00046   BOOST_STATIC_CONSTANT(T, const_max = max_val);
00047 };
00048 
00049 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
00050 //  A definition is required even for integral static constants
00051 template<class T, T min_val, T max_val>
00052 const bool integer_traits_base<T, min_val, max_val>::is_integral;
00053 
00054 template<class T, T min_val, T max_val>
00055 const T integer_traits_base<T, min_val, max_val>::const_min;
00056 
00057 template<class T, T min_val, T max_val>
00058 const T integer_traits_base<T, min_val, max_val>::const_max;
00059 #endif
00060 
00061 } // namespace detail
00062 
00063 template<>
00064 class integer_traits<bool>
00065   : public std::numeric_limits<bool>,
00066     public detail::integer_traits_base<bool, false, true>
00067 { };
00068 
00069 template<>
00070 class integer_traits<char>
00071   : public std::numeric_limits<char>,
00072     public detail::integer_traits_base<char, CHAR_MIN, CHAR_MAX>
00073 { };
00074 
00075 template<>
00076 class integer_traits<signed char>
00077   : public std::numeric_limits<signed char>,
00078     public detail::integer_traits_base<signed char, SCHAR_MIN, SCHAR_MAX>
00079 { };
00080 
00081 template<>
00082 class integer_traits<unsigned char>
00083   : public std::numeric_limits<unsigned char>,
00084     public detail::integer_traits_base<unsigned char, 0, UCHAR_MAX>
00085 { };
00086 
00087 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
00088 template<>
00089 class integer_traits<wchar_t>
00090   : public std::numeric_limits<wchar_t>,
00091     // Don't trust WCHAR_MIN and WCHAR_MAX with Mac OS X's native
00092     // library: they are wrong!
00093 #if defined(WCHAR_MIN) && defined(WCHAR_MAX) && !defined(__APPLE__)
00094     public detail::integer_traits_base<wchar_t, WCHAR_MIN, WCHAR_MAX>
00095 #elif defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__BEOS__) && defined(__GNUC__))
00096     // No WCHAR_MIN and WCHAR_MAX, whar_t is short and unsigned:
00097     public detail::integer_traits_base<wchar_t, 0, 0xffff>
00098 #elif (defined(__sgi) && (!defined(__SGI_STL_PORT) || __SGI_STL_PORT < 0x400))\
00099     || (defined __APPLE__)\
00100     || (defined(__OpenBSD__) && defined(__GNUC__))\
00101     || (defined(__NetBSD__) && defined(__GNUC__))\
00102     || (defined(__FreeBSD__) && defined(__GNUC__))\
00103     || (defined(__DragonFly__) && defined(__GNUC__))\
00104     || (defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 3) && !defined(__SGI_STL_PORT))
00105     // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as int.
00106     //  - SGI MIPSpro with native library
00107     //  - gcc 3.x on HP-UX
00108     //  - Mac OS X with native library
00109     //  - gcc on FreeBSD, OpenBSD and NetBSD
00110     public detail::integer_traits_base<wchar_t, INT_MIN, INT_MAX>
00111 #elif defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 2) && !defined(__SGI_STL_PORT)
00112     // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as unsigned int.
00113     //  - gcc 2.95.x on HP-UX
00114     // (also, std::numeric_limits<wchar_t> appears to return the wrong values).
00115     public detail::integer_traits_base<wchar_t, 0, UINT_MAX>
00116 #else
00117 #error No WCHAR_MIN and WCHAR_MAX present, please adjust integer_traits<> for your compiler.
00118 #endif
00119 { };
00120 #endif // BOOST_NO_INTRINSIC_WCHAR_T
00121 
00122 template<>
00123 class integer_traits<short>
00124   : public std::numeric_limits<short>,
00125     public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX>
00126 { };
00127 
00128 template<>
00129 class integer_traits<unsigned short>
00130   : public std::numeric_limits<unsigned short>,
00131     public detail::integer_traits_base<unsigned short, 0, USHRT_MAX>
00132 { };
00133 
00134 template<>
00135 class integer_traits<int>
00136   : public std::numeric_limits<int>,
00137     public detail::integer_traits_base<int, INT_MIN, INT_MAX>
00138 { };
00139 
00140 template<>
00141 class integer_traits<unsigned int>
00142   : public std::numeric_limits<unsigned int>,
00143     public detail::integer_traits_base<unsigned int, 0, UINT_MAX>
00144 { };
00145 
00146 template<>
00147 class integer_traits<long>
00148   : public std::numeric_limits<long>,
00149     public detail::integer_traits_base<long, LONG_MIN, LONG_MAX>
00150 { };
00151 
00152 template<>
00153 class integer_traits<unsigned long>
00154   : public std::numeric_limits<unsigned long>,
00155     public detail::integer_traits_base<unsigned long, 0, ULONG_MAX>
00156 { };
00157 
00158 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T)
00159 #if defined(ULLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
00160 
00161 template<>
00162 class integer_traits< ::boost::long_long_type>
00163   : public std::numeric_limits< ::boost::long_long_type>,
00164     public detail::integer_traits_base< ::boost::long_long_type, LLONG_MIN, LLONG_MAX>
00165 { };
00166 
00167 template<>
00168 class integer_traits< ::boost::ulong_long_type>
00169   : public std::numeric_limits< ::boost::ulong_long_type>,
00170     public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULLONG_MAX>
00171 { };
00172 
00173 #elif defined(ULONG_LONG_MAX) && defined(BOOST_HAS_LONG_LONG)
00174 
00175 template<>
00176 class integer_traits< ::boost::long_long_type>  : public std::numeric_limits< ::boost::long_long_type>,    public detail::integer_traits_base< ::boost::long_long_type, LONG_LONG_MIN, LONG_LONG_MAX>{ };
00177 template<>
00178 class integer_traits< ::boost::ulong_long_type>
00179   : public std::numeric_limits< ::boost::ulong_long_type>,
00180     public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONG_LONG_MAX>
00181 { };
00182 
00183 #elif defined(ULONGLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
00184 
00185 template<>
00186 class integer_traits< ::boost::long_long_type>
00187   : public std::numeric_limits< ::boost::long_long_type>,
00188     public detail::integer_traits_base< ::boost::long_long_type, LONGLONG_MIN, LONGLONG_MAX>
00189 { };
00190 
00191 template<>
00192 class integer_traits< ::boost::ulong_long_type>
00193   : public std::numeric_limits< ::boost::ulong_long_type>,
00194     public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONGLONG_MAX>
00195 { };
00196 
00197 #elif defined(_LLONG_MAX) && defined(_C2) && defined(BOOST_HAS_LONG_LONG)
00198 
00199 template<>
00200 class integer_traits< ::boost::long_long_type>
00201   : public std::numeric_limits< ::boost::long_long_type>,
00202     public detail::integer_traits_base< ::boost::long_long_type, -_LLONG_MAX - _C2, _LLONG_MAX>
00203 { };
00204 
00205 template<>
00206 class integer_traits< ::boost::ulong_long_type>
00207   : public std::numeric_limits< ::boost::ulong_long_type>,
00208     public detail::integer_traits_base< ::boost::ulong_long_type, 0, _ULLONG_MAX>
00209 { };
00210 
00211 #elif defined(BOOST_HAS_LONG_LONG)
00212 //
00213 // we have long long but no constants, this happens for example with gcc in -ansi mode,
00214 // we'll just have to work out the values for ourselves (assumes 2's compliment representation):
00215 //
00216 template<>
00217 class integer_traits< ::boost::long_long_type>
00218   : public std::numeric_limits< ::boost::long_long_type>,
00219     public detail::integer_traits_base< ::boost::long_long_type, (1LL << (sizeof(::boost::long_long_type) - 1)), ~(1LL << (sizeof(::boost::long_long_type) - 1))>
00220 { };
00221 
00222 template<>
00223 class integer_traits< ::boost::ulong_long_type>
00224   : public std::numeric_limits< ::boost::ulong_long_type>,
00225     public detail::integer_traits_base< ::boost::ulong_long_type, 0, ~0uLL>
00226 { };
00227 
00228 #endif
00229 #endif
00230 
00231 } // namespace boost
00232 
00233 #endif /* BOOST_INTEGER_TRAITS_HPP */
00234 
00235 
00236 

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