meta.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #ifndef BOOST_NUMERIC_CONVERSION_DETAIL_META_FLC_12NOV2002_HPP
  10. #define BOOST_NUMERIC_CONVERSION_DETAIL_META_FLC_12NOV2002_HPP
  11. #include "boost/type_traits/remove_cv.hpp"
  12. #include "boost/mpl/if.hpp"
  13. #include "boost/mpl/eval_if.hpp"
  14. #include "boost/mpl/equal_to.hpp"
  15. #include "boost/mpl/not.hpp"
  16. #include "boost/mpl/and.hpp"
  17. #include "boost/mpl/bool.hpp"
  18. #include "boost/mpl/identity.hpp"
  19. namespace boost { namespace numeric { namespace convdetail
  20. {
  21. template< class T1, class T2>
  22. struct equal_to
  23. {
  24. #if !defined(BOOST_BORLANDC)
  25. enum { x = ( BOOST_MPL_AUX_VALUE_WKND(T1)::value == BOOST_MPL_AUX_VALUE_WKND(T2)::value ) };
  26. BOOST_STATIC_CONSTANT(bool, value = x);
  27. typedef mpl::bool_<value> type;
  28. #else
  29. BOOST_STATIC_CONSTANT(bool, value = (
  30. BOOST_MPL_AUX_VALUE_WKND(T1)::value
  31. == BOOST_MPL_AUX_VALUE_WKND(T2)::value
  32. ));
  33. typedef mpl::bool_<(
  34. BOOST_MPL_AUX_VALUE_WKND(T1)::value
  35. == BOOST_MPL_AUX_VALUE_WKND(T2)::value
  36. )> type;
  37. #endif
  38. };
  39. // Metafunction:
  40. //
  41. // ct_switch4<Value,Case0Val,Case1Val,Case2Val,Case0Type,Case1Type,Case2Type,DefaultType>::type
  42. //
  43. // {Value,Case(X)Val} are Integral Constants (such as: mpl::int_<>)
  44. // {Case(X)Type,DefaultType} are arbitrary types. (not metafunctions)
  45. //
  46. // Returns Case(X)Type if Val==Case(X)Val; DefaultType otherwise.
  47. //
  48. template<class Value,
  49. class Case0Val,
  50. class Case1Val,
  51. class Case2Val,
  52. class Case0Type,
  53. class Case1Type,
  54. class Case2Type,
  55. class DefaultType
  56. >
  57. struct ct_switch4
  58. {
  59. typedef mpl::identity<Case0Type> Case0TypeQ ;
  60. typedef mpl::identity<Case1Type> Case1TypeQ ;
  61. typedef equal_to<Value,Case0Val> is_case0 ;
  62. typedef equal_to<Value,Case1Val> is_case1 ;
  63. typedef equal_to<Value,Case2Val> is_case2 ;
  64. typedef mpl::if_<is_case2,Case2Type,DefaultType> choose_2_3Q ;
  65. typedef mpl::eval_if<is_case1,Case1TypeQ,choose_2_3Q> choose_1_2_3Q ;
  66. typedef typename
  67. mpl::eval_if<is_case0,Case0TypeQ,choose_1_2_3Q>::type
  68. type ;
  69. } ;
  70. // Metafunction:
  71. //
  72. // for_both<expr0,expr1,TT,TF,FT,FF>::type
  73. //
  74. // {exp0,expr1} are Boolean Integral Constants
  75. // {TT,TF,FT,FF} are aribtrary types. (not metafunctions)
  76. //
  77. // According to the combined boolean value of 'expr0 && expr1', selects the corresponding type.
  78. //
  79. template<class expr0, class expr1, class TT, class TF, class FT, class FF>
  80. struct for_both
  81. {
  82. typedef mpl::identity<TF> TF_Q ;
  83. typedef mpl::identity<TT> TT_Q ;
  84. typedef typename mpl::not_<expr0>::type not_expr0 ;
  85. typedef typename mpl::not_<expr1>::type not_expr1 ;
  86. typedef typename mpl::and_<expr0,expr1>::type caseTT ;
  87. typedef typename mpl::and_<expr0,not_expr1>::type caseTF ;
  88. typedef typename mpl::and_<not_expr0,expr1>::type caseFT ;
  89. typedef mpl::if_<caseFT,FT,FF> choose_FT_FF_Q ;
  90. typedef mpl::eval_if<caseTF,TF_Q,choose_FT_FF_Q> choose_TF_FT_FF_Q ;
  91. typedef typename mpl::eval_if<caseTT,TT_Q,choose_TF_FT_FF_Q>::type type ;
  92. } ;
  93. } } } // namespace boost::numeric::convdetail
  94. #endif