4 Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
11 [section Numeric Converter Policy Classes]
14 [section enum range_check_result]
16 namespace boost { namespace numeric {
18 enum range_check_result
27 Defines the values returned by `boost::numeric::converter<>::out_of_range()`
31 [section Policy OverflowHandler]
33 This ['stateless] non-template policy class must be a ['function object] and is
34 called to administrate the result of the range checking. It can throw an exception
35 if overflow has been detected by the range checking as indicated by its argument.
36 If it throws, is is recommended that it be `std::bad_cast` or derived.
38 It must have the following interface (it does not has to be a template class):
40 struct YourOverflowHandlerPolicy
42 void operator() ( boost::range_check_result ) ; // throw bad_cast or derived
45 It is called with the result of the converter's `out_of_range()` inside `validate_range()`.
47 These are the two overflow handler classes provided by the library:
49 namespace boost { namespace numeric {
51 struct def_overflow_handler
53 void operator() ( range_check_result r ) // throw bad_numeric_conversion derived
55 if ( r == cNegOverflow )
56 throw negative_overflow() ;
57 else if ( r == cPosOverflow )
58 throw positive_overflow() ;
62 struct silent_overflow_handler
64 void operator() ( range_check_result ) // no-throw
70 And these are the Exception Classes thrown by the default overflow handler
71 [link numeric_conversion_policy_overflow_handler_important_note (see IMPORTANT note)]
73 namespace boost { namespace numeric {
75 ``[#numeric_conversion_bad_numeric_cast]``
76 class bad_numeric_cast : public std::bad_cast
79 virtual const char *what() const // throw()
81 return "bad numeric conversion: overflow";
86 ``[#numeric_conversion_negative_overflow]``
87 class negative_overflow : public bad_numeric_cast
90 virtual const char *what() const // throw()
92 return "bad numeric conversion: negative overflow";
96 ``[#numeric_conversion_possitive_overflow]``
97 class positive_overflow : public bad_numeric_cast
100 virtual const char *what() const // throw()
102 return "bad numeric conversion: positive overflow";
109 [#numeric_conversion_policy_overflow_handler_important_note]
111 [important [*RELEASE NOTE for 1.33]
112 Previous to boost version 1.33, the exception class `bad_numeric_cast` was
113 named `bad_numeric_conversion`. However, in 1.33, the old function
114 `numeric_cast<>` from `boost/cast.hpp` was completly replaced by the
115 new `numeric_cast<>` in `boost/numeric/conversion/cast.hpp`
116 (and `boost/cast.hpp` is including `boost/numeric/conversion/cast.hpp` now).
117 That old function which existed in boost for quite some time used the
118 `bad_numeric_cast` as its exception type so I decided to avoid backward
119 compatibility problems by adopting it (guessing that the user base for
120 the old code is wider than for the new code).
125 [section Policy Float2IntRounder]
127 This ['stateless] template policy class specifies the rounding mode used
128 for [_float to integral] conversions. It supplies the `nearbyint()`
129 static member function exposed by the converter, which means that it
130 [_publicly inherits from this policy].
132 The policy must have the following interface:
135 struct YourFloat2IntRounderPolicy
137 typedef S source_type ;
138 typedef {S or S const&} argument_type ;
140 static source_type nearbyint ( argument_type s ) { ... }
142 typedef mpl::integral_c<std::float_round_style,std::round_...> round_style ;
146 These are the rounder classes provided by the library (only the specific parts are shown,
147 see the general policy form above)
150 These classes are not intended to be general purpose rounding functions
151 but specific policies for `converter<>`. This is why they are not function objects.
154 namespace boost { namespace numeric {
160 static source_type nearbyint ( argument_type s )
165 return s >= static_cast<S>(0) ? floor(s) : ceil(s) ;
168 typedef mpl::integral_c<std::float_round_style,std::round_toward_zero> round_style ;
175 static source_type nearbyint ( argument_type s )
177 return impl-defined-value ;
180 typedef mpl::integral_c<std::float_round_style,std::round_to_nearest> round_style ;
187 static source_type nearbyint ( argument_type s )
193 typedef mpl::integral_c<std::float_round_style,std::round_toward_infinity> round_style ;
200 static source_type nearbyint ( argument_type s )
205 typedef mpl::integral_c<std::float_round_style,std::round_toward_neg_infinity> round_style ;
208 } } // namespace numeric, namespace boost
210 [heading Math Functions used by the rounder policies]
212 The rounder policies supplied by this header use math functions `floor()` and `ceil()`.
213 The standard versions of these functions are introduced in context by a using directive,
214 so in normal conditions, the standard functions will be used.
216 However, if there are other visible corresponding overloads an ambiguity could arise.
217 In this case, the user can supply her own rounder policy which could, for instance,
218 use a fully qualified call.
220 This technique allows the default rounder policies to be used directly with
221 user defined types. The user only requires that suitable overloads of `floor()` and `ceil()`
222 be visible. See also [link boost_numericconversion.type_requirements_and_user_defined_types_support User Defined Numeric Types]
227 [section Policy RawConverter]
229 This ['stateless] template policy class is used to perform the
230 actual conversion from Source to Target. It supplies the
231 `low_level_convert()` static member function exposed by the
232 converter, which means that it publicly inherits from this policy.
234 The policy must have the following interface:
236 template<class Traits>
237 struct YourRawConverterPolicy
239 typedef typename Traits::result_type result_type ;
240 typedef typename Traits::argument_type argument_type ;
242 static result_type low_level_convert ( argument_type s ) { return <impl defined> ; }
246 This policy is mostly provided as a hook for user defined types which don't support `static_cast<>` conversions to some types
248 This is the only raw converter policy class provided by the library:
250 namespace boost { namespace numeric {
252 template<class Traits>
253 struct raw_numeric_converter
255 typedef typename Traits::result_type result_type ;
256 typedef typename Traits::argument_type argument_type ;
258 static result_type low_level_convert ( argument_type s )
260 return static_cast<result_type>(s) ;
268 [section Policy UserRangeChecker]
270 This ['stateless] template policy class is used [_only if supplied]
271 to [*override] the internal range checking logic.
273 It supplies the `validate_range()` static member function
274 exposed by the converter, which means that it publicly inherits
277 The policy must have the following interface:
279 template<class Traits>
280 struct YourRangeCheckerPolicy
282 typedef typename Traits::argument_type argument_type ;
284 // Determines if the value 's' fits in the range of the Target type.
285 static range_check_result out_of_range ( argument_type s ) ;
287 // Checks whether the value 's' is out_of_range()
288 // and passes the result of the check to the OverflowHandler policy.
289 static void validate_range ( argument_type s )
291 OverflowHandler()( out_of_range(s) ) ;
296 This policy is [*only] provided as a hook for user defined types which require
297 range checking (which is disabled by default when a UDT is involved).
298 The library provides a class: `UseInternalRangeChecker{}`; which is a ['fake]
299 `RangeChecker` policy used to signal the converter to use its internal
300 range checking implementation.