Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:34

0001 /*!
0002 @file
0003 Defines `boost::hana::adjust_if`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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     //! @cond
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     //! @endcond
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 }} // end namespace boost::hana
0077 
0078 #endif // !BOOST_HANA_ADJUST_IF_HPP