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