converter.qbk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 converter<> function object]
  9. [section Synopsis]
  10. namespace boost { namespace numeric {
  11. template<class T,
  12. class S,
  13. class Traits, = conversion_traits<T,S>
  14. class OverflowHandler = def_overflow_handler,
  15. class Float2IntRounder = Trunc< typename Traits::source_type >,
  16. class RawConverter = raw_converter<Traits>,
  17. class UserRangeChecker = UseInternalRangeChecker
  18. >
  19. struct converter
  20. {
  21. typedef Traits traits ;
  22. typedef typename Traits::source_type source_type ;
  23. typedef typename Traits::argument_type argument_type ;
  24. typedef typename Traits::result_type result_type ;
  25. static result_type convert ( argument_type s ) ;
  26. result_type operator() ( argument_type s ) const ;
  27. // Internal member functions:
  28. static range_check_result out_of_range ( argument_type s ) ;
  29. static void validate_range ( argument_type s ) ;
  30. static result_type low_level_convert ( argument_type s ) ;
  31. static source_type nearbyint ( argument_type s ) ;
  32. } ;
  33. } } // namespace numeric, boost
  34. `boost::numeric::converter<>` is a __SGI_UNARY_FUNCTION__ encapsulating
  35. the code to perform a numeric conversion with the direction and
  36. properties specified by the Traits template parameter. It can optionally
  37. take some [link boost_numericconversion.numeric_converter_policy_classes policies] which can be used to customize its behavior. The
  38. `Traits` parameter is not a policy but the parameter that defines
  39. the conversion.
  40. [endsect]
  41. [section Template parameters]
  42. [table
  43. [[ ][ ]]
  44. [[`T`][
  45. The [link boost_numericconversion.definitions.numeric_types Numeric Type]
  46. which is the ['Target] of the conversion.
  47. ]]
  48. [[`S`][
  49. The [link boost_numericconversion.definitions.numeric_types Numeric Type]
  50. which is the ['Source] of the conversion.
  51. ]]
  52. [[`Traits`][
  53. This must be a conversion traits class with the interface of
  54. [link boost_numericconversion.conversion_traits___traits_class `boost::numeric::conversion_traits`]
  55. ]]
  56. [[`OverflowHandler`][
  57. [*Stateless Policy] called to administrate the result of the range checking.
  58. It is a [*Function Object] which receives the result of `out_of_range()`
  59. and is called inside the `validate_range()` static member function exposed
  60. by the converter.
  61. ]]
  62. [[`Float2IntRounder`][
  63. [*Stateless Policy] which specifies the rounding mode used for float to
  64. integral conversions.
  65. It supplies the `nearbyint()` static member function exposed by the converter.
  66. ]]
  67. [[`RawConverter`][
  68. [*Stateless Policy] which is used to perform the actual conversion.
  69. It supplies the `low_level_convert()` static member function exposed
  70. by the converter.
  71. ]]
  72. [[`UserRangeChecker`][
  73. ['Special and Optional] [*Stateless Policy] which can be used to override
  74. the internal range checking logic.
  75. If given, supplies alternative code for the `out_of_range()` and
  76. `validate_range()` static member functions exposed by the converter.
  77. ]]
  78. ]
  79. [endsect]
  80. [section Member functions]
  81. [: `static result_type converter<>::convert ( argument_type s ) ; // throw
  82. `]
  83. This static member function converts an rvalue of type `source_type` to
  84. an rvalue of type `target_type`.
  85. If the conversion requires it, it performs a range checking before the conversion
  86. and passes the result of the check to the overflow handler policy (the default
  87. policy throws an exception if out-of-range is detected)
  88. The implementation of this function is actually built from the policies and is
  89. basically as follows:
  90. result_type converter<>::convert ( argument_type s )
  91. {
  92. validate_range(s); // Implemented by the internal range checking logic
  93. // (which also calls the OverflowHandler policy)
  94. // or externally supplied by the UserRangeChecker policy.
  95. s = nearbyint(s); // Externally supplied by the Float2IntRounder policy.
  96. // NOTE: This is actually called only for float to int conversions.
  97. return low_level_convert(s); // Externally supplied by the RawConverter policy.
  98. }
  99. `converter<>::operator() const` just calls `convert()`
  100. __SPACE__
  101. [: `static range_check_result numeric_converter<>::out_of_range ( argument_type s ) ;`]
  102. This [link numeric_conversion_converter_internal internal] static member function
  103. determines if the value `s` can be
  104. represented by the target type without overflow.
  105. It does not determine if the conversion is ['exact]; that is, it does not detect
  106. ['inexact] conversions, only ['out-of-range] conversions (see the
  107. [link boost_numericconversion.definitions.exact__correctly_rounded_and_out_of_range_representations Definitions] for further details).
  108. The return value is of enum type
  109. [link boost_numericconversion.numeric_converter_policy_classes.enum_range_check_result `boost::numeric::range_check_result`]
  110. The actual code for the range checking logic is optimized for the combined
  111. properties of the source and target types. For example, a non-subranged
  112. conversion (i.e: `int`->`float`), requires no range checking, so `out_of_range()`
  113. returns `cInRange` directly. See the following
  114. [link boost_numericconversion.converter___function_object.range_checking_logic table] for more details.
  115. If the user supplied a
  116. [link boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker UserRangeChecker] policy,
  117. is this policy which implements this function, so the implementation is user
  118. defined, although it is expected to perform the same conceptual check and
  119. return the appropriate result.
  120. __SPACE__
  121. [: `static void numeric_converter<>::validate_range ( argument_type s ) ; // no throw
  122. `]
  123. This [link numeric_conversion_converter_internal internal] static member function
  124. calls out_of_range(s), and passes the
  125. result to the [link boost_numericconversion.numeric_converter_policy_classes.policy_overflowhandler OverflowHandler]
  126. policy class.
  127. For those Target/Source combinations which don't require range checking, this
  128. is an empty inline function.
  129. If the user supplied a
  130. [link boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker UserRangeChecker] policy,
  131. is this policy which implements this function, so the implementation is user
  132. defined, although it is expected to perform the same action as the default.
  133. In particular, it is expected to pass the result of the check to the overflow handler.
  134. __SPACE__
  135. [: `static result_type numeric_converter<>::low_level_convert ( argument_type s ) ;` ]
  136. This [link numeric_conversion_converter_internal internal] static member function
  137. performs the actual conversion.
  138. This function is externally supplied by the
  139. [link boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter RawConverter] policy class.
  140. __SPACE__
  141. [: `static source_type converter<>::nearbyint ( argument_type s ) ;`]
  142. This [link numeric_conversion_converter_internal internal] static member function,
  143. which is [_only used] for
  144. `float` to `int` conversions, returns an ['integer] value of ['[_floating-point
  145. type]] according to some rounding direction.
  146. This function is externally supplied by the
  147. [link boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder Float2IntRounder] policy class
  148. which encapsulates the specific rounding mode.
  149. __SPACE__
  150. [#numeric_conversion_converter_internal]
  151. [heading Internal Member Functions]
  152. These static member functions build the actual conversion code used by `convert()`.
  153. The user does not have to call these if calling `convert()`, since `convert()` calls
  154. them infernally, but they can be called separately for specific needs.
  155. [endsect]
  156. [section Range Checking Logic]
  157. The following table summarizes the internal range checking logic performed for
  158. each combination of the properties of Source and Target.
  159. LowestT/HighestT denotes the highest and lowest values of the Target type, respectively.
  160. `S(n)` is short for `static_cast<S>(n)` (`S` denotes the Source type).
  161. `NONE` indicates that for this case there is no range checking.
  162. [pre
  163. [^
  164. int_to_int |--> sig_to_sig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
  165. | |--> not subranged |--> NONE
  166. |
  167. |--> unsig_to_unsig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
  168. | |--> not subranged |--> NONE
  169. |
  170. |--> sig_to_unsig |--> pos subranged |--> ( s >= S(0) ) && ( s <= S(HighestT) )
  171. | |--> not pos subranged |--> ( s >= S(0) )
  172. |
  173. |--> unsig_to_sig |--> subranged |--> ( s <= S(HighestT) )
  174. | |--> not subranged |--> NONE
  175. ]
  176. [^
  177. int_to_float |--> NONE
  178. ]
  179. [^
  180. float_to_int |--> round_to_zero |--> ( s > S(LowestT)-S(1) ) && ( s < S(HighestT)+S(1) )
  181. |--> round_to_even_nearest |--> ( s >= S(LowestT)-S(0.5) ) && ( s < S(HighestT)+S(0.5) )
  182. |--> round_to_infinity |--> ( s > S(LowestT)-S(1) ) && ( s <= S(HighestT) )
  183. |--> round_to_neg_infinity |--> ( s >= S(LowestT) ) && ( s < S(HighestT)+S(1) )
  184. ]
  185. [^
  186. float_to_float |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
  187. |--> not subranged |--> NONE
  188. ]
  189. ]
  190. [endsect]
  191. [section Examples]
  192. #include <cassert>
  193. #include <boost/numeric/conversion/converter.hpp>
  194. int main() {
  195. typedef boost::numeric::converter<int,double> Double2Int ;
  196. int x = Double2Int::convert(2.0);
  197. assert ( x == 2 );
  198. int y = Double2Int()(3.14); // As a function object.
  199. assert ( y == 3 ) ; // The default rounding is trunc.
  200. try
  201. {
  202. double m = boost::numeric::bounds<double>::highest();
  203. int z = Double2Int::convert(m); // By default throws positive_overflow()
  204. }
  205. catch ( boost::numeric::positive_overflow const& )
  206. {
  207. }
  208. return 0;
  209. }
  210. [endsect]
  211. [endsect]