Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef MP_PROXY_TYPE_FUNCTION_H
6 : : #define MP_PROXY_TYPE_FUNCTION_H
7 : :
8 : : #include <mp/util.h>
9 : :
10 : : namespace mp {
11 : : //! Adapter to convert ProxyCallback object call to function object call.
12 : : template <typename Result, typename... Args>
13 : 0 : class ProxyCallbackImpl final : public ProxyCallback<std::function<Result(Args...)>>
14 : : {
15 : : using Fn = std::function<Result(Args...)>;
16 : : Fn m_fn;
17 : :
18 : : public:
19 : 0 : ProxyCallbackImpl(Fn fn) : m_fn(std::move(fn)) {}
20 : 0 : Result call(Args&&... args) override { return m_fn(std::forward<Args>(args)...); }
21 : : };
22 : :
23 : : template <typename Value, typename FnR, typename... FnParams, typename Output>
24 [ # # ]: 0 : void CustomBuildField(TypeList<std::function<FnR(FnParams...)>>,
25 : : Priority<1>,
26 : : InvokeContext& invoke_context,
27 : : Value&& value,
28 : : Output&& output)
29 : : {
30 [ # # ]: 0 : if (value) {
31 : : using Interface = typename decltype(output.get())::Calls;
32 : : using Callback = ProxyCallbackImpl<FnR, FnParams...>;
33 [ # # # # ]: 0 : output.set(kj::heap<ProxyServer<Interface>>(
34 [ # # ]: 0 : std::make_shared<Callback>(std::forward<Value>(value)), invoke_context.connection));
35 : : }
36 : 0 : }
37 : :
38 : : // ProxyCallFn class is needed because c++11 doesn't support auto lambda parameters.
39 : : // It's equivalent c++14: [invoke_context](auto&& params) {
40 : : // invoke_context->call(std::forward<decltype(params)>(params)...)
41 : : template <typename InvokeContext>
42 [ # # # # : 0 : struct ProxyCallFn
# # ]
43 : : {
44 : : InvokeContext m_proxy;
45 : :
46 : : template <typename... CallParams>
47 : 0 : decltype(auto) operator()(CallParams&&... params) { return this->m_proxy->call(std::forward<CallParams>(params)...); }
48 : : };
49 : :
50 : : template <typename FnR, typename... FnParams, typename Input, typename ReadDest>
51 : 0 : decltype(auto) CustomReadField(TypeList<std::function<FnR(FnParams...)>>,
52 : : Priority<1>,
53 : : InvokeContext& invoke_context,
54 : : Input&& input,
55 : : ReadDest&& read_dest)
56 : : {
57 [ # # ]: 0 : if (input.has()) {
58 : : using Interface = typename Decay<decltype(input.get())>::Calls;
59 [ # # ]: 0 : auto client = std::make_shared<ProxyClient<Interface>>(
60 [ # # ]: 0 : input.get(), &invoke_context.connection, /* destroy_connection= */ false);
61 [ # # # # ]: 0 : return read_dest.construct(ProxyCallFn<decltype(client)>{std::move(client)});
62 : 0 : }
63 : 0 : return read_dest.construct();
64 [ # # ]: 0 : };
65 : : } // namespace mp
66 : :
67 : : #endif // MP_PROXY_TYPE_FUNCTION_H
|