replace.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. @file
  3. Defines `boost::hana::replace`.
  4. Copyright Louis Dionne 2013-2022
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_REPLACE_HPP
  9. #define BOOST_HANA_REPLACE_HPP
  10. #include <boost/hana/fwd/replace.hpp>
  11. #include <boost/hana/concept/functor.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/equal.hpp>
  15. #include <boost/hana/replace_if.hpp>
  16. namespace boost { namespace hana {
  17. //! @cond
  18. template <typename Xs, typename OldVal, typename NewVal>
  19. constexpr auto replace_t::operator()(Xs&& xs, OldVal&& oldval, NewVal&& newval) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using Replace = BOOST_HANA_DISPATCH_IF(replace_impl<S>,
  22. hana::Functor<S>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Functor<S>::value,
  26. "hana::replace(xs, oldval, newval) requires 'xs' to be a Functor");
  27. #endif
  28. return Replace::apply(static_cast<Xs&&>(xs),
  29. static_cast<OldVal&&>(oldval),
  30. static_cast<NewVal&&>(newval));
  31. }
  32. //! @endcond
  33. template <typename Fun, bool condition>
  34. struct replace_impl<Fun, when<condition>> : default_ {
  35. template <typename Xs, typename OldVal, typename NewVal>
  36. static constexpr decltype(auto)
  37. apply(Xs&& xs, OldVal&& oldval, NewVal&& newval) {
  38. return hana::replace_if(
  39. static_cast<Xs&&>(xs),
  40. hana::equal.to(static_cast<OldVal&&>(oldval)),
  41. static_cast<NewVal&&>(newval)
  42. );
  43. }
  44. };
  45. }} // end namespace boost::hana
  46. #endif // !BOOST_HANA_REPLACE_HPP