cast.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright Daniel Wallin 2006. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_PARAMETER_CAST_060902_HPP
  5. # define BOOST_PARAMETER_CAST_060902_HPP
  6. # include <boost/detail/workaround.hpp>
  7. # if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
  8. && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  9. # include <boost/type_traits/add_reference.hpp>
  10. # include <boost/type_traits/remove_const.hpp>
  11. # endif
  12. namespace boost { namespace parameter { namespace aux {
  13. struct use_default_tag {};
  14. # if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
  15. || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  16. # define BOOST_PARAMETER_FUNCTION_CAST(value, predicate) value
  17. # else
  18. // Handles possible implicit casts. Used by preprocessor.hpp to
  19. // normalize user input.
  20. //
  21. // cast<void*>::execute() is identity
  22. // cast<void*(X)>::execute() is identity
  23. // cast<void(X)>::execute() casts to X
  24. //
  25. // preprocessor.hpp uses this like this:
  26. //
  27. // #define X(value, predicate)
  28. // cast<void predicate>::execute(value)
  29. //
  30. // X(something, *)
  31. // X(something, *(predicate))
  32. // X(something, (int))
  33. template <class T, class Args>
  34. struct cast;
  35. template <class Args>
  36. struct cast<void*, Args>
  37. {
  38. static use_default_tag execute(use_default_tag)
  39. {
  40. return use_default_tag();
  41. }
  42. static use_default_tag remove_const(use_default_tag)
  43. {
  44. return use_default_tag();
  45. }
  46. template <class U>
  47. static U& execute(U& value)
  48. {
  49. return value;
  50. }
  51. template <class U>
  52. static U& remove_const(U& x)
  53. {
  54. return x;
  55. }
  56. };
  57. #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
  58. typedef void* voidstar;
  59. template <class T, class Args>
  60. struct cast<voidstar(T), Args>
  61. : cast<void*, Args>
  62. {
  63. };
  64. #else
  65. template <class T, class Args>
  66. struct cast<void*(T), Args>
  67. : cast<void*, Args>
  68. {
  69. };
  70. #endif
  71. // This is a hack used in cast<> to turn the user supplied type,
  72. // which may or may not be a placeholder expression into one, so
  73. // that it will be properly evaluated by mpl::apply.
  74. template <class T, class Dummy = mpl::_1>
  75. struct as_placeholder_expr
  76. {
  77. typedef T type;
  78. };
  79. template <class T, class Args>
  80. struct cast<void(T), Args>
  81. {
  82. typedef typename mpl::apply2<
  83. as_placeholder_expr<T>, Args, Args>::type type0;
  84. typedef typename boost::add_reference<
  85. typename boost::remove_const<type0>::type
  86. >::type reference;
  87. static use_default_tag execute(use_default_tag)
  88. {
  89. return use_default_tag();
  90. }
  91. static use_default_tag remove_const(use_default_tag)
  92. {
  93. return use_default_tag();
  94. }
  95. static type0 execute(type0 value)
  96. {
  97. return value;
  98. }
  99. template <class U>
  100. static reference remove_const(U const& x)
  101. {
  102. return const_cast<reference>(x);
  103. }
  104. };
  105. # define BOOST_PARAMETER_FUNCTION_CAST(value, predicate, args) \
  106. boost::parameter::aux::cast<void predicate, args>::remove_const( \
  107. boost::parameter::aux::cast<void predicate, args>::execute(value) \
  108. )
  109. # endif
  110. }}} // namespace boost::parameter::aux
  111. #endif // BOOST_PARAMETER_CAST_060902_HPP