Bitcoin Core 29.99.0
P2P Digital Currency
type-function.h
Go to the documentation of this file.
1// Copyright (c) 2025 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...)>>,
53 InvokeContext& invoke_context,
54 Input&& input,
55 ReadDest&& read_dest)
56{
57 if (input.has()) {
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:287
Adapter to convert ProxyCallback object call to function object call.
Definition: type-function.h:14
Result call(Args &&... args) override
Definition: type-function.h:20
std::function< Result(Args...)> Fn
Definition: type-function.h:15
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest)
Overload multiprocess library's CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:83
std::decay_t< T > Decay
Type helper abbreviating std::decay.
Definition: util.h:86
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output)
Overload multiprocess library's CustomBuildField hook to allow any serializable object to be stored i...
Definition: common-types.h:63
Connection & connection
Definition: proxy-io.h:28
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Definition: util.h:109
InvokeContext m_proxy
Definition: type-function.h:44
Mapping from capnp interface type to proxy server implementation (specializations are generated by pr...
Definition: proxy.h:28
Generic utility functions used by capnp code.
Definition: util.h:33