requirements.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 Type Requirements and User-defined-types support]
  9. [section Type Requirements]
  10. Both arithmetic (built-in) and user-defined numeric types require proper
  11. specialization of `std::numeric_limits<>` (that is, with (in-class) integral
  12. constants).
  13. The library uses `std::numeric_limits<T>::is_specialized` to detect whether
  14. the type is builtin or user defined, and `std::numeric_limits<T>::is_integer`,
  15. `std::numeric_limits<T>::is_signed` to detect whether the type is integer
  16. or floating point; and whether it is signed/unsigned.
  17. The default `Float2IntRounder` policies uses unqualified calls to functions
  18. `floor()` and `ceil()`; but the standard functions are introduced in scope
  19. by a using directive:
  20. using std::floor ; return floor(s);
  21. Therefore, for builtin arithmetic types, the std functions will be used.
  22. User defined types should provide overloaded versions of these functions in
  23. order to use the default rounder policies. If these overloads are defined
  24. within a user namespace argument dependent lookup (ADL) should find them,
  25. but if your compiler has a weak ADL you might need to put these functions
  26. some place else or write your own rounder policy.
  27. The default `Trunc<>` rounder policy needs to determine if the source value
  28. is positive or not, and for this it evaluates the expression
  29. `s < static_cast<S>(0)`. Therefore, user defined types require a visible
  30. `operator<` in order to use the `Trunc<>` policy (the default).
  31. [endsect]
  32. [section UDT's special semantics]
  33. [heading Conversion Traits]
  34. If a User Defined Type is involved in a conversion, it is ['assumed] that
  35. the UDT has [link boost_numericconversion.definitions.range_and_precision wider range]
  36. than any built-in type, and consequently the values
  37. of some `converter_traits<>` members are hardwired regardless of the reality.
  38. The following table summarizes this:
  39. * `Target=`['UDT] and `Source=`['built-in]
  40. * `subranged=false`
  41. * `supertype=Target`
  42. * `subtype=Source`
  43. * `Target=`['built-in] and `Source=`['UDT]
  44. * `subranged=true`
  45. * `supertype=Source`
  46. * `subtype=Target`
  47. * `Target=`['UDT] and `Source=`['UDT]
  48. * `subranged=false`
  49. * `supertype=Target`
  50. * `subtype=Source`
  51. The `Traits` member `udt_mixture` can be used to detect whether a UDT is involved
  52. and to infer the validity of the other members as shown above.
  53. [heading Range Checking]
  54. Because User Defined Numeric Types might have peculiar ranges (such as an
  55. unbounded range), this library does not attempt to supply a meaningful range
  56. checking logic when UDTs are involved in a conversion. Therefore, if either
  57. Target or Source are not built-in types, the bundled range checking of the
  58. `converter<>` function object is automatically disabled. However, it is possible
  59. to supply a user-defined range-checker. See
  60. [link boost_numericconversion.type_requirements_and_user_defined_types_support.special_policies Special Policies]
  61. [endsect]
  62. [section Special Policies]
  63. There are two components of the `converter<>` class that might require special
  64. behavior if User Defined Numeric Types are involved: the Range Checking and the
  65. Raw Conversion.
  66. When both Target and Source are built-in types, the converter class uses an internal
  67. range checking logic which is optimized and customized for the combined properties
  68. of the types.
  69. However, this internal logic is disabled when either type is User Defined.
  70. In this case, the user can specify an ['external] range checking policy which will be
  71. used in place of the internal code. See
  72. [link boost_numericconversion.type_requirements_and_user_defined_types_support.udts_with_numeric_cast numeric_cast_traits]
  73. for details on using UDTs with `numeric_cast`.
  74. The converter class performs the actual conversion using a Raw Converter policy.
  75. The default raw converter simply performs a `static_cast<Target>(source)`.
  76. However, if the a UDT is involved, the `static_cast` might not work. In this case,
  77. the user can implement and pass a different raw converter policy.
  78. See [link boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter RawConverter] policy for details.
  79. [endsect]
  80. [section UDTs with numeric_cast]
  81. In order to employ UDTs with `numeric_cast`, the user should define
  82. a `numeric_cast_traits` specialization on the UDT for each conversion.
  83. Here is an example of specializations for converting between the UDT
  84. and any other type:
  85. namespace boost { namespace numeric {
  86. template <typename Source>
  87. struct numeric_cast_traits<UDT, Source>
  88. {
  89. typedef conversion_traits<UDT, Source> conv_traits;
  90. //! The following are required:
  91. typedef YourOverflowHandlerPolicy overflow_policy;
  92. typedef YourRangeCheckerPolicy<conv_traits> range_checking_policy;
  93. typedef YourFloat2IntRounderPolicy<Source> rounding_policy;
  94. };
  95. template <typename Target>
  96. struct numeric_cast_traits<Target, UDT>
  97. {
  98. typedef conversion_traits<Target, UDT> conv_traits;
  99. //! The following are required:
  100. typedef YourOverflowHandlerPolicy overflow_policy;
  101. typedef YourRangeCheckerPolicy<conv_traits> range_checking_policy;
  102. typedef YourFloat2IntRounderPolicy<UDT> rounding_policy;
  103. };
  104. }}//namespace boost::numeric;
  105. These specializations are already defined with default values for the built-in
  106. numeric types. It is possible to disable the generation of specializations for
  107. built-in types by defining `BOOST_NUMERIC_CONVERSION_RELAX_BUILT_IN_CAST_TRAITS`.
  108. For details on defining custom policies see [link boost_numericconversion.numeric_converter_policy_classes Converter Policies].
  109. Here is a full example of how to define a custom UDT for use with `numeric_cast`:
  110. //! Define a simple custom number
  111. struct Double
  112. : boost::ordered_field_operators
  113. <
  114. Double
  115. , boost::ordered_field_operators2< Double, long double
  116. , boost::ordered_field_operators2< Double, double
  117. , boost::ordered_field_operators2< Double, float
  118. , boost::ordered_field_operators2< Double, int
  119. , boost::ordered_field_operators2< Double, unsigned int
  120. , boost::ordered_field_operators2< Double, long
  121. , boost::ordered_field_operators2< Double, unsigned long
  122. , boost::ordered_field_operators2< Double, long long
  123. , boost::ordered_field_operators2< Double, unsigned long long
  124. , boost::ordered_field_operators2< Double, char
  125. , boost::ordered_field_operators2< Double, unsigned char
  126. , boost::ordered_field_operators2< Double, short
  127. , boost::ordered_field_operators2< Double, unsigned short
  128. > > > > > > > > > > > > > >
  129. {
  130. Double()
  131. : v(0)
  132. {}
  133. template <typename T>
  134. explicit Double( T v )
  135. : v(static_cast<double>(v))
  136. {}
  137. template <typename T>
  138. Double& operator= ( T t )
  139. {
  140. v = static_cast<double>(t);
  141. return *this;
  142. }
  143. bool operator < ( const Double& rhs ) const
  144. {
  145. return v < rhs.v;
  146. }
  147. template <typename T>
  148. bool operator < ( T rhs ) const
  149. {
  150. return v < static_cast<double>(rhs);
  151. }
  152. bool operator > ( const Double& rhs ) const
  153. {
  154. return v > rhs.v;
  155. }
  156. template <typename T>
  157. bool operator > ( T rhs ) const
  158. {
  159. return v > static_cast<double>(rhs);
  160. }
  161. bool operator ==( const Double& rhs ) const
  162. {
  163. return v == rhs.v;
  164. }
  165. template <typename T>
  166. bool operator == ( T rhs ) const
  167. {
  168. return v == static_cast<double>(rhs);
  169. }
  170. bool operator !() const
  171. {
  172. return v == 0;
  173. }
  174. Double operator -() const
  175. {
  176. return Double(-v);
  177. }
  178. Double& operator +=( const Double& t )
  179. {
  180. v += t.v;
  181. return *this;
  182. }
  183. template <typename T>
  184. Double& operator +=( T t )
  185. {
  186. v += static_cast<double>(t);
  187. return *this;
  188. }
  189. Double& operator -=( const Double& t )
  190. {
  191. v -= t.v;
  192. return *this;
  193. }
  194. template <typename T>
  195. Double& operator -=( T t )
  196. {
  197. v -= static_cast<double>(t);
  198. return *this;
  199. }
  200. Double& operator *= ( const Double& factor )
  201. {
  202. v *= factor.v;
  203. return *this;
  204. }
  205. template <typename T>
  206. Double& operator *=( T t )
  207. {
  208. v *= static_cast<double>(t);
  209. return *this;
  210. }
  211. Double& operator /= (const Double& divisor)
  212. {
  213. v /= divisor.v;
  214. return *this;
  215. }
  216. template <typename T>
  217. Double& operator /=( T t )
  218. {
  219. v /= static_cast<double>(t);
  220. return (*this);
  221. }
  222. double v;
  223. };
  224. //! Define numeric_limits for the custom type.
  225. namespace std
  226. {
  227. template<>
  228. class numeric_limits<Double> : public numeric_limits<double>
  229. {
  230. public:
  231. //! Limit our Double to a range of +/- 100.0
  232. static Double (min)()
  233. {
  234. return Double(1.e-2);
  235. }
  236. static Double (max)()
  237. {
  238. return Double(1.e2);
  239. }
  240. static Double epsilon()
  241. {
  242. return Double( std::numeric_limits<double>::epsilon() );
  243. }
  244. };
  245. }
  246. //! Define range checking and overflow policies.
  247. namespace custom
  248. {
  249. //! Define a custom range checker
  250. template<typename Traits, typename OverFlowHandler>
  251. struct range_checker
  252. {
  253. typedef typename Traits::argument_type argument_type ;
  254. typedef typename Traits::source_type S;
  255. typedef typename Traits::target_type T;
  256. //! Check range of integral types.
  257. static boost::numeric::range_check_result out_of_range( argument_type s )
  258. {
  259. using namespace boost::numeric;
  260. if( s > bounds<T>::highest() )
  261. return cPosOverflow;
  262. else if( s < bounds<T>::lowest() )
  263. return cNegOverflow;
  264. else
  265. return cInRange;
  266. }
  267. static void validate_range ( argument_type s )
  268. {
  269. BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_bounded );
  270. OverFlowHandler()( out_of_range(s) );
  271. }
  272. };
  273. //! Overflow handler
  274. struct positive_overflow{};
  275. struct negative_overflow{};
  276. struct overflow_handler
  277. {
  278. void operator() ( boost::numeric::range_check_result r )
  279. {
  280. using namespace boost::numeric;
  281. if( r == cNegOverflow )
  282. throw negative_overflow() ;
  283. else if( r == cPosOverflow )
  284. throw positive_overflow() ;
  285. }
  286. };
  287. //! Define a rounding policy and specialize on the custom type.
  288. template<class S>
  289. struct Ceil : boost::numeric::Ceil<S>{};
  290. template<>
  291. struct Ceil<Double>
  292. {
  293. typedef Double source_type;
  294. typedef Double const& argument_type;
  295. static source_type nearbyint ( argument_type s )
  296. {
  297. #if !defined(BOOST_NO_STDC_NAMESPACE)
  298. using std::ceil ;
  299. #endif
  300. return Double( ceil(s.v) );
  301. }
  302. typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style;
  303. };
  304. //! Define a rounding policy and specialize on the custom type.
  305. template<class S>
  306. struct Trunc: boost::numeric::Trunc<S>{};
  307. template<>
  308. struct Trunc<Double>
  309. {
  310. typedef Double source_type;
  311. typedef Double const& argument_type;
  312. static source_type nearbyint ( argument_type s )
  313. {
  314. #if !defined(BOOST_NO_STDC_NAMESPACE)
  315. using std::floor;
  316. #endif
  317. return Double( floor(s.v) );
  318. }
  319. typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style;
  320. };
  321. }//namespace custom;
  322. namespace boost { namespace numeric {
  323. //! Define the numeric_cast_traits specializations on the custom type.
  324. template <typename S>
  325. struct numeric_cast_traits<Double, S>
  326. {
  327. typedef custom::overflow_handler overflow_policy;
  328. typedef custom::range_checker
  329. <
  330. boost::numeric::conversion_traits<Double, S>
  331. , overflow_policy
  332. > range_checking_policy;
  333. typedef boost::numeric::Trunc<S> rounding_policy;
  334. };
  335. template <typename T>
  336. struct numeric_cast_traits<T, Double>
  337. {
  338. typedef custom::overflow_handler overflow_policy;
  339. typedef custom::range_checker
  340. <
  341. boost::numeric::conversion_traits<T, Double>
  342. , overflow_policy
  343. > range_checking_policy;
  344. typedef custom::Trunc<Double> rounding_policy;
  345. };
  346. //! Define the conversion from the custom type to built-in types and vice-versa.
  347. template<typename T>
  348. struct raw_converter< conversion_traits< T, Double > >
  349. {
  350. static T low_level_convert ( const Double& n )
  351. {
  352. return static_cast<T>( n.v );
  353. }
  354. };
  355. template<typename S>
  356. struct raw_converter< conversion_traits< Double, S > >
  357. {
  358. static Double low_level_convert ( const S& n )
  359. {
  360. return Double(n);
  361. }
  362. };
  363. }}//namespace boost::numeric;
  364. [endsect]
  365. [endsect]