replace.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_08182005_0841)
  7. #define FUSION_REPLACE_08182005_0841
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/type_traits/is_convertible.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_helper;
  16. template <>
  17. struct replacer_helper<false>
  18. {
  19. template <typename U, typename T>
  20. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  21. static U&
  22. call(U& x, T const&, T const&)
  23. {
  24. return x;
  25. }
  26. };
  27. template <>
  28. struct replacer_helper<true>
  29. {
  30. template <typename U, typename T>
  31. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  32. static U
  33. call(U& x, T const& old_value, T const& new_value)
  34. {
  35. return (x == old_value) ? new_value : x;
  36. }
  37. };
  38. template <typename T>
  39. struct replacer
  40. {
  41. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  42. replacer(T const& in_old_value, T const& in_new_value)
  43. : old_value(in_old_value), new_value(in_new_value) {}
  44. template<typename Sig>
  45. struct result;
  46. template <typename U1, typename U2>
  47. struct result<replacer<U1>(U2)>
  48. {
  49. typedef typename remove_reference<U2>::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(U)>::type
  57. operator()(U const& x) const
  58. {
  59. return replacer_helper<is_convertible<T, U>::value>::
  60. call(x, old_value, new_value);
  61. }
  62. T old_value;
  63. T new_value;
  64. };
  65. }}}
  66. #endif