[29] | 1 | /* boost integer_traits.hpp header file |
---|
| 2 | * |
---|
| 3 | * Copyright Jens Maurer 2000 |
---|
| 4 | * Distributed under the Boost Software License, Version 1.0. (See |
---|
| 5 | * accompanying file LICENSE_1_0.txt or copy at |
---|
| 6 | * http://www.boost.org/LICENSE_1_0.txt) |
---|
| 7 | * |
---|
| 8 | * $Id: integer_traits.hpp,v 1.30 2006/02/05 10:19:42 johnmaddock Exp $ |
---|
| 9 | * |
---|
| 10 | * Idea by Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | // See http://www.boost.org/libs/integer for documentation. |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #ifndef BOOST_INTEGER_TRAITS_HPP |
---|
| 17 | #define BOOST_INTEGER_TRAITS_HPP |
---|
| 18 | |
---|
| 19 | #include <boost/config.hpp> |
---|
| 20 | #include <boost/limits.hpp> |
---|
| 21 | |
---|
| 22 | // These are an implementation detail and not part of the interface |
---|
| 23 | #include <limits.h> |
---|
| 24 | // we need wchar.h for WCHAR_MAX/MIN but not all platforms provide it, |
---|
| 25 | // and some may have <wchar.h> but not <cwchar> ... |
---|
| 26 | #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && (!defined(BOOST_NO_CWCHAR) || defined(sun) || defined(__sun) || defined(__QNX__)) |
---|
| 27 | #include <wchar.h> |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | namespace boost { |
---|
| 32 | template<class T> |
---|
| 33 | class integer_traits : public std::numeric_limits<T> |
---|
| 34 | { |
---|
| 35 | public: |
---|
| 36 | BOOST_STATIC_CONSTANT(bool, is_integral = false); |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | namespace detail { |
---|
| 40 | template<class T, T min_val, T max_val> |
---|
| 41 | class integer_traits_base |
---|
| 42 | { |
---|
| 43 | public: |
---|
| 44 | BOOST_STATIC_CONSTANT(bool, is_integral = true); |
---|
| 45 | BOOST_STATIC_CONSTANT(T, const_min = min_val); |
---|
| 46 | BOOST_STATIC_CONSTANT(T, const_max = max_val); |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION |
---|
| 50 | // A definition is required even for integral static constants |
---|
| 51 | template<class T, T min_val, T max_val> |
---|
| 52 | const bool integer_traits_base<T, min_val, max_val>::is_integral; |
---|
| 53 | |
---|
| 54 | template<class T, T min_val, T max_val> |
---|
| 55 | const T integer_traits_base<T, min_val, max_val>::const_min; |
---|
| 56 | |
---|
| 57 | template<class T, T min_val, T max_val> |
---|
| 58 | const T integer_traits_base<T, min_val, max_val>::const_max; |
---|
| 59 | #endif |
---|
| 60 | |
---|
| 61 | } // namespace detail |
---|
| 62 | |
---|
| 63 | template<> |
---|
| 64 | class integer_traits<bool> |
---|
| 65 | : public std::numeric_limits<bool>, |
---|
| 66 | public detail::integer_traits_base<bool, false, true> |
---|
| 67 | { }; |
---|
| 68 | |
---|
| 69 | template<> |
---|
| 70 | class integer_traits<char> |
---|
| 71 | : public std::numeric_limits<char>, |
---|
| 72 | public detail::integer_traits_base<char, CHAR_MIN, CHAR_MAX> |
---|
| 73 | { }; |
---|
| 74 | |
---|
| 75 | template<> |
---|
| 76 | class integer_traits<signed char> |
---|
| 77 | : public std::numeric_limits<signed char>, |
---|
| 78 | public detail::integer_traits_base<signed char, SCHAR_MIN, SCHAR_MAX> |
---|
| 79 | { }; |
---|
| 80 | |
---|
| 81 | template<> |
---|
| 82 | class integer_traits<unsigned char> |
---|
| 83 | : public std::numeric_limits<unsigned char>, |
---|
| 84 | public detail::integer_traits_base<unsigned char, 0, UCHAR_MAX> |
---|
| 85 | { }; |
---|
| 86 | |
---|
| 87 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T |
---|
| 88 | template<> |
---|
| 89 | class integer_traits<wchar_t> |
---|
| 90 | : public std::numeric_limits<wchar_t>, |
---|
| 91 | // Don't trust WCHAR_MIN and WCHAR_MAX with Mac OS X's native |
---|
| 92 | // library: they are wrong! |
---|
| 93 | #if defined(WCHAR_MIN) && defined(WCHAR_MAX) && !defined(__APPLE__) |
---|
| 94 | public detail::integer_traits_base<wchar_t, WCHAR_MIN, WCHAR_MAX> |
---|
| 95 | #elif defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__BEOS__) && defined(__GNUC__)) |
---|
| 96 | // No WCHAR_MIN and WCHAR_MAX, whar_t is short and unsigned: |
---|
| 97 | public detail::integer_traits_base<wchar_t, 0, 0xffff> |
---|
| 98 | #elif (defined(__sgi) && (!defined(__SGI_STL_PORT) || __SGI_STL_PORT < 0x400))\ |
---|
| 99 | || (defined __APPLE__)\ |
---|
| 100 | || (defined(__OpenBSD__) && defined(__GNUC__))\ |
---|
| 101 | || (defined(__NetBSD__) && defined(__GNUC__))\ |
---|
| 102 | || (defined(__FreeBSD__) && defined(__GNUC__))\ |
---|
| 103 | || (defined(__DragonFly__) && defined(__GNUC__))\ |
---|
| 104 | || (defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 3) && !defined(__SGI_STL_PORT)) |
---|
| 105 | // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as int. |
---|
| 106 | // - SGI MIPSpro with native library |
---|
| 107 | // - gcc 3.x on HP-UX |
---|
| 108 | // - Mac OS X with native library |
---|
| 109 | // - gcc on FreeBSD, OpenBSD and NetBSD |
---|
| 110 | public detail::integer_traits_base<wchar_t, INT_MIN, INT_MAX> |
---|
| 111 | #elif defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 2) && !defined(__SGI_STL_PORT) |
---|
| 112 | // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as unsigned int. |
---|
| 113 | // - gcc 2.95.x on HP-UX |
---|
| 114 | // (also, std::numeric_limits<wchar_t> appears to return the wrong values). |
---|
| 115 | public detail::integer_traits_base<wchar_t, 0, UINT_MAX> |
---|
| 116 | #else |
---|
| 117 | #error No WCHAR_MIN and WCHAR_MAX present, please adjust integer_traits<> for your compiler. |
---|
| 118 | #endif |
---|
| 119 | { }; |
---|
| 120 | #endif // BOOST_NO_INTRINSIC_WCHAR_T |
---|
| 121 | |
---|
| 122 | template<> |
---|
| 123 | class integer_traits<short> |
---|
| 124 | : public std::numeric_limits<short>, |
---|
| 125 | public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX> |
---|
| 126 | { }; |
---|
| 127 | |
---|
| 128 | template<> |
---|
| 129 | class integer_traits<unsigned short> |
---|
| 130 | : public std::numeric_limits<unsigned short>, |
---|
| 131 | public detail::integer_traits_base<unsigned short, 0, USHRT_MAX> |
---|
| 132 | { }; |
---|
| 133 | |
---|
| 134 | template<> |
---|
| 135 | class integer_traits<int> |
---|
| 136 | : public std::numeric_limits<int>, |
---|
| 137 | public detail::integer_traits_base<int, INT_MIN, INT_MAX> |
---|
| 138 | { }; |
---|
| 139 | |
---|
| 140 | template<> |
---|
| 141 | class integer_traits<unsigned int> |
---|
| 142 | : public std::numeric_limits<unsigned int>, |
---|
| 143 | public detail::integer_traits_base<unsigned int, 0, UINT_MAX> |
---|
| 144 | { }; |
---|
| 145 | |
---|
| 146 | template<> |
---|
| 147 | class integer_traits<long> |
---|
| 148 | : public std::numeric_limits<long>, |
---|
| 149 | public detail::integer_traits_base<long, LONG_MIN, LONG_MAX> |
---|
| 150 | { }; |
---|
| 151 | |
---|
| 152 | template<> |
---|
| 153 | class integer_traits<unsigned long> |
---|
| 154 | : public std::numeric_limits<unsigned long>, |
---|
| 155 | public detail::integer_traits_base<unsigned long, 0, ULONG_MAX> |
---|
| 156 | { }; |
---|
| 157 | |
---|
| 158 | #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) |
---|
| 159 | #if defined(ULLONG_MAX) && defined(BOOST_HAS_LONG_LONG) |
---|
| 160 | |
---|
| 161 | template<> |
---|
| 162 | class integer_traits< ::boost::long_long_type> |
---|
| 163 | : public std::numeric_limits< ::boost::long_long_type>, |
---|
| 164 | public detail::integer_traits_base< ::boost::long_long_type, LLONG_MIN, LLONG_MAX> |
---|
| 165 | { }; |
---|
| 166 | |
---|
| 167 | template<> |
---|
| 168 | class integer_traits< ::boost::ulong_long_type> |
---|
| 169 | : public std::numeric_limits< ::boost::ulong_long_type>, |
---|
| 170 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULLONG_MAX> |
---|
| 171 | { }; |
---|
| 172 | |
---|
| 173 | #elif defined(ULONG_LONG_MAX) && defined(BOOST_HAS_LONG_LONG) |
---|
| 174 | |
---|
| 175 | template<> |
---|
| 176 | 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>{ }; |
---|
| 177 | template<> |
---|
| 178 | class integer_traits< ::boost::ulong_long_type> |
---|
| 179 | : public std::numeric_limits< ::boost::ulong_long_type>, |
---|
| 180 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONG_LONG_MAX> |
---|
| 181 | { }; |
---|
| 182 | |
---|
| 183 | #elif defined(ULONGLONG_MAX) && defined(BOOST_HAS_LONG_LONG) |
---|
| 184 | |
---|
| 185 | template<> |
---|
| 186 | class integer_traits< ::boost::long_long_type> |
---|
| 187 | : public std::numeric_limits< ::boost::long_long_type>, |
---|
| 188 | public detail::integer_traits_base< ::boost::long_long_type, LONGLONG_MIN, LONGLONG_MAX> |
---|
| 189 | { }; |
---|
| 190 | |
---|
| 191 | template<> |
---|
| 192 | class integer_traits< ::boost::ulong_long_type> |
---|
| 193 | : public std::numeric_limits< ::boost::ulong_long_type>, |
---|
| 194 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONGLONG_MAX> |
---|
| 195 | { }; |
---|
| 196 | |
---|
| 197 | #elif defined(_LLONG_MAX) && defined(_C2) && defined(BOOST_HAS_LONG_LONG) |
---|
| 198 | |
---|
| 199 | template<> |
---|
| 200 | class integer_traits< ::boost::long_long_type> |
---|
| 201 | : public std::numeric_limits< ::boost::long_long_type>, |
---|
| 202 | public detail::integer_traits_base< ::boost::long_long_type, -_LLONG_MAX - _C2, _LLONG_MAX> |
---|
| 203 | { }; |
---|
| 204 | |
---|
| 205 | template<> |
---|
| 206 | class integer_traits< ::boost::ulong_long_type> |
---|
| 207 | : public std::numeric_limits< ::boost::ulong_long_type>, |
---|
| 208 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, _ULLONG_MAX> |
---|
| 209 | { }; |
---|
| 210 | |
---|
| 211 | #elif defined(BOOST_HAS_LONG_LONG) |
---|
| 212 | // |
---|
| 213 | // we have long long but no constants, this happens for example with gcc in -ansi mode, |
---|
| 214 | // we'll just have to work out the values for ourselves (assumes 2's compliment representation): |
---|
| 215 | // |
---|
| 216 | template<> |
---|
| 217 | class integer_traits< ::boost::long_long_type> |
---|
| 218 | : public std::numeric_limits< ::boost::long_long_type>, |
---|
| 219 | public detail::integer_traits_base< ::boost::long_long_type, (1LL << (sizeof(::boost::long_long_type) - 1)), ~(1LL << (sizeof(::boost::long_long_type) - 1))> |
---|
| 220 | { }; |
---|
| 221 | |
---|
| 222 | template<> |
---|
| 223 | class integer_traits< ::boost::ulong_long_type> |
---|
| 224 | : public std::numeric_limits< ::boost::ulong_long_type>, |
---|
| 225 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ~0uLL> |
---|
| 226 | { }; |
---|
| 227 | |
---|
| 228 | #endif |
---|
| 229 | #endif |
---|
| 230 | |
---|
| 231 | } // namespace boost |
---|
| 232 | |
---|
| 233 | #endif /* BOOST_INTEGER_TRAITS_HPP */ |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | |
---|