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