replace_if.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_REPLACE_IF_08182005_0946)
  7. #define FUSION_REPLACE_IF_08182005_0946
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/utility/enable_if.hpp>
  10. #include <boost/mpl/if.hpp>
  11. #include <boost/type_traits/remove_reference.hpp>
  12. namespace boost { namespace fusion { namespace detail
  13. {
  14. template <bool is_convertible>
  15. struct replacer_if_helper;
  16. template <>
  17. struct replacer_if_helper<false>
  18. {
  19. template <typename U, typename F, typename T>
  20. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  21. static U&
  22. call(U& x, F&, T const&)
  23. {
  24. return x;
  25. }
  26. };
  27. template <>
  28. struct replacer_if_helper<true>
  29. {
  30. template <typename U, typename F, typename T>
  31. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  32. static U
  33. call(U& x, F& f, T const& new_value)
  34. {
  35. return f(x) ? new_value : x;
  36. }
  37. };
  38. template <typename F, typename T>
  39. struct replacer_if
  40. {
  41. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  42. replacer_if(F in_f, T const& in_new_value)
  43. : f(in_f), new_value(in_new_value) {}
  44. template<typename Params>
  45. struct result;
  46. template <typename F1, typename T1, typename U>
  47. struct result<replacer_if<F1, T1>(U)>
  48. {
  49. typedef typename remove_reference<U>::type value;
  50. typedef typename
  51. mpl::if_<is_convertible<T, value>, value, value const&>::type
  52. type;
  53. };
  54. template <typename U>
  55. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  56. typename result<replacer_if(U)>::type
  57. operator()(U const& x) const
  58. {
  59. return replacer_if_helper<is_convertible<T, U>::value>::
  60. call(x, f, new_value);
  61. }
  62. F f;
  63. T new_value;
  64. };
  65. }}}
  66. #endif