converter_policies.qbk 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. [/
  2. Boost.Optional
  3. Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [section Numeric Converter Policy Classes]
  9. [section enum range_check_result]
  10. namespace boost { namespace numeric {
  11. enum range_check_result
  12. {
  13. cInRange ,
  14. cNegOverflow ,
  15. cPosOverflow
  16. } ;
  17. } }
  18. Defines the values returned by `boost::numeric::converter<>::out_of_range()`
  19. [endsect]
  20. [section Policy OverflowHandler]
  21. This ['stateless] non-template policy class must be a ['function object] and is
  22. called to administrate the result of the range checking. It can throw an exception
  23. if overflow has been detected by the range checking as indicated by its argument.
  24. If it throws, is is recommended that it be `std::bad_cast` or derived.
  25. It must have the following interface (it does not has to be a template class):
  26. struct YourOverflowHandlerPolicy
  27. {
  28. void operator() ( boost::range_check_result ) ; // throw bad_cast or derived
  29. } ;
  30. It is called with the result of the converter's `out_of_range()` inside `validate_range()`.
  31. These are the two overflow handler classes provided by the library:
  32. namespace boost { namespace numeric {
  33. struct def_overflow_handler
  34. {
  35. void operator() ( range_check_result r ) // throw bad_numeric_conversion derived
  36. {
  37. if ( r == cNegOverflow )
  38. throw negative_overflow() ;
  39. else if ( r == cPosOverflow )
  40. throw positive_overflow() ;
  41. }
  42. } ;
  43. struct silent_overflow_handler
  44. {
  45. void operator() ( range_check_result ) // no-throw
  46. {}
  47. } ;
  48. } }
  49. And these are the Exception Classes thrown by the default overflow handler
  50. [link numeric_conversion_policy_overflow_handler_important_note (see IMPORTANT note)]
  51. namespace boost { namespace numeric {
  52. ``[#numeric_conversion_bad_numeric_cast]``
  53. class bad_numeric_cast : public std::bad_cast
  54. {
  55. public:
  56. virtual const char *what() const // throw()
  57. {
  58. return "bad numeric conversion: overflow";
  59. }
  60. };
  61. ``[#numeric_conversion_negative_overflow]``
  62. class negative_overflow : public bad_numeric_cast
  63. {
  64. public:
  65. virtual const char *what() const // throw()
  66. {
  67. return "bad numeric conversion: negative overflow";
  68. }
  69. };
  70. ``[#numeric_conversion_possitive_overflow]``
  71. class positive_overflow : public bad_numeric_cast
  72. {
  73. public:
  74. virtual const char *what() const // throw()
  75. {
  76. return "bad numeric conversion: positive overflow";
  77. }
  78. };
  79. } }
  80. [#numeric_conversion_policy_overflow_handler_important_note]
  81. [important [*RELEASE NOTE for 1.33]
  82. Previous to boost version 1.33, the exception class `bad_numeric_cast` was
  83. named `bad_numeric_conversion`. However, in 1.33, the old function
  84. `numeric_cast<>` from `boost/cast.hpp` was completly replaced by the
  85. new `numeric_cast<>` in `boost/numeric/conversion/cast.hpp`
  86. (and `boost/cast.hpp` is including `boost/numeric/conversion/cast.hpp` now).
  87. That old function which existed in boost for quite some time used the
  88. `bad_numeric_cast` as its exception type so I decided to avoid backward
  89. compatibility problems by adopting it (guessing that the user base for
  90. the old code is wider than for the new code).
  91. ]
  92. [endsect]
  93. [section Policy Float2IntRounder]
  94. This ['stateless] template policy class specifies the rounding mode used
  95. for [_float to integral] conversions. It supplies the `nearbyint()`
  96. static member function exposed by the converter, which means that it
  97. [_publicly inherits from this policy].
  98. The policy must have the following interface:
  99. template<class S>
  100. struct YourFloat2IntRounderPolicy
  101. {
  102. typedef S source_type ;
  103. typedef {S or S const&} argument_type ;
  104. static source_type nearbyint ( argument_type s ) { ... }
  105. typedef mpl::integral_c<std::float_round_style,std::round_...> round_style ;
  106. } ;
  107. These are the rounder classes provided by the library (only the specific parts are shown,
  108. see the general policy form above)
  109. [note
  110. These classes are not intended to be general purpose rounding functions
  111. but specific policies for `converter<>`. This is why they are not function objects.
  112. ]
  113. namespace boost { namespace numeric {
  114. template<class S>
  115. struct Trunc
  116. {
  117. static source_type nearbyint ( argument_type s )
  118. {
  119. using std::floor ;
  120. using std::ceil ;
  121. return s >= static_cast<S>(0) ? floor(s) : ceil(s) ;
  122. }
  123. typedef mpl::integral_c<std::float_round_style,std::round_toward_zero> round_style ;
  124. } ;
  125. template<class S>
  126. struct RoundEven
  127. {
  128. static source_type nearbyint ( argument_type s )
  129. {
  130. return impl-defined-value ;
  131. }
  132. typedef mpl::integral_c<std::float_round_style,std::round_to_nearest> round_style ;
  133. } ;
  134. template<class S>
  135. struct Ceil
  136. {
  137. static source_type nearbyint ( argument_type s )
  138. {
  139. using std::ceil ;
  140. return ceil(s) ;
  141. }
  142. typedef mpl::integral_c<std::float_round_style,std::round_toward_infinity> round_style ;
  143. } ;
  144. template<class S>
  145. struct Floor
  146. {
  147. static source_type nearbyint ( argument_type s )
  148. {
  149. using std::floor ;
  150. return floor(s) ;
  151. }
  152. typedef mpl::integral_c<std::float_round_style,std::round_toward_neg_infinity> round_style ;
  153. } ;
  154. } } // namespace numeric, namespace boost
  155. [heading Math Functions used by the rounder policies]
  156. The rounder policies supplied by this header use math functions `floor()` and `ceil()`.
  157. The standard versions of these functions are introduced in context by a using directive,
  158. so in normal conditions, the standard functions will be used.
  159. However, if there are other visible corresponding overloads an ambiguity could arise.
  160. In this case, the user can supply her own rounder policy which could, for instance,
  161. use a fully qualified call.
  162. This technique allows the default rounder policies to be used directly with
  163. user defined types. The user only requires that suitable overloads of `floor()` and `ceil()`
  164. be visible. See also [link boost_numericconversion.type_requirements_and_user_defined_types_support User Defined Numeric Types]
  165. support.
  166. [endsect]
  167. [section Policy RawConverter]
  168. This ['stateless] template policy class is used to perform the
  169. actual conversion from Source to Target. It supplies the
  170. `low_level_convert()` static member function exposed by the
  171. converter, which means that it publicly inherits from this policy.
  172. The policy must have the following interface:
  173. template<class Traits>
  174. struct YourRawConverterPolicy
  175. {
  176. typedef typename Traits::result_type result_type ;
  177. typedef typename Traits::argument_type argument_type ;
  178. static result_type low_level_convert ( argument_type s ) { return <impl defined> ; }
  179. } ;
  180. This policy is mostly provided as a hook for user defined types which don't support `static_cast<>` conversions to some types
  181. This is the only raw converter policy class provided by the library:
  182. namespace boost { namespace numeric {
  183. template<class Traits>
  184. struct raw_numeric_converter
  185. {
  186. typedef typename Traits::result_type result_type ;
  187. typedef typename Traits::argument_type argument_type ;
  188. static result_type low_level_convert ( argument_type s )
  189. {
  190. return static_cast<result_type>(s) ;
  191. }
  192. } ;
  193. } }
  194. [endsect]
  195. [section Policy UserRangeChecker]
  196. This ['stateless] template policy class is used [_only if supplied]
  197. to [*override] the internal range checking logic.
  198. It supplies the `validate_range()` static member function
  199. exposed by the converter, which means that it publicly inherits
  200. from this policy.
  201. The policy must have the following interface:
  202. template<class Traits>
  203. struct YourRangeCheckerPolicy
  204. {
  205. typedef typename Traits::argument_type argument_type ;
  206. // Determines if the value 's' fits in the range of the Target type.
  207. static range_check_result out_of_range ( argument_type s ) ;
  208. // Checks whether the value 's' is out_of_range()
  209. // and passes the result of the check to the OverflowHandler policy.
  210. static void validate_range ( argument_type s )
  211. {
  212. OverflowHandler()( out_of_range(s) ) ;
  213. }
  214. } ;
  215. This policy is [*only] provided as a hook for user defined types which require
  216. range checking (which is disabled by default when a UDT is involved).
  217. The library provides a class: `UseInternalRangeChecker{}`; which is a ['fake]
  218. `RangeChecker` policy used to signal the converter to use its internal
  219. range checking implementation.
  220. [endsect]
  221. [endsect]