source: NonGTP/Boost/boost/integer_traits.hpp @ 857

Revision 857, 8.0 KB checked in by igarcia, 19 years ago (diff)
Line 
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.27.2.1 2005/08/24 15:45:17 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))
27#include <wchar.h>
28#endif
29
30
31namespace boost {
32template<class T>
33class integer_traits : public std::numeric_limits<T>
34{
35public:
36  BOOST_STATIC_CONSTANT(bool, is_integral = false);
37};
38
39namespace detail {
40template<class T, T min_val, T max_val>
41class integer_traits_base
42{
43public:
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
51template<class T, T min_val, T max_val>
52const bool integer_traits_base<T, min_val, max_val>::is_integral;
53
54template<class T, T min_val, T max_val>
55const T integer_traits_base<T, min_val, max_val>::const_min;
56
57template<class T, T min_val, T max_val>
58const T integer_traits_base<T, min_val, max_val>::const_max;
59#endif
60
61} // namespace detail
62
63template<>
64class integer_traits<bool>
65  : public std::numeric_limits<bool>,
66    public detail::integer_traits_base<bool, false, true>
67{ };
68
69template<>
70class integer_traits<char>
71  : public std::numeric_limits<char>,
72    public detail::integer_traits_base<char, CHAR_MIN, CHAR_MAX>
73{ };
74
75template<>
76class 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
81template<>
82class 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
88template<>
89class 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
122template<>
123class integer_traits<short>
124  : public std::numeric_limits<short>,
125    public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX>
126{ };
127
128template<>
129class integer_traits<unsigned short>
130  : public std::numeric_limits<unsigned short>,
131    public detail::integer_traits_base<unsigned short, 0, USHRT_MAX>
132{ };
133
134template<>
135class integer_traits<int>
136  : public std::numeric_limits<int>,
137    public detail::integer_traits_base<int, INT_MIN, INT_MAX>
138{ };
139
140template<>
141class integer_traits<unsigned int>
142  : public std::numeric_limits<unsigned int>,
143    public detail::integer_traits_base<unsigned int, 0, UINT_MAX>
144{ };
145
146template<>
147class integer_traits<long>
148  : public std::numeric_limits<long>,
149    public detail::integer_traits_base<long, LONG_MIN, LONG_MAX>
150{ };
151
152template<>
153class 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
161template<>
162class 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
167template<>
168class 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
175template<>
176class 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>{ };
177template<>
178class 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
185template<>
186class 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
191template<>
192class 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
199template<>
200class 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
205template<>
206class 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//
216template<>
217class 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
222template<>
223class 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
Note: See TracBrowser for help on using the repository browser.