ap.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. @file
  3. Defines `boost::hana::ap`.
  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_AP_HPP
  9. #define BOOST_HANA_AP_HPP
  10. #include <boost/hana/fwd/ap.hpp>
  11. #include <boost/hana/chain.hpp>
  12. #include <boost/hana/concept/applicative.hpp>
  13. #include <boost/hana/concept/sequence.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/variadic/foldl1.hpp>
  17. #include <boost/hana/functional/curry.hpp>
  18. #include <boost/hana/functional/partial.hpp>
  19. #include <boost/hana/transform.hpp>
  20. namespace boost { namespace hana {
  21. template <typename A, bool condition>
  22. struct ap_impl<A, when<condition>> : default_ {
  23. template <typename ...Args>
  24. static constexpr auto apply(Args&& ...args) = delete;
  25. };
  26. //! @cond
  27. template <typename F, typename X>
  28. constexpr decltype(auto) ap_t::operator()(F&& f, X&& x) const {
  29. using Function = typename hana::tag_of<F>::type;
  30. using Value = typename hana::tag_of<X>::type;
  31. using Ap = BOOST_HANA_DISPATCH_IF(ap_impl<Function>,
  32. hana::Applicative<Function>::value && hana::Applicative<Value>::value
  33. );
  34. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  35. static_assert(hana::Applicative<Function>::value,
  36. "hana::ap(f, x) requires 'f' to be an Applicative");
  37. static_assert(hana::Applicative<Value>::value,
  38. "hana::ap(f, x) requires 'x' to be an Applicative");
  39. #endif
  40. return Ap::apply(static_cast<F&&>(f), static_cast<X&&>(x));
  41. }
  42. template <typename F, typename ...Xs>
  43. constexpr decltype(auto) ap_t::operator()(F&& f, Xs&& ...xs) const {
  44. static_assert(sizeof...(xs) >= 1,
  45. "hana::ap must be called with at least two arguments");
  46. return detail::variadic::foldl1(
  47. *this,
  48. hana::transform(static_cast<F&&>(f), hana::curry<sizeof...(xs)>),
  49. static_cast<Xs&&>(xs)...
  50. );
  51. }
  52. //! @endcond
  53. template <typename S>
  54. struct ap_impl<S, when<Sequence<S>::value>> {
  55. template <typename F, typename X>
  56. static constexpr decltype(auto) apply(F&& f, X&& x) {
  57. return hana::chain(
  58. static_cast<F&&>(f),
  59. hana::partial(hana::transform, static_cast<X&&>(x))
  60. );
  61. }
  62. };
  63. }} // end namespace boost::hana
  64. #endif // !BOOST_HANA_AP_HPP