Bitcoin Core  31.0.0
P2P Digital Currency
type-interface.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_INTERFACE_H
6 #define MP_PROXY_TYPE_INTERFACE_H
7 
8 #include <mp/util.h>
9 
10 namespace mp {
11 template <typename Interface, typename Impl>
12 kj::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 
17 template <typename Interface, typename Impl>
18 kj::Own<typename Interface::Server> CustomMakeProxyServer(InvokeContext& context, std::shared_ptr<Impl>&& impl)
19 {
20  return MakeProxyServer<Interface, Impl>(context, std::move(impl));
21 }
22 
23 template <typename Impl, typename Value, typename Output>
24 void 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 
37 template <typename Impl, typename Value, typename Output>
38 void 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::forward<Value>(value)));
48  }
49 }
50 
51 template <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 
66 template <typename Interface, typename Impl>
67 std::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 
73 template <typename Interface, typename Impl>
74 std::unique_ptr<Impl> CustomMakeProxyClient(InvokeContext& context, typename Interface::Client&& client)
75 {
76  return MakeProxyClient<Interface, Impl>(context, kj::mv(client));
77 }
78 
79 template <typename LocalType, typename Input, typename ReadDest>
80 decltype(auto) CustomReadField(TypeList<std::unique_ptr<LocalType>>,
81  Priority<1>,
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 (CustomHasField(TypeList<LocalType>(), invoke_context, input)) {
89  return read_dest.construct(
90  CustomMakeProxyClient<Interface, LocalType>(invoke_context, std::move(input.get())));
91  }
92  return read_dest.construct();
93 }
94 
95 template <typename LocalType, typename Input, typename ReadDest>
96 decltype(auto) CustomReadField(TypeList<std::shared_ptr<LocalType>>,
97  Priority<1>,
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 (CustomHasField(TypeList<LocalType>(), invoke_context, input)) {
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
std::unique_ptr< Impl > CustomMakeProxyClient(InvokeContext &context, typename Interface::Client &&client)
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
Definition: util.h:108
bool CustomHasField(TypeList< LocalTypes... >, InvokeContext &invoke_context, const Input &input)
Return whether to read a C++ value from a Cap&#39;n Proto field.
Definition: proxy-types.h:207
Definition: common.h:29
Generic utility functions used by capnp code.
Definition: util.h:32
kj::Own< typename Interface::Server > CustomMakeProxyServer(InvokeContext &context, std::shared_ptr< Impl > &&impl)
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
Connection & connection
Definition: proxy-io.h:31
std::unique_ptr< Impl > MakeProxyClient(InvokeContext &context, typename Interface::Client &&client)
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest) requires Unserializable< LocalType
Overload multiprocess library&#39;s CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:100
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output) requires Serializable< LocalType
Overload multiprocess library&#39;s CustomBuildField hook to allow any serializable object to be stored i...
std::decay_t< T > Decay
Type helper abbreviating std::decay.
Definition: util.h:86
kj::Own< typename Interface::Server > MakeProxyServer(InvokeContext &context, std::shared_ptr< Impl > impl)