take_while.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*!
  2. @file
  3. Defines `boost::hana::take_while`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_TAKE_WHILE_HPP
  9. #define BOOST_HANA_TAKE_WHILE_HPP
  10. #include <boost/hana/fwd/take_while.hpp>
  11. #include <boost/hana/concept/sequence.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/detail/first_unsatisfied_index.hpp>
  15. #include <boost/hana/take_front.hpp>
  16. #include <boost/hana/unpack.hpp>
  17. BOOST_HANA_NAMESPACE_BEGIN
  18. //! @cond
  19. template <typename Xs, typename Pred>
  20. constexpr auto take_while_t::operator()(Xs&& xs, Pred&& pred) const {
  21. using S = typename hana::tag_of<Xs>::type;
  22. using TakeWhile = BOOST_HANA_DISPATCH_IF(take_while_impl<S>,
  23. hana::Sequence<S>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Sequence<S>::value,
  27. "hana::take_while(xs, pred) requires 'xs' to be a Sequence");
  28. #endif
  29. return TakeWhile::apply(static_cast<Xs&&>(xs),
  30. static_cast<Pred&&>(pred));
  31. }
  32. //! @endcond
  33. template <typename S, bool condition>
  34. struct take_while_impl<S, when<condition>> : default_ {
  35. template <typename Xs, typename Pred>
  36. static constexpr auto apply(Xs&& xs, Pred&&) {
  37. using FirstUnsatisfied = decltype(
  38. hana::unpack(static_cast<Xs&&>(xs),
  39. detail::first_unsatisfied_index<Pred&&>{})
  40. );
  41. return hana::take_front(static_cast<Xs&&>(xs), FirstUnsatisfied{});
  42. }
  43. };
  44. BOOST_HANA_NAMESPACE_END
  45. #endif // !BOOST_HANA_TAKE_WHILE_HPP