]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/hana/index_if.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / hana / index_if.hpp
1 /*!
2 @file
3 Defines `boost::hana::index_if`.
4
5 @copyright Louis Dionne 2013-2017
6 @copyright Jason Rice 2017
7 Distributed under the Boost Software License, Version 1.0.
8 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
9 */
10
11 #ifndef BOOST_HANA_INDEX_IF_HPP
12 #define BOOST_HANA_INDEX_IF_HPP
13
14 #include <boost/hana/concept/foldable.hpp>
15 #include <boost/hana/concept/iterable.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/detail/decay.hpp>
18 #include <boost/hana/detail/index_if.hpp>
19 #include <boost/hana/fwd/at.hpp>
20 #include <boost/hana/fwd/basic_tuple.hpp>
21 #include <boost/hana/fwd/index_if.hpp>
22 #include <boost/hana/integral_constant.hpp>
23 #include <boost/hana/length.hpp>
24 #include <boost/hana/optional.hpp>
25
26 #include <cstddef>
27 #include <utility>
28
29
30 BOOST_HANA_NAMESPACE_BEGIN
31 //! @cond
32 template <typename Xs, typename Pred>
33 constexpr auto index_if_t::operator()(Xs&& xs, Pred&& pred) const {
34 using S = typename hana::tag_of<Xs>::type;
35 using IndexIf = BOOST_HANA_DISPATCH_IF(index_if_impl<S>,
36 hana::Iterable<S>::value
37 );
38
39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
40 static_assert(hana::Iterable<S>::value,
41 "hana::index_if(xs, pred) requires 'xs' to be a Iterable");
42 #endif
43
44 return IndexIf::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
45 }
46 //! @endcond
47
48 namespace detail {
49 template <std::size_t i, std::size_t N, bool Done>
50 struct iterate_while;
51
52 template <std::size_t i, std::size_t N>
53 struct iterate_while<i, N, false> {
54 template <typename Xs, typename Pred>
55 using f = typename iterate_while<i + 1, N,
56 static_cast<bool>(detail::decay<decltype(
57 std::declval<Pred>()(
58 hana::at(std::declval<Xs>(), hana::size_c<i>)))>::type::value)
59 >::template f<Xs, Pred>;
60 };
61
62 template <std::size_t N>
63 struct iterate_while<N, N, false> {
64 template <typename Xs, typename Pred>
65 using f = hana::optional<>;
66 };
67
68 template <std::size_t i, std::size_t N>
69 struct iterate_while<i, N, true> {
70 template <typename Xs, typename Pred>
71 using f = hana::optional<hana::size_t<i - 1>>;
72 };
73 }
74
75 template <typename Tag>
76 struct index_if_impl<Tag, when<Foldable<Tag>::value>> {
77 template <typename Xs, typename Pred>
78 static constexpr auto apply(Xs const& xs, Pred const&)
79 -> typename detail::iterate_while<0,
80 decltype(hana::length(xs))::value, false>
81 ::template f<Xs, Pred>
82 { return {}; }
83 };
84
85 template <typename It>
86 struct index_if_impl<It, when<!Foldable<It>::value>> {
87 template <typename Xs, typename Pred>
88 static constexpr auto apply(Xs const&, Pred const&)
89 -> typename detail::iterate_while<0,
90 static_cast<std::size_t>(-1), false>
91 ::template f<Xs, Pred>
92 { return {}; }
93 };
94
95 // basic_tuple is implemented here to solve circular dependency issues.
96 template <>
97 struct index_if_impl<basic_tuple_tag> {
98 template <typename ...Xs, typename Pred>
99 static constexpr auto apply(basic_tuple<Xs...> const&, Pred const&)
100 -> typename detail::index_if<Pred, Xs...>::type
101 { return {}; }
102 };
103 BOOST_HANA_NAMESPACE_END
104
105 #endif // !BOOST_HANA_INDEX_IF_HPP