/Src/Dependencies/Boost/boost/fusion/algorithm/transformation/detail/replace.hpp

http://hadesmem.googlecode.com/ · C++ Header · 73 lines · 56 code · 11 blank · 6 comment · 1 complexity · 934a1a432608566f22b4dd3cfd1ac006 MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 2001-2006 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/type_traits/is_convertible.hpp>
  9. #include <boost/mpl/if.hpp>
  10. #include <boost/type_traits/remove_reference.hpp>
  11. namespace boost { namespace fusion { namespace detail
  12. {
  13. template <bool is_convertible>
  14. struct replacer_helper;
  15. template <>
  16. struct replacer_helper<false>
  17. {
  18. template <typename U, typename T>
  19. static U&
  20. call(U& x, T const&, T const&)
  21. {
  22. return x;
  23. }
  24. };
  25. template <>
  26. struct replacer_helper<true>
  27. {
  28. template <typename U, typename T>
  29. static U
  30. call(U& x, T const& old_value, T const& new_value)
  31. {
  32. return (x == old_value) ? new_value : x;
  33. }
  34. };
  35. template <typename T>
  36. struct replacer
  37. {
  38. replacer(T const& in_old_value, T const& in_new_value)
  39. : old_value(in_old_value), new_value(in_new_value) {}
  40. template<typename Sig>
  41. struct result;
  42. template <typename U1, typename U2>
  43. struct result<replacer<U1>(U2)>
  44. {
  45. typedef typename remove_reference<U2>::type value;
  46. typedef typename
  47. mpl::if_<is_convertible<T, value>, value, value const&>::type
  48. type;
  49. };
  50. template <typename U>
  51. typename result<replacer(U)>::type
  52. operator()(U const& x) const
  53. {
  54. return replacer_helper<is_convertible<T, U>::value>::
  55. call(x, old_value, new_value);
  56. }
  57. T old_value;
  58. T new_value;
  59. };
  60. }}}
  61. #endif