]>
Commit | Line | Data |
---|---|---|
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$ | |
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 | // We simply cannot include this header on gcc without getting copious warnings of the kind: | |
32 | // | |
33 | // ../../../boost/integer_traits.hpp:164:66: warning: use of C99 long long integer constant | |
34 | // | |
35 | // And yet there is no other reasonable implementation, so we declare this a system header | |
36 | // to suppress these warnings. | |
37 | // | |
38 | #if defined(__GNUC__) && (__GNUC__ >= 4) | |
39 | #pragma GCC system_header | |
40 | #endif | |
41 | ||
42 | namespace boost { | |
43 | template<class T> | |
44 | class integer_traits : public std::numeric_limits<T> | |
45 | { | |
46 | public: | |
47 | BOOST_STATIC_CONSTANT(bool, is_integral = false); | |
48 | }; | |
49 | ||
50 | namespace detail { | |
51 | template<class T, T min_val, T max_val> | |
52 | class integer_traits_base | |
53 | { | |
54 | public: | |
55 | BOOST_STATIC_CONSTANT(bool, is_integral = true); | |
56 | BOOST_STATIC_CONSTANT(T, const_min = min_val); | |
57 | BOOST_STATIC_CONSTANT(T, const_max = max_val); | |
58 | }; | |
59 | ||
60 | #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION | |
61 | // A definition is required even for integral static constants | |
62 | template<class T, T min_val, T max_val> | |
63 | const bool integer_traits_base<T, min_val, max_val>::is_integral; | |
64 | ||
65 | template<class T, T min_val, T max_val> | |
66 | const T integer_traits_base<T, min_val, max_val>::const_min; | |
67 | ||
68 | template<class T, T min_val, T max_val> | |
69 | const T integer_traits_base<T, min_val, max_val>::const_max; | |
70 | #endif | |
71 | ||
72 | } // namespace detail | |
73 | ||
74 | template<> | |
75 | class integer_traits<bool> | |
76 | : public std::numeric_limits<bool>, | |
77 | public detail::integer_traits_base<bool, false, true> | |
78 | { }; | |
79 | ||
80 | template<> | |
81 | class integer_traits<char> | |
82 | : public std::numeric_limits<char>, | |
83 | public detail::integer_traits_base<char, CHAR_MIN, CHAR_MAX> | |
84 | { }; | |
85 | ||
86 | template<> | |
87 | class integer_traits<signed char> | |
88 | : public std::numeric_limits<signed char>, | |
89 | public detail::integer_traits_base<signed char, SCHAR_MIN, SCHAR_MAX> | |
90 | { }; | |
91 | ||
92 | template<> | |
93 | class integer_traits<unsigned char> | |
94 | : public std::numeric_limits<unsigned char>, | |
95 | public detail::integer_traits_base<unsigned char, 0, UCHAR_MAX> | |
96 | { }; | |
97 | ||
98 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
99 | template<> | |
100 | class integer_traits<wchar_t> | |
101 | : public std::numeric_limits<wchar_t>, | |
102 | // Don't trust WCHAR_MIN and WCHAR_MAX with Mac OS X's native | |
103 | // library: they are wrong! | |
104 | #if defined(WCHAR_MIN) && defined(WCHAR_MAX) && !defined(__APPLE__) | |
105 | public detail::integer_traits_base<wchar_t, WCHAR_MIN, WCHAR_MAX> | |
106 | #elif defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__BEOS__) && defined(__GNUC__)) | |
107 | // No WCHAR_MIN and WCHAR_MAX, whar_t is short and unsigned: | |
108 | public detail::integer_traits_base<wchar_t, 0, 0xffff> | |
109 | #elif (defined(__sgi) && (!defined(__SGI_STL_PORT) || __SGI_STL_PORT < 0x400))\ | |
110 | || (defined __APPLE__)\ | |
111 | || (defined(__OpenBSD__) && defined(__GNUC__))\ | |
112 | || (defined(__NetBSD__) && defined(__GNUC__))\ | |
113 | || (defined(__FreeBSD__) && defined(__GNUC__))\ | |
114 | || (defined(__DragonFly__) && defined(__GNUC__))\ | |
115 | || (defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 3) && !defined(__SGI_STL_PORT)) | |
116 | // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as int. | |
117 | // - SGI MIPSpro with native library | |
118 | // - gcc 3.x on HP-UX | |
119 | // - Mac OS X with native library | |
120 | // - gcc on FreeBSD, OpenBSD and NetBSD | |
121 | public detail::integer_traits_base<wchar_t, INT_MIN, INT_MAX> | |
122 | #else | |
123 | #error No WCHAR_MIN and WCHAR_MAX present, please adjust integer_traits<> for your compiler. | |
124 | #endif | |
125 | { }; | |
126 | #endif // BOOST_NO_INTRINSIC_WCHAR_T | |
127 | ||
128 | template<> | |
129 | class integer_traits<short> | |
130 | : public std::numeric_limits<short>, | |
131 | public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX> | |
132 | { }; | |
133 | ||
134 | template<> | |
135 | class integer_traits<unsigned short> | |
136 | : public std::numeric_limits<unsigned short>, | |
137 | public detail::integer_traits_base<unsigned short, 0, USHRT_MAX> | |
138 | { }; | |
139 | ||
140 | template<> | |
141 | class integer_traits<int> | |
142 | : public std::numeric_limits<int>, | |
143 | public detail::integer_traits_base<int, INT_MIN, INT_MAX> | |
144 | { }; | |
145 | ||
146 | template<> | |
147 | class integer_traits<unsigned int> | |
148 | : public std::numeric_limits<unsigned int>, | |
149 | public detail::integer_traits_base<unsigned int, 0, UINT_MAX> | |
150 | { }; | |
151 | ||
152 | template<> | |
153 | class integer_traits<long> | |
154 | : public std::numeric_limits<long>, | |
155 | public detail::integer_traits_base<long, LONG_MIN, LONG_MAX> | |
156 | { }; | |
157 | ||
158 | template<> | |
159 | class integer_traits<unsigned long> | |
160 | : public std::numeric_limits<unsigned long>, | |
161 | public detail::integer_traits_base<unsigned long, 0, ULONG_MAX> | |
162 | { }; | |
163 | ||
164 | #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) | |
165 | #if defined(ULLONG_MAX) && defined(BOOST_HAS_LONG_LONG) | |
166 | ||
167 | template<> | |
168 | class integer_traits< ::boost::long_long_type> | |
169 | : public std::numeric_limits< ::boost::long_long_type>, | |
170 | public detail::integer_traits_base< ::boost::long_long_type, LLONG_MIN, LLONG_MAX> | |
171 | { }; | |
172 | ||
173 | template<> | |
174 | class integer_traits< ::boost::ulong_long_type> | |
175 | : public std::numeric_limits< ::boost::ulong_long_type>, | |
176 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULLONG_MAX> | |
177 | { }; | |
178 | ||
179 | #elif defined(ULONG_LONG_MAX) && defined(BOOST_HAS_LONG_LONG) | |
180 | ||
181 | template<> | |
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>{ }; | |
183 | template<> | |
184 | class integer_traits< ::boost::ulong_long_type> | |
185 | : public std::numeric_limits< ::boost::ulong_long_type>, | |
186 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONG_LONG_MAX> | |
187 | { }; | |
188 | ||
189 | #elif defined(ULONGLONG_MAX) && defined(BOOST_HAS_LONG_LONG) | |
190 | ||
191 | template<> | |
192 | class integer_traits< ::boost::long_long_type> | |
193 | : public std::numeric_limits< ::boost::long_long_type>, | |
194 | public detail::integer_traits_base< ::boost::long_long_type, LONGLONG_MIN, LONGLONG_MAX> | |
195 | { }; | |
196 | ||
197 | template<> | |
198 | class integer_traits< ::boost::ulong_long_type> | |
199 | : public std::numeric_limits< ::boost::ulong_long_type>, | |
200 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONGLONG_MAX> | |
201 | { }; | |
202 | ||
203 | #elif defined(_LLONG_MAX) && defined(_C2) && defined(BOOST_HAS_LONG_LONG) | |
204 | ||
205 | template<> | |
206 | class integer_traits< ::boost::long_long_type> | |
207 | : public std::numeric_limits< ::boost::long_long_type>, | |
208 | public detail::integer_traits_base< ::boost::long_long_type, -_LLONG_MAX - _C2, _LLONG_MAX> | |
209 | { }; | |
210 | ||
211 | template<> | |
212 | class integer_traits< ::boost::ulong_long_type> | |
213 | : public std::numeric_limits< ::boost::ulong_long_type>, | |
214 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, _ULLONG_MAX> | |
215 | { }; | |
216 | ||
217 | #elif defined(BOOST_HAS_LONG_LONG) | |
218 | // | |
219 | // we have long long but no constants, this happens for example with gcc in -ansi mode, | |
220 | // we'll just have to work out the values for ourselves (assumes 2's compliment representation): | |
221 | // | |
222 | template<> | |
223 | class integer_traits< ::boost::long_long_type> | |
224 | : public std::numeric_limits< ::boost::long_long_type>, | |
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))> | |
226 | { }; | |
227 | ||
228 | template<> | |
229 | class integer_traits< ::boost::ulong_long_type> | |
230 | : public std::numeric_limits< ::boost::ulong_long_type>, | |
231 | public detail::integer_traits_base< ::boost::ulong_long_type, 0, ~0uLL> | |
232 | { }; | |
233 | ||
234 | #elif defined(BOOST_HAS_MS_INT64) | |
235 | ||
236 | template<> | |
237 | class integer_traits< __int64> | |
238 | : public std::numeric_limits< __int64>, | |
239 | public detail::integer_traits_base< __int64, _I64_MIN, _I64_MAX> | |
240 | { }; | |
241 | ||
242 | template<> | |
243 | class integer_traits< unsigned __int64> | |
244 | : public std::numeric_limits< unsigned __int64>, | |
245 | public detail::integer_traits_base< unsigned __int64, 0, _UI64_MAX> | |
246 | { }; | |
247 | ||
248 | #endif | |
249 | #endif | |
250 | ||
251 | } // namespace boost | |
252 | ||
253 | #endif /* BOOST_INTEGER_TRAITS_HPP */ | |
254 | ||
255 | ||
256 |