Bitcoin Core 29.99.0
P2P Digital Currency
type-interface.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_INTERFACE_H
6#define MP_PROXY_TYPE_INTERFACE_H
7
8#include <mp/util.h>
9
10namespace mp {
11template <typename Interface, typename Impl>
12kj::Own<typename Interface::Server> MakeProxyServer(InvokeContext& context, std::shared_ptr<Impl> impl)
13{
14 return kj::heap<ProxyServer<Interface>>(std::move(impl), context.connection);
15}
16
17template <typename Interface, typename Impl>
18kj::Own<typename Interface::Server> CustomMakeProxyServer(InvokeContext& context, std::shared_ptr<Impl>&& impl)
19{
20 return MakeProxyServer<Interface, Impl>(context, std::move(impl));
21}
22
23template <typename Impl, typename Value, typename Output>
24void CustomBuildField(TypeList<std::unique_ptr<Impl>>,
26 InvokeContext& invoke_context,
27 Value&& value,
28 Output&& output,
29 typename Decay<decltype(output.get())>::Calls* enable = nullptr)
30{
31 if (value) {
32 using Interface = typename decltype(output.get())::Calls;
33 output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::shared_ptr<Impl>(value.release())));
34 }
35}
36
37template <typename Impl, typename Value, typename Output>
38void CustomBuildField(TypeList<std::shared_ptr<Impl>>,
40 InvokeContext& invoke_context,
41 Value&& value,
42 Output&& output,
43 typename Decay<decltype(output.get())>::Calls* enable = nullptr)
44{
45 if (value) {
46 using Interface = typename decltype(output.get())::Calls;
47 output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::move(value)));
48 }
49}
50
51template <typename Impl, typename Output>
54 InvokeContext& invoke_context,
55 Impl& value,
56 Output&& output,
57 typename decltype(output.get())::Calls* enable = nullptr)
58{
59 // Disable deleter so proxy server object doesn't attempt to delete the
60 // wrapped implementation when the proxy client is destroyed or
61 // disconnected.
62 using Interface = typename decltype(output.get())::Calls;
63 output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::shared_ptr<Impl>(&value, [](Impl*){})));
64}
65
66template <typename Interface, typename Impl>
67std::unique_ptr<Impl> MakeProxyClient(InvokeContext& context, typename Interface::Client&& client)
68{
69 return std::make_unique<ProxyClient<Interface>>(
70 std::move(client), &context.connection, /* destroy_connection= */ false);
71}
72
73template <typename Interface, typename Impl>
74std::unique_ptr<Impl> CustomMakeProxyClient(InvokeContext& context, typename Interface::Client&& client)
75{
76 return MakeProxyClient<Interface, Impl>(context, kj::mv(client));
77}
78
79template <typename LocalType, typename Input, typename ReadDest>
80decltype(auto) CustomReadField(TypeList<std::unique_ptr<LocalType>>,
82 InvokeContext& invoke_context,
83 Input&& input,
84 ReadDest&& read_dest,
85 typename Decay<decltype(input.get())>::Calls* enable = nullptr)
86{
87 using Interface = typename Decay<decltype(input.get())>::Calls;
88 if (input.has()) {
89 return read_dest.construct(
90 CustomMakeProxyClient<Interface, LocalType>(invoke_context, std::move(input.get())));
91 }
92 return read_dest.construct();
93}
94
95template <typename LocalType, typename Input, typename ReadDest>
96decltype(auto) CustomReadField(TypeList<std::shared_ptr<LocalType>>,
98 InvokeContext& invoke_context,
99 Input&& input,
100 ReadDest&& read_dest,
101 typename Decay<decltype(input.get())>::Calls* enable = nullptr)
102{
103 using Interface = typename Decay<decltype(input.get())>::Calls;
104 if (input.has()) {
105 return read_dest.construct(
106 CustomMakeProxyClient<Interface, LocalType>(invoke_context, std::move(input.get())));
107 }
108 return read_dest.construct();
109}
110} // namespace mp
111
112#endif // MP_PROXY_TYPE_INTERFACE_H
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
std::unique_ptr< Impl > CustomMakeProxyClient(InvokeContext &context, typename Interface::Client &&client)
kj::Own< typename Interface::Server > MakeProxyServer(InvokeContext &context, std::shared_ptr< Impl > impl)
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
kj::Own< typename Interface::Server > CustomMakeProxyServer(InvokeContext &context, std::shared_ptr< Impl > &&impl)
std::unique_ptr< Impl > MakeProxyClient(InvokeContext &context, typename Interface::Client &&client)
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
Generic utility functions used by capnp code.
Definition: util.h:33