prefix.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*!
  2. @file
  3. Defines `boost::hana::prefix`.
  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_PREFIX_HPP
  9. #define BOOST_HANA_PREFIX_HPP
  10. #include <boost/hana/fwd/prefix.hpp>
  11. #include <boost/hana/append.hpp>
  12. #include <boost/hana/chain.hpp>
  13. #include <boost/hana/concept/monad_plus.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/functional/partial.hpp>
  17. #include <boost/hana/lift.hpp>
  18. BOOST_HANA_NAMESPACE_BEGIN
  19. //! @cond
  20. template <typename Xs, typename Pref>
  21. constexpr auto prefix_t::operator()(Xs&& xs, Pref&& pref) const {
  22. using M = typename hana::tag_of<Xs>::type;
  23. using Prefix = BOOST_HANA_DISPATCH_IF(prefix_impl<M>,
  24. hana::MonadPlus<M>::value
  25. );
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(hana::MonadPlus<M>::value,
  28. "hana::prefix(xs, pref) requires 'xs' to be a MonadPlus");
  29. #endif
  30. return Prefix::apply(static_cast<Xs&&>(xs), static_cast<Pref&&>(pref));
  31. }
  32. //! @endcond
  33. template <typename M, bool condition>
  34. struct prefix_impl<M, when<condition>> : default_ {
  35. template <typename Xs, typename Z>
  36. static constexpr decltype(auto) apply(Xs&& xs, Z&& z) {
  37. return hana::chain(static_cast<Xs&&>(xs),
  38. hana::partial(hana::append, hana::lift<M>(static_cast<Z&&>(z))));
  39. }
  40. };
  41. BOOST_HANA_NAMESPACE_END
  42. #endif // !BOOST_HANA_PREFIX_HPP