combine.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. combine.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_COMBINE_H
  8. #define BOOST_HOF_GUARD_COMBINE_H
  9. /// combine
  10. /// =======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `combine` function adaptor combines several functions together with
  16. /// their arguments. It essentially zips each function with an argument before
  17. /// calling the main function.
  18. ///
  19. /// Synopsis
  20. /// --------
  21. ///
  22. /// template<class F, class... Gs>
  23. /// constexpr combine_adaptor<F, Gs...> combine(F f, Gs... gs);
  24. ///
  25. /// Semantics
  26. /// ---------
  27. ///
  28. /// assert(combine(f, gs...)(xs...) == f(gs(xs)...));
  29. ///
  30. /// Requirements
  31. /// ------------
  32. ///
  33. /// F and Gs must be:
  34. ///
  35. /// * [ConstInvocable](ConstInvocable)
  36. /// * MoveConstructible
  37. ///
  38. /// Example
  39. /// -------
  40. ///
  41. /// #include <boost/hof.hpp>
  42. /// #include <cassert>
  43. /// #include <tuple>
  44. /// #include <utility>
  45. ///
  46. /// int main() {
  47. /// auto f = boost::hof::combine(
  48. /// boost::hof::construct<std::tuple>(),
  49. /// boost::hof::capture(1)(boost::hof::construct<std::pair>()),
  50. /// boost::hof::capture(2)(boost::hof::construct<std::pair>()));
  51. /// assert(f(3, 7) == std::make_tuple(std::make_pair(1, 3), std::make_pair(2, 7)));
  52. /// }
  53. ///
  54. #include <boost/hof/pack.hpp>
  55. #include <boost/hof/always.hpp>
  56. #include <boost/hof/detail/callable_base.hpp>
  57. #include <boost/hof/detail/make.hpp>
  58. namespace boost { namespace hof { namespace detail {
  59. template<class S, class F, class... Gs>
  60. struct combine_adaptor_base;
  61. template<std::size_t... Ns, class F, class... Gs>
  62. struct combine_adaptor_base<seq<Ns...>, F, Gs...>
  63. : F, pack_base<seq<Ns...>, Gs...>
  64. {
  65. typedef pack_base<seq<Ns...>, Gs...> base_type;
  66. BOOST_HOF_INHERIT_DEFAULT(combine_adaptor_base, base_type, F)
  67. template<class X, class... Xs,
  68. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(F, X),
  69. BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base_type, Xs...)>
  70. constexpr combine_adaptor_base(X&& x, Xs&&... xs)
  71. : F(BOOST_HOF_FORWARD(X)(x)), base_type(BOOST_HOF_FORWARD(Xs)(xs)...)
  72. {}
  73. template<class... Ts>
  74. constexpr const F& base_function(Ts&&... xs) const
  75. {
  76. return boost::hof::always_ref(*this)(xs...);
  77. }
  78. BOOST_HOF_RETURNS_CLASS(combine_adaptor_base);
  79. // Result needs to be calculated in a separate class to avoid confusing the
  80. // compiler on MSVC
  81. #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
  82. template<class... Ts>
  83. struct combine_result
  84. : result_of<const F&, result_of<const Gs&, id_<Ts>>...>
  85. {};
  86. #endif
  87. template<class... Ts>
  88. #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
  89. constexpr typename combine_result<Ts...>::type
  90. #else
  91. constexpr auto
  92. #endif
  93. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS
  94. (
  95. (BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
  96. (boost::hof::alias_value<pack_tag<seq<Ns>, Gs...>, Gs>(*BOOST_HOF_CONST_THIS, xs)(BOOST_HOF_FORWARD(Ts)(xs))...)
  97. );
  98. };
  99. }
  100. template<class F, class... Gs>
  101. struct combine_adaptor
  102. : detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...>
  103. {
  104. typedef detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...> base_type;
  105. BOOST_HOF_INHERIT_CONSTRUCTOR(combine_adaptor, base_type)
  106. };
  107. BOOST_HOF_DECLARE_STATIC_VAR(combine, detail::make<combine_adaptor>);
  108. }} // namespace boost::hof
  109. #endif