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@101
|
8 * $Id$
|
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 #else
|
Chris@16
|
123 #error No WCHAR_MIN and WCHAR_MAX present, please adjust integer_traits<> for your compiler.
|
Chris@16
|
124 #endif
|
Chris@16
|
125 { };
|
Chris@16
|
126 #endif // BOOST_NO_INTRINSIC_WCHAR_T
|
Chris@16
|
127
|
Chris@16
|
128 template<>
|
Chris@16
|
129 class integer_traits<short>
|
Chris@16
|
130 : public std::numeric_limits<short>,
|
Chris@16
|
131 public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX>
|
Chris@16
|
132 { };
|
Chris@16
|
133
|
Chris@16
|
134 template<>
|
Chris@16
|
135 class integer_traits<unsigned short>
|
Chris@16
|
136 : public std::numeric_limits<unsigned short>,
|
Chris@16
|
137 public detail::integer_traits_base<unsigned short, 0, USHRT_MAX>
|
Chris@16
|
138 { };
|
Chris@16
|
139
|
Chris@16
|
140 template<>
|
Chris@16
|
141 class integer_traits<int>
|
Chris@16
|
142 : public std::numeric_limits<int>,
|
Chris@16
|
143 public detail::integer_traits_base<int, INT_MIN, INT_MAX>
|
Chris@16
|
144 { };
|
Chris@16
|
145
|
Chris@16
|
146 template<>
|
Chris@16
|
147 class integer_traits<unsigned int>
|
Chris@16
|
148 : public std::numeric_limits<unsigned int>,
|
Chris@16
|
149 public detail::integer_traits_base<unsigned int, 0, UINT_MAX>
|
Chris@16
|
150 { };
|
Chris@16
|
151
|
Chris@16
|
152 template<>
|
Chris@16
|
153 class integer_traits<long>
|
Chris@16
|
154 : public std::numeric_limits<long>,
|
Chris@16
|
155 public detail::integer_traits_base<long, LONG_MIN, LONG_MAX>
|
Chris@16
|
156 { };
|
Chris@16
|
157
|
Chris@16
|
158 template<>
|
Chris@16
|
159 class integer_traits<unsigned long>
|
Chris@16
|
160 : public std::numeric_limits<unsigned long>,
|
Chris@16
|
161 public detail::integer_traits_base<unsigned long, 0, ULONG_MAX>
|
Chris@16
|
162 { };
|
Chris@16
|
163
|
Chris@16
|
164 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T)
|
Chris@16
|
165 #if defined(ULLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
|
Chris@16
|
166
|
Chris@16
|
167 template<>
|
Chris@16
|
168 class integer_traits< ::boost::long_long_type>
|
Chris@16
|
169 : public std::numeric_limits< ::boost::long_long_type>,
|
Chris@16
|
170 public detail::integer_traits_base< ::boost::long_long_type, LLONG_MIN, LLONG_MAX>
|
Chris@16
|
171 { };
|
Chris@16
|
172
|
Chris@16
|
173 template<>
|
Chris@16
|
174 class integer_traits< ::boost::ulong_long_type>
|
Chris@16
|
175 : public std::numeric_limits< ::boost::ulong_long_type>,
|
Chris@16
|
176 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULLONG_MAX>
|
Chris@16
|
177 { };
|
Chris@16
|
178
|
Chris@16
|
179 #elif defined(ULONG_LONG_MAX) && defined(BOOST_HAS_LONG_LONG)
|
Chris@16
|
180
|
Chris@16
|
181 template<>
|
Chris@16
|
182 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
|
183 template<>
|
Chris@16
|
184 class integer_traits< ::boost::ulong_long_type>
|
Chris@16
|
185 : public std::numeric_limits< ::boost::ulong_long_type>,
|
Chris@16
|
186 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONG_LONG_MAX>
|
Chris@16
|
187 { };
|
Chris@16
|
188
|
Chris@16
|
189 #elif defined(ULONGLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
|
Chris@16
|
190
|
Chris@16
|
191 template<>
|
Chris@16
|
192 class integer_traits< ::boost::long_long_type>
|
Chris@16
|
193 : public std::numeric_limits< ::boost::long_long_type>,
|
Chris@16
|
194 public detail::integer_traits_base< ::boost::long_long_type, LONGLONG_MIN, LONGLONG_MAX>
|
Chris@16
|
195 { };
|
Chris@16
|
196
|
Chris@16
|
197 template<>
|
Chris@16
|
198 class integer_traits< ::boost::ulong_long_type>
|
Chris@16
|
199 : public std::numeric_limits< ::boost::ulong_long_type>,
|
Chris@16
|
200 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONGLONG_MAX>
|
Chris@16
|
201 { };
|
Chris@16
|
202
|
Chris@16
|
203 #elif defined(_LLONG_MAX) && defined(_C2) && defined(BOOST_HAS_LONG_LONG)
|
Chris@16
|
204
|
Chris@16
|
205 template<>
|
Chris@16
|
206 class integer_traits< ::boost::long_long_type>
|
Chris@16
|
207 : public std::numeric_limits< ::boost::long_long_type>,
|
Chris@16
|
208 public detail::integer_traits_base< ::boost::long_long_type, -_LLONG_MAX - _C2, _LLONG_MAX>
|
Chris@16
|
209 { };
|
Chris@16
|
210
|
Chris@16
|
211 template<>
|
Chris@16
|
212 class integer_traits< ::boost::ulong_long_type>
|
Chris@16
|
213 : public std::numeric_limits< ::boost::ulong_long_type>,
|
Chris@16
|
214 public detail::integer_traits_base< ::boost::ulong_long_type, 0, _ULLONG_MAX>
|
Chris@16
|
215 { };
|
Chris@16
|
216
|
Chris@16
|
217 #elif defined(BOOST_HAS_LONG_LONG)
|
Chris@16
|
218 //
|
Chris@16
|
219 // we have long long but no constants, this happens for example with gcc in -ansi mode,
|
Chris@16
|
220 // we'll just have to work out the values for ourselves (assumes 2's compliment representation):
|
Chris@16
|
221 //
|
Chris@16
|
222 template<>
|
Chris@16
|
223 class integer_traits< ::boost::long_long_type>
|
Chris@16
|
224 : public std::numeric_limits< ::boost::long_long_type>,
|
Chris@16
|
225 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
|
226 { };
|
Chris@16
|
227
|
Chris@16
|
228 template<>
|
Chris@16
|
229 class integer_traits< ::boost::ulong_long_type>
|
Chris@16
|
230 : public std::numeric_limits< ::boost::ulong_long_type>,
|
Chris@16
|
231 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ~0uLL>
|
Chris@16
|
232 { };
|
Chris@16
|
233
|
Chris@16
|
234 #elif defined(BOOST_HAS_MS_INT64)
|
Chris@16
|
235
|
Chris@16
|
236 template<>
|
Chris@16
|
237 class integer_traits< __int64>
|
Chris@16
|
238 : public std::numeric_limits< __int64>,
|
Chris@16
|
239 public detail::integer_traits_base< __int64, _I64_MIN, _I64_MAX>
|
Chris@16
|
240 { };
|
Chris@16
|
241
|
Chris@16
|
242 template<>
|
Chris@16
|
243 class integer_traits< unsigned __int64>
|
Chris@16
|
244 : public std::numeric_limits< unsigned __int64>,
|
Chris@16
|
245 public detail::integer_traits_base< unsigned __int64, 0, _UI64_MAX>
|
Chris@16
|
246 { };
|
Chris@16
|
247
|
Chris@16
|
248 #endif
|
Chris@16
|
249 #endif
|
Chris@16
|
250
|
Chris@16
|
251 } // namespace boost
|
Chris@16
|
252
|
Chris@16
|
253 #endif /* BOOST_INTEGER_TRAITS_HPP */
|
Chris@16
|
254
|
Chris@16
|
255
|
Chris@16
|
256
|