/*! @file Defines `boost::hana::find_if`. @copyright Louis Dionne 2013-2017 @copyright Jason Rice 2017 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_FIND_IF_HPP #define BOOST_HANA_FIND_IF_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace hana { //! @cond template constexpr auto find_if_t::operator()(Xs&& xs, Pred&& pred) const { using S = typename hana::tag_of::type; using FindIf = BOOST_HANA_DISPATCH_IF(find_if_impl, hana::Searchable::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Searchable::value, "hana::find_if(xs, pred) requires 'xs' to be a Searchable"); #endif return FindIf::apply(static_cast(xs), static_cast(pred)); } //! @endcond template struct find_if_impl> : default_ { template static constexpr auto apply(Args&& ...) = delete; }; namespace detail { template struct partial_at { Xs const& xs; template constexpr decltype(auto) operator()(I i) const { return hana::at(xs, i); } }; } template struct find_if_impl::value>> { template static constexpr auto apply(Xs&& xs, Pred&& pred) { using Result = decltype(hana::index_if( static_cast(xs), static_cast(pred))); return hana::transform(Result{}, detail::partial_at>{static_cast(xs)}); } }; template struct find_if_impl { template static constexpr auto find_if_helper(Xs&&, hana::false_) { return hana::nothing; } template static constexpr auto find_if_helper(Xs&& xs, hana::true_) { return hana::just(static_cast(xs)[0]); } template static constexpr auto apply(Xs&& xs, Pred&& pred) { return find_if_helper(static_cast(xs), hana::bool_c(pred)(static_cast(xs)[0]) )::value> ); } }; namespace struct_detail { template struct get_member { X x; template constexpr decltype(auto) operator()(Member&& member) && { return hana::second(static_cast(member))( static_cast(x) ); } }; } template struct find_if_impl::value>> { template static constexpr decltype(auto) apply(X&& x, Pred&& pred) { return hana::transform( hana::find_if(hana::accessors(), hana::compose(static_cast(pred), hana::first) ), struct_detail::get_member{static_cast(x)} ); } }; }} // end namespace boost::hana #endif // !BOOST_HANA_FIND_IF_HPP