then.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*!
  2. @file
  3. Defines `boost::hana::then`.
  4. Copyright Louis Dionne 2013-2022
  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_THEN_HPP
  9. #define BOOST_HANA_THEN_HPP
  10. #include <boost/hana/fwd/then.hpp>
  11. #include <boost/hana/chain.hpp>
  12. #include <boost/hana/concept/monad.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/functional/always.hpp>
  16. namespace boost { namespace hana {
  17. //! @cond
  18. template <typename Before, typename Xs>
  19. constexpr decltype(auto) then_t::operator()(Before&& before, Xs&& xs) const {
  20. using M = typename hana::tag_of<Before>::type;
  21. using Then = BOOST_HANA_DISPATCH_IF(then_impl<M>,
  22. hana::Monad<M>::value &&
  23. hana::Monad<Xs>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Monad<M>::value,
  27. "hana::then(before, xs) requires 'before' to be a Monad");
  28. static_assert(hana::Monad<Xs>::value,
  29. "hana::then(before, xs) requires 'xs' to be a Monad");
  30. #endif
  31. return Then::apply(static_cast<Before&&>(before),
  32. static_cast<Xs&&>(xs));
  33. }
  34. //! @endcond
  35. template <typename M, bool condition>
  36. struct then_impl<M, when<condition>> : default_ {
  37. template <typename Xs, typename Ys>
  38. static constexpr decltype(auto) apply(Xs&& xs, Ys&& ys) {
  39. return hana::chain(static_cast<Xs&&>(xs),
  40. hana::always(static_cast<Ys&&>(ys)));
  41. }
  42. };
  43. }} // end namespace boost::hana
  44. #endif // !BOOST_HANA_THEN_HPP