replace_if.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*!
  2. @file
  3. Defines `boost::hana::replace_if`.
  4. @copyright Louis Dionne 2013-2017
  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_IF_HPP
  9. #define BOOST_HANA_REPLACE_IF_HPP
  10. #include <boost/hana/fwd/replace_if.hpp>
  11. #include <boost/hana/adjust_if.hpp>
  12. #include <boost/hana/concept/functor.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/functional/always.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename Pred, typename Value>
  19. constexpr auto replace_if_t::operator()(Xs&& xs, Pred&& pred, Value&& value) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using ReplaceIf = BOOST_HANA_DISPATCH_IF(replace_if_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_if(xs, pred, value) requires 'xs' to be a Functor");
  27. #endif
  28. return ReplaceIf::apply(static_cast<Xs&&>(xs),
  29. static_cast<Pred&&>(pred),
  30. static_cast<Value&&>(value));
  31. }
  32. //! @endcond
  33. template <typename Fun, bool condition>
  34. struct replace_if_impl<Fun, when<condition>> : default_ {
  35. template <typename Xs, typename Pred, typename Value>
  36. static constexpr auto apply(Xs&& xs, Pred&& pred, Value&& v) {
  37. return hana::adjust_if(static_cast<Xs&&>(xs),
  38. static_cast<Pred&&>(pred),
  39. hana::always(static_cast<Value&&>(v))
  40. );
  41. }
  42. };
  43. BOOST_HANA_NAMESPACE_END
  44. #endif // !BOOST_HANA_REPLACE_IF_HPP