williamr@2
|
1 |
// (C) Copyright David Abrahams 2001, Howard Hinnant 2001.
|
williamr@2
|
2 |
//
|
williamr@2
|
3 |
// Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
4 |
// accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
5 |
// http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
6 |
//
|
williamr@2
|
7 |
// Template class numeric_traits<Number> --
|
williamr@2
|
8 |
//
|
williamr@2
|
9 |
// Supplies:
|
williamr@2
|
10 |
//
|
williamr@2
|
11 |
// typedef difference_type -- a type used to represent the difference
|
williamr@2
|
12 |
// between any two values of Number.
|
williamr@2
|
13 |
//
|
williamr@2
|
14 |
// Support:
|
williamr@2
|
15 |
// 1. Not all specializations are supplied
|
williamr@2
|
16 |
//
|
williamr@2
|
17 |
// 2. Use of specializations that are not supplied will cause a
|
williamr@2
|
18 |
// compile-time error
|
williamr@2
|
19 |
//
|
williamr@2
|
20 |
// 3. Users are free to specialize numeric_traits for any type.
|
williamr@2
|
21 |
//
|
williamr@2
|
22 |
// 4. Right now, specializations are only supplied for integer types.
|
williamr@2
|
23 |
//
|
williamr@2
|
24 |
// 5. On implementations which do not supply compile-time constants in
|
williamr@2
|
25 |
// std::numeric_limits<>, only specializations for built-in integer types
|
williamr@2
|
26 |
// are supplied.
|
williamr@2
|
27 |
//
|
williamr@2
|
28 |
// 6. Handling of numbers whose range of representation is at least as
|
williamr@2
|
29 |
// great as boost::intmax_t can cause some differences to be
|
williamr@2
|
30 |
// unrepresentable in difference_type:
|
williamr@2
|
31 |
//
|
williamr@2
|
32 |
// Number difference_type
|
williamr@2
|
33 |
// ------ ---------------
|
williamr@2
|
34 |
// signed Number
|
williamr@2
|
35 |
// unsigned intmax_t
|
williamr@2
|
36 |
//
|
williamr@2
|
37 |
// template <class Number> typename numeric_traits<Number>::difference_type
|
williamr@2
|
38 |
// numeric_distance(Number x, Number y)
|
williamr@2
|
39 |
// computes (y - x), attempting to avoid overflows.
|
williamr@2
|
40 |
//
|
williamr@2
|
41 |
|
williamr@2
|
42 |
// See http://www.boost.org for most recent version including documentation.
|
williamr@2
|
43 |
|
williamr@2
|
44 |
// Revision History
|
williamr@2
|
45 |
// 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
|
williamr@2
|
46 |
// 11 Feb 2001 - Rolled back ineffective Borland-specific code
|
williamr@2
|
47 |
// (David Abrahams)
|
williamr@2
|
48 |
// 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
|
williamr@2
|
49 |
// not seeing any improvement yet (David Abrahams)
|
williamr@2
|
50 |
// 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
|
williamr@2
|
51 |
// (David Abrahams)
|
williamr@2
|
52 |
// 23 Jan 2001 - Fixed logic of difference_type selection, which was
|
williamr@2
|
53 |
// completely wack. In the process, added digit_traits<>
|
williamr@2
|
54 |
// to compute the number of digits in intmax_t even when
|
williamr@2
|
55 |
// not supplied by numeric_limits<>. (David Abrahams)
|
williamr@2
|
56 |
// 21 Jan 2001 - Created (David Abrahams)
|
williamr@2
|
57 |
|
williamr@2
|
58 |
#ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
|
williamr@2
|
59 |
# define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
|
williamr@2
|
60 |
|
williamr@2
|
61 |
# include <boost/config.hpp>
|
williamr@2
|
62 |
# include <boost/cstdint.hpp>
|
williamr@2
|
63 |
# include <boost/static_assert.hpp>
|
williamr@2
|
64 |
# include <boost/type_traits.hpp>
|
williamr@2
|
65 |
# include <boost/detail/select_type.hpp>
|
williamr@2
|
66 |
# include <boost/limits.hpp>
|
williamr@2
|
67 |
|
williamr@2
|
68 |
namespace boost { namespace detail {
|
williamr@2
|
69 |
|
williamr@2
|
70 |
// Template class is_signed -- determine whether a numeric type is signed
|
williamr@2
|
71 |
// Requires that T is constructable from the literals -1 and 0. Compile-time
|
williamr@2
|
72 |
// error results if that requirement is not met (and thus signedness is not
|
williamr@2
|
73 |
// likely to have meaning for that type).
|
williamr@2
|
74 |
template <class Number>
|
williamr@2
|
75 |
struct is_signed
|
williamr@2
|
76 |
{
|
williamr@2
|
77 |
#if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_MSVC) && BOOST_MSVC <= 1300
|
williamr@2
|
78 |
BOOST_STATIC_CONSTANT(bool, value = (Number(-1) < Number(0)));
|
williamr@2
|
79 |
#else
|
williamr@2
|
80 |
BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<Number>::is_signed);
|
williamr@2
|
81 |
#endif
|
williamr@2
|
82 |
};
|
williamr@2
|
83 |
|
williamr@2
|
84 |
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
williamr@2
|
85 |
// digit_traits - compute the number of digits in a built-in integer
|
williamr@2
|
86 |
// type. Needed for implementations on which numeric_limits is not specialized
|
williamr@2
|
87 |
// for intmax_t (e.g. VC6).
|
williamr@2
|
88 |
template <bool is_specialized> struct digit_traits_select;
|
williamr@2
|
89 |
|
williamr@2
|
90 |
// numeric_limits is specialized; just select that version of digits
|
williamr@2
|
91 |
template <> struct digit_traits_select<true>
|
williamr@2
|
92 |
{
|
williamr@2
|
93 |
template <class T> struct traits
|
williamr@2
|
94 |
{
|
williamr@2
|
95 |
BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
|
williamr@2
|
96 |
};
|
williamr@2
|
97 |
};
|
williamr@2
|
98 |
|
williamr@2
|
99 |
// numeric_limits is not specialized; compute digits from sizeof(T)
|
williamr@2
|
100 |
template <> struct digit_traits_select<false>
|
williamr@2
|
101 |
{
|
williamr@2
|
102 |
template <class T> struct traits
|
williamr@2
|
103 |
{
|
williamr@2
|
104 |
BOOST_STATIC_CONSTANT(int, digits = (
|
williamr@2
|
105 |
sizeof(T) * std::numeric_limits<unsigned char>::digits
|
williamr@2
|
106 |
- (is_signed<T>::value ? 1 : 0))
|
williamr@2
|
107 |
);
|
williamr@2
|
108 |
};
|
williamr@2
|
109 |
};
|
williamr@2
|
110 |
|
williamr@2
|
111 |
// here's the "usable" template
|
williamr@2
|
112 |
template <class T> struct digit_traits
|
williamr@2
|
113 |
{
|
williamr@2
|
114 |
typedef digit_traits_select<
|
williamr@2
|
115 |
::std::numeric_limits<T>::is_specialized> selector;
|
williamr@2
|
116 |
typedef typename selector::template traits<T> traits;
|
williamr@2
|
117 |
BOOST_STATIC_CONSTANT(int, digits = traits::digits);
|
williamr@2
|
118 |
};
|
williamr@2
|
119 |
#endif
|
williamr@2
|
120 |
|
williamr@2
|
121 |
// Template class integer_traits<Integer> -- traits of various integer types
|
williamr@2
|
122 |
// This should probably be rolled into boost::integer_traits one day, but I
|
williamr@2
|
123 |
// need it to work without <limits>
|
williamr@2
|
124 |
template <class Integer>
|
williamr@2
|
125 |
struct integer_traits
|
williamr@2
|
126 |
{
|
williamr@2
|
127 |
# ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
williamr@2
|
128 |
private:
|
williamr@2
|
129 |
typedef Integer integer_type;
|
williamr@2
|
130 |
typedef std::numeric_limits<integer_type> x;
|
williamr@2
|
131 |
# if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
|
williamr@2
|
132 |
// for some reason, MSVC asserts when it shouldn't unless we make these
|
williamr@2
|
133 |
// local definitions
|
williamr@2
|
134 |
BOOST_STATIC_CONSTANT(bool, is_integer = x::is_integer);
|
williamr@2
|
135 |
BOOST_STATIC_CONSTANT(bool, is_specialized = x::is_specialized);
|
williamr@2
|
136 |
|
williamr@2
|
137 |
BOOST_STATIC_ASSERT(is_integer);
|
williamr@2
|
138 |
BOOST_STATIC_ASSERT(is_specialized);
|
williamr@2
|
139 |
# endif
|
williamr@2
|
140 |
public:
|
williamr@2
|
141 |
typedef typename
|
williamr@2
|
142 |
if_true<(int(x::is_signed)
|
williamr@2
|
143 |
&& (!int(x::is_bounded)
|
williamr@2
|
144 |
// digits is the number of no-sign bits
|
williamr@2
|
145 |
|| (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits)))>::template then<
|
williamr@2
|
146 |
Integer,
|
williamr@2
|
147 |
|
williamr@2
|
148 |
typename if_true<(int(x::digits) + 1 < digit_traits<signed int>::digits)>::template then<
|
williamr@2
|
149 |
signed int,
|
williamr@2
|
150 |
|
williamr@2
|
151 |
typename if_true<(int(x::digits) + 1 < digit_traits<signed long>::digits)>::template then<
|
williamr@2
|
152 |
signed long,
|
williamr@2
|
153 |
|
williamr@2
|
154 |
// else
|
williamr@2
|
155 |
intmax_t
|
williamr@2
|
156 |
>::type>::type>::type difference_type;
|
williamr@2
|
157 |
#else
|
williamr@2
|
158 |
BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
|
williamr@2
|
159 |
|
williamr@2
|
160 |
typedef typename
|
williamr@2
|
161 |
if_true<(sizeof(Integer) >= sizeof(intmax_t))>::template then<
|
williamr@2
|
162 |
|
williamr@2
|
163 |
typename if_true<(is_signed<Integer>::value)>::template then<
|
williamr@2
|
164 |
Integer,
|
williamr@2
|
165 |
intmax_t
|
williamr@2
|
166 |
>::type,
|
williamr@2
|
167 |
|
williamr@2
|
168 |
typename if_true<(sizeof(Integer) < sizeof(std::ptrdiff_t))>::template then<
|
williamr@2
|
169 |
std::ptrdiff_t,
|
williamr@2
|
170 |
intmax_t
|
williamr@2
|
171 |
>::type
|
williamr@2
|
172 |
>::type difference_type;
|
williamr@2
|
173 |
# endif
|
williamr@2
|
174 |
};
|
williamr@2
|
175 |
|
williamr@2
|
176 |
// Right now, only supports integers, but should be expanded.
|
williamr@2
|
177 |
template <class Number>
|
williamr@2
|
178 |
struct numeric_traits
|
williamr@2
|
179 |
{
|
williamr@2
|
180 |
typedef typename integer_traits<Number>::difference_type difference_type;
|
williamr@2
|
181 |
};
|
williamr@2
|
182 |
|
williamr@2
|
183 |
template <class Number>
|
williamr@2
|
184 |
typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
|
williamr@2
|
185 |
{
|
williamr@2
|
186 |
typedef typename numeric_traits<Number>::difference_type difference_type;
|
williamr@2
|
187 |
return difference_type(y) - difference_type(x);
|
williamr@2
|
188 |
}
|
williamr@2
|
189 |
}}
|
williamr@2
|
190 |
|
williamr@2
|
191 |
#endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901
|