extend.hpp 1.5 KB

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