File indexing completed on 2025-01-30 09:43:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_ADJUST_IF_HPP
0011 #define BOOST_HANA_ADJUST_IF_HPP
0012
0013 #include <boost/hana/fwd/adjust_if.hpp>
0014
0015 #include <boost/hana/bool.hpp>
0016 #include <boost/hana/concept/functor.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/dispatch.hpp>
0019 #include <boost/hana/if.hpp>
0020 #include <boost/hana/transform.hpp>
0021
0022
0023 namespace boost { namespace hana {
0024
0025 template <typename Xs, typename Pred, typename F>
0026 constexpr auto adjust_if_t::operator()(Xs&& xs, Pred const& pred, F const& f) const {
0027 using S = typename hana::tag_of<Xs>::type;
0028 using AdjustIf = BOOST_HANA_DISPATCH_IF(adjust_if_impl<S>,
0029 hana::Functor<S>::value
0030 );
0031
0032 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0033 static_assert(hana::Functor<S>::value,
0034 "hana::adjust_if(xs, pred, f) requires 'xs' to be a Functor");
0035 #endif
0036
0037 return AdjustIf::apply(static_cast<Xs&&>(xs), pred, f);
0038 }
0039
0040
0041 namespace detail {
0042 template <typename Pred, typename F>
0043 struct apply_if {
0044 Pred const& pred;
0045 F const& f;
0046
0047 template <typename X>
0048 constexpr decltype(auto) helper(bool cond, X&& x) const
0049 { return cond ? f(static_cast<X&&>(x)) : static_cast<X&&>(x); }
0050
0051 template <typename X>
0052 constexpr decltype(auto) helper(hana::true_, X&& x) const
0053 { return f(static_cast<X&&>(x)); }
0054
0055 template <typename X>
0056 constexpr decltype(auto) helper(hana::false_, X&& x) const
0057 { return static_cast<X&&>(x); }
0058
0059
0060 template <typename X>
0061 constexpr decltype(auto) operator()(X&& x) const {
0062 auto cond = hana::if_(pred(x), hana::true_c, hana::false_c);
0063 return this->helper(cond, static_cast<X&&>(x));
0064 }
0065 };
0066 }
0067
0068 template <typename Fun, bool condition>
0069 struct adjust_if_impl<Fun, when<condition>> : default_ {
0070 template <typename Xs, typename Pred, typename F>
0071 static constexpr auto apply(Xs&& xs, Pred const& pred, F const& f) {
0072 return hana::transform(static_cast<Xs&&>(xs),
0073 detail::apply_if<Pred, F>{pred, f});
0074 }
0075 };
0076 }}
0077
0078 #endif