Bitcoin Core  31.0.0
P2P Digital Currency
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 
10 namespace mp {
12 template <typename Result, typename... Args>
13 class ProxyCallbackImpl final : public ProxyCallback<std::function<Result(Args...)>>
14 {
15  using Fn = std::function<Result(Args...)>;
17 
18 public:
19  ProxyCallbackImpl(Fn fn) : m_fn(std::move(fn)) {}
20  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 void 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)...)
41 template <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 
50 template <typename FnR, typename... FnParams, typename Input, typename ReadDest>
51 decltype(auto) CustomReadField(TypeList<std::function<FnR(FnParams...)>> types,
52  Priority<1>,
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
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
Definition: util.h:108
Wrapper around std::function for passing std::function objects between client and servers...
Definition: proxy.h:326
std::function< Result(Args...)> Fn
Definition: type-function.h:15
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
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
Mapping from capnp interface type to proxy server implementation (specializations are generated by pr...
Definition: proxy.h:28
Adapter to convert ProxyCallback object call to function object call.
Definition: type-function.h:13
ArgsManager & args
Definition: bitcoind.cpp:277
Connection & connection
Definition: proxy-io.h:31
InvokeContext m_proxy
Definition: type-function.h:44
Result call(Args &&... args) override
Definition: type-function.h:20
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