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