chain.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*!
  2. @file
  3. Defines `boost::hana::chain`.
  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_CHAIN_HPP
  9. #define BOOST_HANA_CHAIN_HPP
  10. #include <boost/hana/fwd/chain.hpp>
  11. #include <boost/hana/concept/monad.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/flatten.hpp>
  15. #include <boost/hana/transform.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename F>
  19. constexpr decltype(auto) chain_t::operator()(Xs&& xs, F&& f) const {
  20. using M = typename hana::tag_of<Xs>::type;
  21. using Chain = BOOST_HANA_DISPATCH_IF(chain_impl<M>,
  22. hana::Monad<M>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Monad<M>::value,
  26. "hana::chain(xs, f) requires 'xs' to be a Monad");
  27. #endif
  28. return Chain::apply(static_cast<Xs&&>(xs), static_cast<F&&>(f));
  29. }
  30. //! @endcond
  31. template <typename M, bool condition>
  32. struct chain_impl<M, when<condition>> : default_ {
  33. template <typename Xs, typename F>
  34. static constexpr auto apply(Xs&& xs, F&& f) {
  35. return hana::flatten(hana::transform(static_cast<Xs&&>(xs),
  36. static_cast<F&&>(f)));
  37. }
  38. };
  39. BOOST_HANA_NAMESPACE_END
  40. #endif // !BOOST_HANA_CHAIN_HPP