flatten.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*!
  2. @file
  3. Defines `boost::hana::flatten`.
  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_FLATTEN_HPP
  9. #define BOOST_HANA_FLATTEN_HPP
  10. #include <boost/hana/fwd/flatten.hpp>
  11. #include <boost/hana/concept/monad.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/detail/unpack_flatten.hpp>
  17. #include <boost/hana/functional/id.hpp>
  18. #include <boost/hana/fwd/chain.hpp>
  19. #include <cstddef>
  20. #include <utility>
  21. namespace boost { namespace hana {
  22. //! @cond
  23. template <typename Xs>
  24. constexpr auto flatten_t::operator()(Xs&& xs) const {
  25. using M = typename hana::tag_of<Xs>::type;
  26. using Flatten = BOOST_HANA_DISPATCH_IF(flatten_impl<M>,
  27. hana::Monad<M>::value
  28. );
  29. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  30. static_assert(hana::Monad<M>::value,
  31. "hana::flatten(xs) requires 'xs' to be a Monad");
  32. #endif
  33. return Flatten::apply(static_cast<Xs&&>(xs));
  34. }
  35. //! @endcond
  36. template <typename M, bool condition>
  37. struct flatten_impl<M, when<condition>> : default_ {
  38. template <typename Xs>
  39. static constexpr auto apply(Xs&& xs)
  40. { return hana::chain(static_cast<Xs&&>(xs), hana::id); }
  41. };
  42. template <typename S>
  43. struct flatten_impl<S, when<Sequence<S>::value>> {
  44. template <typename Xs>
  45. static constexpr auto apply(Xs&& xs) {
  46. return detail::unpack_flatten(static_cast<Xs&&>(xs), hana::make<S>);
  47. }
  48. };
  49. }} // end namespace boost::hana
  50. #endif // !BOOST_HANA_FLATTEN_HPP