adjust_if.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. @file
  3. Defines `boost::hana::adjust_if`.
  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_ADJUST_IF_HPP
  9. #define BOOST_HANA_ADJUST_IF_HPP
  10. #include <boost/hana/fwd/adjust_if.hpp>
  11. #include <boost/hana/bool.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/if.hpp>
  16. #include <boost/hana/transform.hpp>
  17. namespace boost { namespace hana {
  18. //! @cond
  19. template <typename Xs, typename Pred, typename F>
  20. constexpr auto adjust_if_t::operator()(Xs&& xs, Pred const& pred, F const& f) const {
  21. using S = typename hana::tag_of<Xs>::type;
  22. using AdjustIf = BOOST_HANA_DISPATCH_IF(adjust_if_impl<S>,
  23. hana::Functor<S>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Functor<S>::value,
  27. "hana::adjust_if(xs, pred, f) requires 'xs' to be a Functor");
  28. #endif
  29. return AdjustIf::apply(static_cast<Xs&&>(xs), pred, f);
  30. }
  31. //! @endcond
  32. namespace detail {
  33. template <typename Pred, typename F>
  34. struct apply_if {
  35. Pred const& pred;
  36. F const& f;
  37. template <typename X>
  38. constexpr decltype(auto) helper(bool cond, X&& x) const
  39. { return cond ? f(static_cast<X&&>(x)) : static_cast<X&&>(x); }
  40. template <typename X>
  41. constexpr decltype(auto) helper(hana::true_, X&& x) const
  42. { return f(static_cast<X&&>(x)); }
  43. template <typename X>
  44. constexpr decltype(auto) helper(hana::false_, X&& x) const
  45. { return static_cast<X&&>(x); }
  46. template <typename X>
  47. constexpr decltype(auto) operator()(X&& x) const {
  48. auto cond = hana::if_(pred(x), hana::true_c, hana::false_c);
  49. return this->helper(cond, static_cast<X&&>(x));
  50. }
  51. };
  52. }
  53. template <typename Fun, bool condition>
  54. struct adjust_if_impl<Fun, when<condition>> : default_ {
  55. template <typename Xs, typename Pred, typename F>
  56. static constexpr auto apply(Xs&& xs, Pred const& pred, F const& f) {
  57. return hana::transform(static_cast<Xs&&>(xs),
  58. detail::apply_if<Pred, F>{pred, f});
  59. }
  60. };
  61. }} // end namespace boost::hana
  62. #endif // !BOOST_HANA_ADJUST_IF_HPP