3 Defines `boost::hana::find_if`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_FIND_IF_HPP
11 #define BOOST_HANA_FIND_IF_HPP
13 #include <boost/hana/fwd/find_if.hpp>
15 #include <boost/hana/accessors.hpp>
16 #include <boost/hana/at.hpp>
17 #include <boost/hana/bool.hpp>
18 #include <boost/hana/concept/iterable.hpp>
19 #include <boost/hana/concept/searchable.hpp>
20 #include <boost/hana/concept/sequence.hpp>
21 #include <boost/hana/concept/struct.hpp>
22 #include <boost/hana/config.hpp>
23 #include <boost/hana/core/dispatch.hpp>
24 #include <boost/hana/detail/decay.hpp>
25 #include <boost/hana/drop_while.hpp>
26 #include <boost/hana/first.hpp>
27 #include <boost/hana/front.hpp>
28 #include <boost/hana/functional/compose.hpp>
29 #include <boost/hana/is_empty.hpp>
30 #include <boost/hana/length.hpp>
31 #include <boost/hana/not.hpp>
32 #include <boost/hana/optional.hpp>
33 #include <boost/hana/second.hpp>
34 #include <boost/hana/transform.hpp>
40 BOOST_HANA_NAMESPACE_BEGIN
42 template <typename Xs, typename Pred>
43 constexpr auto find_if_t::operator()(Xs&& xs, Pred&& pred) const {
44 using S = typename hana::tag_of<Xs>::type;
45 using FindIf = BOOST_HANA_DISPATCH_IF(find_if_impl<S>,
46 hana::Searchable<S>::value
49 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
50 static_assert(hana::Searchable<S>::value,
51 "hana::find_if(xs, pred) requires 'xs' to be a Searchable");
54 return FindIf::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
58 template <typename S, bool condition>
59 struct find_if_impl<S, when<condition>> : default_ {
60 template <typename ...Args>
61 static constexpr auto apply(Args&& ...) = delete;
65 template <typename Xs, typename Pred, std::size_t i, std::size_t N, bool Done>
68 template <typename Xs, typename Pred, std::size_t i, std::size_t N>
69 struct advance_until<Xs, Pred, i, N, false>
70 : advance_until<Xs, Pred, i + 1, N, static_cast<bool>(detail::decay<decltype(
71 std::declval<Pred>()(hana::at_c<i>(std::declval<Xs>()))
75 template <typename Xs, typename Pred, std::size_t N>
76 struct advance_until<Xs, Pred, N, N, false> {
77 template <typename Ys>
78 static constexpr auto apply(Ys&&) {
83 template <typename Xs, typename Pred, std::size_t i, std::size_t N>
84 struct advance_until<Xs, Pred, i, N, true> {
85 template <typename Ys>
86 static constexpr auto apply(Ys&& ys) {
87 return hana::just(hana::at_c<i - 1>(static_cast<Ys&&>(ys)));
93 struct find_if_impl<S, when<Sequence<S>::value>> {
94 template <typename Xs, typename Pred>
95 static constexpr auto apply(Xs&& xs, Pred&&) {
96 constexpr std::size_t N = decltype(hana::length(xs))::value;
97 return detail::advance_until<Xs&&, Pred&&, 0, N, false>::apply(
103 template <typename It>
104 struct find_if_impl<It, when<hana::Iterable<It>::value && !Sequence<It>::value>> {
105 template <typename Xs, typename Pred>
106 static constexpr auto find_if_helper(Xs&& xs, Pred&& pred, hana::true_) {
107 return hana::just(hana::front(
108 hana::drop_while(static_cast<Xs&&>(xs),
109 hana::compose(hana::not_, static_cast<Pred&&>(pred)))
113 template <typename Xs, typename Pred>
114 static constexpr auto find_if_helper(Xs&&, Pred&&, hana::false_) {
115 return hana::nothing;
118 template <typename Xs, typename Pred>
119 static constexpr auto apply(Xs&& xs, Pred&& pred) {
120 constexpr bool found = !decltype(
121 hana::is_empty(hana::drop_while(static_cast<Xs&&>(xs),
122 hana::compose(hana::not_, static_cast<Pred&&>(pred))))
124 return find_if_impl::find_if_helper(static_cast<Xs&&>(xs),
125 static_cast<Pred&&>(pred),
126 hana::bool_<found>{});
130 template <typename T, std::size_t N>
131 struct find_if_impl<T[N]> {
132 template <typename Xs>
133 static constexpr auto find_if_helper(Xs&&, hana::false_)
134 { return hana::nothing; }
136 template <typename Xs>
137 static constexpr auto find_if_helper(Xs&& xs, hana::true_)
138 { return hana::just(static_cast<Xs&&>(xs)[0]); }
140 template <typename Xs, typename Pred>
141 static constexpr auto apply(Xs&& xs, Pred&& pred) {
142 return find_if_helper(static_cast<Xs&&>(xs),
143 hana::bool_c<decltype(
144 static_cast<Pred&&>(pred)(static_cast<Xs&&>(xs)[0])
150 namespace struct_detail {
151 template <typename X>
154 template <typename Member>
155 constexpr decltype(auto) operator()(Member&& member) && {
156 return hana::second(static_cast<Member&&>(member))(
163 template <typename S>
164 struct find_if_impl<S, when<hana::Struct<S>::value>> {
165 template <typename X, typename Pred>
166 static constexpr decltype(auto) apply(X&& x, Pred&& pred) {
167 return hana::transform(
168 hana::find_if(hana::accessors<S>(),
169 hana::compose(static_cast<Pred&&>(pred), hana::first)
171 struct_detail::get_member<X>{static_cast<X&&>(x)}
175 BOOST_HANA_NAMESPACE_END
177 #endif // !BOOST_HANA_FIND_IF_HPP