diff options
Diffstat (limited to 'boost/hana/detail/index_if.hpp')
-rw-r--r-- | boost/hana/detail/index_if.hpp | 68 |
1 files changed, 26 insertions, 42 deletions
diff --git a/boost/hana/detail/index_if.hpp b/boost/hana/detail/index_if.hpp index 8224f74be8..f643eb76ff 100644 --- a/boost/hana/detail/index_if.hpp +++ b/boost/hana/detail/index_if.hpp @@ -3,6 +3,7 @@ Defines `boost::hana::detail::index_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) */ @@ -11,61 +12,44 @@ Distributed under the Boost Software License, Version 1.0. #define BOOST_HANA_DETAIL_INDEX_IF_HPP #include <boost/hana/config.hpp> -#include <boost/hana/core/when.hpp> +#include <boost/hana/detail/decay.hpp> +#include <boost/hana/integral_constant.hpp> +#include <boost/hana/optional.hpp> #include <cstddef> #include <utility> BOOST_HANA_NAMESPACE_BEGIN namespace detail { - template <typename ...T> - struct pack { - static constexpr std::size_t length = sizeof...(T); + template <std::size_t i, std::size_t N, bool Done> + struct index_if_helper; + + template <std::size_t i, std::size_t N> + struct index_if_helper<i, N, false> { + template <typename Pred, typename X1, typename ...Xs> + using f = typename index_if_helper<i + 1, N, + static_cast<bool>(detail::decay<decltype( + std::declval<Pred>()(std::declval<X1>()))>::type::value) + >::template f<Pred, Xs...>; }; - template <typename T> - struct make_pack; - - template <template <typename...> class Template, typename ...T> - struct make_pack<Template<T...>> { - using type = pack<T...>; - }; - - template <typename T> struct make_pack<T const> : make_pack<T> { }; - template <typename T> struct make_pack<T&> : make_pack<T> { }; - template <typename T> struct make_pack<T&&> : make_pack<T> { }; - - - //! @ingroup group-details - //! Returns the index of the first element of the `pack<>` that satisfies - //! the predicate, or the size of the pack if there is no such element. - //! - //! @note - //! The predicate must return an `IntegralConstant` that can be explicitly - //! converted to `bool`. - template <typename Pred, typename Ts, typename = when<true>> - struct index_if; - - //! @cond - template <typename Pred, typename T, typename ...Ts> - struct index_if<Pred, pack<T, Ts...>, when<static_cast<bool>(decltype( - std::declval<Pred>()(std::declval<T>()) - )::value)>> { - static constexpr std::size_t value = 0; + template <std::size_t N> + struct index_if_helper<N, N, false> { + template <typename ...> + using f = hana::optional<>; }; - template <typename Pred, typename T, typename ...Ts> - struct index_if<Pred, pack<T, Ts...>, when<!static_cast<bool>(decltype( - std::declval<Pred>()(std::declval<T>()) - )::value)>> { - static constexpr std::size_t value = 1 + index_if<Pred, pack<Ts...>>::value; + template <std::size_t i, std::size_t N> + struct index_if_helper<i, N, true> { + template <typename ...> + using f = hana::optional<hana::size_t<i - 1>>; }; - template <typename Pred> - struct index_if<Pred, pack<>> { - static constexpr std::size_t value = 0; + template <typename Pred, typename ...Xs> + struct index_if { + using type = typename index_if_helper<0, sizeof...(Xs), false> + ::template f<Pred, Xs...>; }; - //! @endcond } BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_DETAIL_INDEX_IF_HPP |