conversion_traits.qbk 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 conversion_traits<> traits class]
  9. [section Types]
  10. [section enumeration int_float_mixture_enum]
  11. namespace boost { namespace numeric {
  12. enum int_float_mixture_enum
  13. {
  14. integral_to_integral
  15. ,integral_to_float
  16. ,float_to_integral
  17. ,float_to_float
  18. } ;
  19. } } // namespace boost::numeric
  20. [endsect]
  21. [section enumeration sign_mixture_enum]
  22. namespace boost { namespace numeric {
  23. enum sign_mixture_enum
  24. {
  25. unsigned_to_unsigned
  26. ,signed_to_signed
  27. ,signed_to_unsigned
  28. ,unsigned_to_signed
  29. } ;
  30. } } // namespace boost::numeric
  31. [endsect]
  32. [section enumeration udt_builtin_mixture_enum]
  33. namespace boost { namespace numeric {
  34. enum udt_builtin_mixture_enum
  35. {
  36. builtin_to_builtin
  37. ,builtin_to_udt
  38. ,udt_to_builtin
  39. ,udt_to_udt
  40. } ;
  41. } } // namespace boost::numeric
  42. [endsect]
  43. [section template class int_float_mixture<>]
  44. namespace boost { namespace numeric {
  45. template <class T, class S>
  46. struct int_float_mixture : mpl::integral_c<int_float_mixture_enum, impl-def-value> {} ;
  47. } } // namespace boost::numeric
  48. Classifying `S` and `T` as either integral or float, this __MPL_INTEGRAL_CONSTANT__
  49. indicates the combination of these attributes.
  50. Its `::value` is of enumeration type
  51. [link boost_numericconversion.conversion_traits___traits_class.types.enumeration_int_float_mixture_enum `boost::numeric::int_float_mixture_enum`]
  52. [endsect]
  53. [section template class sign_mixture<>]
  54. namespace boost { namespace numeric {
  55. template <class T, class S>
  56. struct sign_mixture : mpl::integral_c<sign_mixture_enum, impl-def-value> {} ;
  57. } } // namespace boost::numeric
  58. Classifying `S` and `T` as either signed or unsigned, this __MPL_INTEGRAL_CONSTANT__
  59. indicates the combination of these attributes.
  60. Its `::value` is of enumeration type
  61. [link boost_numericconversion.conversion_traits___traits_class.types.enumeration_sign_mixture_enum `boost::numeric::sign_mixture_enum`]
  62. [endsect]
  63. [section template class udt_builtin_mixture<>]
  64. namespace boost { namespace numeric {
  65. template <class T, class S>
  66. struct udt_builtin_mixture : mpl::integral_c<udt_builtin__mixture_enum, impl-def-value> {} ;
  67. } } // namespace boost::numeric
  68. Classifying `S` and `T` as either user-defined or builtin, this __MPL_INTEGRAL_CONSTANT__
  69. indicates the combination of these attributes.
  70. Its `::value` is of enumeration type
  71. [link boost_numericconversion.conversion_traits___traits_class.types.enumeration_udt_builtin_mixture_enum `boost::numeric::udt_builtin_mixture_enum`]
  72. [endsect]
  73. [section template class is_subranged<>]
  74. namespace boost { namespace numeric {
  75. template <class T, class S>
  76. struct is_subranged : mpl::bool_<impl-def-value> {} ;
  77. } } // namespace boost::numeric
  78. Indicates if the range of the target type `T` is a subset of the range of the source
  79. type `S`. That is: if there are some source values which fall out of the
  80. Target type's range.
  81. It is a boolean __MPL_INTEGRAL_CONSTANT__.
  82. It does not indicate if a particular conversion is effectively out of range;
  83. it indicates that some conversion might be out of range because not all the
  84. source values are representable as Target type.
  85. [endsect]
  86. [section template class conversion_traits<>]
  87. namespace boost { namespace numeric {
  88. template <class T, class S>
  89. struct conversion_traits
  90. {
  91. mpl::integral_c<int_float_mixture_enum , ...> int_float_mixture ;
  92. mpl::integral_c<sign_mixture_enum , ...> sign_mixture;
  93. mpl::integral_c<udt_builtin_mixture_enum, ...> udt_builtin_mixture ;
  94. mpl::bool_<...> subranged ;
  95. mpl::bool_<...> trivial ;
  96. typedef T target_type ;
  97. typedef S source_type ;
  98. typedef ... argument_type ;
  99. typedef ... result_type ;
  100. typedef ... supertype ;
  101. typedef ... subtype ;
  102. } ;
  103. } } // namespace numeric, namespace boost
  104. This traits class indicates some properties of a ['numeric conversion] direction:
  105. from a source type `S` to a target type `T`. It does not indicate the properties
  106. of a ['specific] conversion, but of the conversion direction. See
  107. [link boost_numericconversion.definitions.subranged_conversion_direction__subtype_and_supertype Definitions] for details.
  108. The traits class provides the following __MPL_INTEGRAL_CONSTANT__\s of enumeration
  109. type. They express the combination of certain attributes of the Source and
  110. Target types (thus they are call mixture):
  111. [table
  112. [[ ][ ]]
  113. [[[*int_float_mixture ]][
  114. Same as given by the traits class
  115. [link boost_numericconversion.conversion_traits___traits_class.types.template_class_int_float_mixture__ int_float_mixture]
  116. ]]
  117. [[[*sign_mixture ]][
  118. Same as given by the traits class
  119. [link boost_numericconversion.conversion_traits___traits_class.types.template_class_sign_mixture__ sign_mixture]
  120. ]]
  121. [[[*udt_builtin_mixture ]]
  122. [Same as given by the traits class
  123. [link boost_numericconversion.conversion_traits___traits_class.types.template_class_udt_builtin_mixture__ udt_builtin_mixture]
  124. ]]
  125. ]
  126. The traits class provides the following __MPL_INTEGRAL_CONSTANT__\s of boolean type
  127. which indicates indirectly the relation between the Source and Target ranges
  128. (see [link boost_numericconversion.definitions.range_and_precision Definitions] for details).
  129. [table
  130. [[ ][ ]]
  131. [[subranged ][
  132. Same as given by [link boost_numericconversion.conversion_traits___traits_class.types.template_class_is_subranged__ is_subranged]
  133. ]]
  134. [[trivial][
  135. Indicates if both Source and Target, [_without cv-qualifications], are the same type.
  136. Its `::value` is of boolean type.
  137. ]]
  138. ]
  139. The traits class provides the following types. They are the Source and Target types classified
  140. and qualified for different purposes.
  141. [table
  142. [[ ][ ]]
  143. [[[*target_type]][
  144. The template parameter `T` without cv-qualifications
  145. ]]
  146. [[[*source_type]][
  147. The template parameter `S` without cv-qualifications
  148. ]]
  149. [[[*argument_type]][
  150. This type is either source_type or `source_type const&`.
  151. It represents the optimal argument type for the
  152. [link boost_numericconversion.converter___function_object converter] member functions.
  153. If S is a built-in type, this is `source_type`, otherwise, this is `source_type const&`.
  154. ]]
  155. [[[*result_type]][
  156. This type is either target_type or target_type const&
  157. It represents the return type of the
  158. [link boost_numericconversion.converter___function_object converter] member functions.
  159. If `T==S`, it is `target_type const&`, otherwise, it is `target_type`.
  160. ]]
  161. [[[*supertype]][
  162. If the conversion is subranged, it is `source_type`, otherwise, it is `target_type`
  163. ]]
  164. [[[*subtype]][
  165. If the conversion is subranged, it is `target_type`, otherwise, it is `source_type`
  166. ]]
  167. ]
  168. [endsect]
  169. [endsect]
  170. [section Examples]
  171. #include <cassert>
  172. #include <typeinfo>
  173. #include <boost/numeric/conversion/conversion_traits.hpp>
  174. int main()
  175. {
  176. // A trivial conversion.
  177. typedef boost::numeric::conversion_traits<short,short> Short2Short_Traits ;
  178. assert ( Short2Short_Traits::trivial::value ) ;
  179. // A subranged conversion.
  180. typedef boost::numeric::conversion_traits<double,unsigned int> UInt2Double_Traits ;
  181. assert ( UInt2Double_Traits::int_float_mixture::value == boost::numeric::integral_to_float ) ;
  182. assert ( UInt2Double_Traits::sign_mixture::value == boost::numeric::unsigned_to_signed ) ;
  183. assert ( !UInt2Double_Traits::subranged::value ) ;
  184. assert ( typeid(UInt2Double_Traits::supertype) == typeid(double) ) ;
  185. assert ( typeid(UInt2Double_Traits::subtype) == typeid(unsigned int) ) ;
  186. // A doubly subranged conversion.
  187. assert ( (boost::numeric::conversion_traits<short, unsigned short>::subranged::value) );
  188. assert ( (boost::numeric::conversion_traits<unsigned short, short>::subranged::value) );
  189. return 0;
  190. }
  191. [endsect]
  192. [endsect]