1 /*============================================================================= 2 Copyright (c) 2015 Paul Fultz II 3 capture.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 8 #ifndef BOOST_HOF_GUARD_CAPTURE_H 9 #define BOOST_HOF_GUARD_CAPTURE_H 10 11 #include <boost/hof/detail/callable_base.hpp> 12 #include <boost/hof/detail/compressed_pair.hpp> 13 #include <boost/hof/reveal.hpp> 14 #include <boost/hof/pack.hpp> 15 #include <boost/hof/always.hpp> 16 #include <boost/hof/detail/move.hpp> 17 #include <boost/hof/detail/result_type.hpp> 18 19 /// capture 20 /// ======= 21 /// 22 /// Description 23 /// ----------- 24 /// 25 /// The `capture` function decorator is used to capture values in a function. 26 /// It provides more flexibility in capturing than the lambda capture list in 27 /// C++. It provides a way to do move and perfect capturing. The values 28 /// captured are prepended to the argument list of the function that will be 29 /// called. 30 /// 31 /// Synopsis 32 /// -------- 33 /// 34 /// // Capture by decaying each value 35 /// template<class... Ts> 36 /// constexpr auto capture(Ts&&... xs); 37 /// 38 /// // Capture lvalues by reference and rvalue reference by reference 39 /// template<class... Ts> 40 /// constexpr auto capture_forward(Ts&&... xs); 41 /// 42 /// // Capture lvalues by reference and rvalues by value. 43 /// template<class... Ts> 44 /// constexpr auto capture_basic(Ts&&... xs); 45 /// 46 /// Semantics 47 /// --------- 48 /// 49 /// assert(capture(xs...)(f)(ys...) == f(xs..., ys...)); 50 /// 51 /// 52 /// Example 53 /// ------- 54 /// 55 /// #include <boost/hof.hpp> 56 /// #include <cassert> 57 /// 58 /// struct sum_f 59 /// { 60 /// template<class T, class U> 61 /// T operator()(T x, U y) const 62 /// { 63 /// return x+y; 64 /// } 65 /// }; 66 /// 67 /// int main() { 68 /// auto add_one = boost::hof::capture(1)(sum_f()); 69 /// assert(add_one(2) == 3); 70 /// } 71 /// 72 73 namespace boost { namespace hof { 74 75 namespace detail { 76 77 template<class F, class Pack> 78 struct capture_invoke : detail::compressed_pair<detail::callable_base<F>, Pack>, detail::function_result_type<F> 79 { 80 typedef capture_invoke fit_rewritable1_tag; 81 typedef detail::compressed_pair<detail::callable_base<F>, Pack> base; BOOST_HOF_INHERIT_CONSTRUCTORboost::hof::detail::capture_invoke82 BOOST_HOF_INHERIT_CONSTRUCTOR(capture_invoke, base) 83 template<class... Ts> 84 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept 85 { 86 return this->first(xs...); 87 } 88 89 template<class... Ts> get_packboost::hof::detail::capture_invoke90 constexpr const Pack& get_pack(Ts&&...xs) const noexcept 91 { 92 return this->second(xs...); 93 } 94 95 template<class Failure, class... Ts> 96 struct unpack_capture_failure 97 { 98 template<class... Us> 99 struct apply 100 { 101 typedef typename Failure::template of<Us..., Ts...> type; 102 }; 103 }; 104 105 struct capture_failure 106 { 107 template<class Failure> 108 struct apply 109 { 110 template<class... Ts> 111 struct of 112 : Pack::template apply<unpack_capture_failure<Failure, Ts...>>::type 113 {}; 114 }; 115 }; 116 117 struct failure 118 : failure_map<capture_failure, detail::callable_base<F>> 119 {}; 120 121 BOOST_HOF_RETURNS_CLASS(capture_invoke); 122 123 template<class... Ts> 124 constexpr BOOST_HOF_SFINAE_RESULT 125 ( 126 typename result_of<decltype(boost::hof::pack_join), 127 id_<const Pack&>, 128 result_of<decltype(boost::hof::pack_forward), id_<Ts>...> 129 >::type, 130 id_<detail::callable_base<F>&&> 131 ) 132 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS 133 ( 134 boost::hof::pack_join 135 ( 136 BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...)), 137 boost::hof::pack_forward(BOOST_HOF_FORWARD(Ts)(xs)...) 138 ) 139 (BOOST_HOF_RETURNS_C_CAST(detail::callable_base<F>&&)(BOOST_HOF_CONST_THIS->base_function(xs...))) 140 ); 141 }; 142 143 template<class Pack> 144 struct capture_pack : Pack 145 { 146 BOOST_HOF_INHERIT_CONSTRUCTOR(capture_pack, Pack); 147 148 BOOST_HOF_RETURNS_CLASS(capture_pack); 149 150 // TODO: Should use rvalue ref qualifier 151 template<class F> 152 constexpr auto operator()(F f) const BOOST_HOF_SFINAE_RETURNS 153 ( 154 capture_invoke<F, Pack>(BOOST_HOF_RETURNS_STATIC_CAST(F&&)(f), 155 BOOST_HOF_RETURNS_C_CAST(Pack&&)( 156 BOOST_HOF_RETURNS_STATIC_CAST(const Pack&)(*boost::hof::always(BOOST_HOF_CONST_THIS)(f)) 157 ) 158 ) 159 ); 160 }; 161 162 struct make_capture_pack_f 163 { 164 template<class Pack> operator ()boost::hof::detail::make_capture_pack_f165 constexpr capture_pack<Pack> operator()(Pack p) const 166 BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(capture_pack<Pack>, Pack&&) 167 { 168 return capture_pack<Pack>(static_cast<Pack&&>(p)); 169 } 170 }; 171 172 template<class F> 173 struct capture_f 174 { 175 template<class... Ts> 176 constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS 177 ( 178 BOOST_HOF_RETURNS_CONSTRUCT(make_capture_pack_f)()(BOOST_HOF_RETURNS_CONSTRUCT(F)()(BOOST_HOF_FORWARD(Ts)(xs)...)) 179 ); 180 }; 181 } 182 183 BOOST_HOF_DECLARE_STATIC_VAR(capture_basic, detail::capture_f<detail::pack_basic_f>); 184 BOOST_HOF_DECLARE_STATIC_VAR(capture_forward, detail::capture_f<detail::pack_forward_f>); 185 BOOST_HOF_DECLARE_STATIC_VAR(capture, detail::capture_f<detail::pack_f>); 186 187 }} // namespace boost::hof 188 189 #endif 190