Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
type-function.h
Go to the documentation of this file.
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
10namespace mp {
12template <typename Result, typename... Args>
13class ProxyCallbackImpl final : public ProxyCallback<std::function<Result(Args...)>>
14{
15 using Fn = std::function<Result(Args...)>;
17
18public:
19 ProxyCallbackImpl(Fn fn) : m_fn(std::move(fn)) {}
20 Result call(Args&&... args) override { return m_fn(std::forward<Args>(args)...); }
21};
22
23template <typename Value, typename FnR, typename... FnParams, typename Output>
24void CustomBuildField(TypeList<std::function<FnR(FnParams...)>>,
26 InvokeContext& invoke_context,
27 Value&& value,
28 Output&& output)
29{
30 if (value) {
31 using Interface = typename decltype(output.get())::Calls;
32 using Callback = ProxyCallbackImpl<FnR, FnParams...>;
33 output.set(kj::heap<ProxyServer<Interface>>(
34 std::make_shared<Callback>(std::forward<Value>(value)), invoke_context.connection));
35 }
36}
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)...)
41template <typename InvokeContext>
43{
45
46 template <typename... CallParams>
47 decltype(auto) operator()(CallParams&&... params) { return this->m_proxy->call(std::forward<CallParams>(params)...); }
48};
49
50template <typename FnR, typename... FnParams, typename Input, typename ReadDest>
51decltype(auto) CustomReadField(TypeList<std::function<FnR(FnParams...)>> types,
53 InvokeContext& invoke_context,
54 Input&& input,
55 ReadDest&& read_dest)
56{
57 if (CustomHasField(types, invoke_context, input)) {
58 using Interface = typename Decay<decltype(input.get())>::Calls;
59 auto client = std::make_shared<ProxyClient<Interface>>(
60 input.get(), &invoke_context.connection, /* destroy_connection= */ false);
61 return read_dest.construct(ProxyCallFn<decltype(client)>{std::move(client)});
62 }
63 return read_dest.construct();
64};
65} // namespace mp
66
67#endif // MP_PROXY_TYPE_FUNCTION_H
ArgsManager & args
Definition bitcoind.cpp:277
Wrapper around std::function for passing std::function objects between client and servers.
Definition proxy.h:326
Adapter to convert ProxyCallback object call to function object call.
Result call(Args &&... args) override
std::function< Result(Args...)> Fn
Functions to serialize / deserialize common bitcoin types.
bool CustomHasField(TypeList< LocalTypes... >, InvokeContext &invoke_context, const Input &input)
std::decay_t< T > Decay
Type helper abbreviating std::decay.
Definition util.h:86
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest)
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output)
Definition common.h:29
Connection & connection
Definition proxy-io.h:31
InvokeContext m_proxy
Generic utility functions used by capnp code.
Definition util.h:33