Skip to content

Commit 5df55a3

Browse files
committed
moveonly: add mp/type-function.h
1 parent 0d2f939 commit 5df55a3

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(MP_PUBLIC_HEADERS
4747
include/mp/proxy.h
4848
include/mp/type-char.h
4949
include/mp/type-context.h
50+
include/mp/type-function.h
5051
include/mp/type-interface.h
5152
include/mp/type-map.h
5253
include/mp/type-number.h

include/mp/proxy-types.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -141,34 +141,6 @@ struct ReadDestUpdate
141141
Value& m_value;
142142
};
143143

144-
// ProxyCallFn class is needed because c++11 doesn't support auto lambda parameters.
145-
// It's equivalent c++14: [invoke_context](auto&& params) {
146-
// invoke_context->call(std::forward<decltype(params)>(params)...)
147-
template <typename InvokeContext>
148-
struct ProxyCallFn
149-
{
150-
InvokeContext m_proxy;
151-
152-
template <typename... CallParams>
153-
decltype(auto) operator()(CallParams&&... params) { return this->m_proxy->call(std::forward<CallParams>(params)...); }
154-
};
155-
156-
template <typename FnR, typename... FnParams, typename Input, typename ReadDest>
157-
decltype(auto) CustomReadField(TypeList<std::function<FnR(FnParams...)>>,
158-
Priority<1>,
159-
InvokeContext& invoke_context,
160-
Input&& input,
161-
ReadDest&& read_dest)
162-
{
163-
if (input.has()) {
164-
using Interface = typename Decay<decltype(input.get())>::Calls;
165-
auto client = std::make_shared<ProxyClient<Interface>>(
166-
input.get(), &invoke_context.connection, /* destroy_connection= */ false);
167-
return read_dest.construct(ProxyCallFn<decltype(client)>{std::move(client)});
168-
}
169-
return read_dest.construct();
170-
};
171-
172144
template <size_t index, typename LocalType, typename Input, typename Value>
173145
void ReadOne(TypeList<LocalType> param,
174146
InvokeContext& invoke_context,
@@ -274,32 +246,6 @@ void BuildField(TypeList<LocalTypes...>, Context& context, Output&& output, Valu
274246
}
275247
}
276248

277-
//! Adapter to convert ProxyCallback object call to function object call.
278-
template <typename Result, typename... Args>
279-
class ProxyCallbackImpl final : public ProxyCallback<std::function<Result(Args...)>>
280-
{
281-
using Fn = std::function<Result(Args...)>;
282-
Fn m_fn;
283-
284-
public:
285-
ProxyCallbackImpl(Fn fn) : m_fn(std::move(fn)) {}
286-
Result call(Args&&... args) override { return m_fn(std::forward<Args>(args)...); }
287-
};
288-
289-
template <typename Value, typename FnR, typename... FnParams, typename Output>
290-
void CustomBuildField(TypeList<std::function<FnR(FnParams...)>>,
291-
Priority<1>,
292-
InvokeContext& invoke_context,
293-
Value& value,
294-
Output&& output)
295-
{
296-
if (value) {
297-
using Interface = typename decltype(output.get())::Calls;
298-
using Callback = ProxyCallbackImpl<FnR, FnParams...>;
299-
output.set(kj::heap<ProxyServer<Interface>>(
300-
std::make_shared<Callback>(std::forward<Value>(value)), invoke_context.connection));
301-
}
302-
}
303249
// Adapter to let BuildField overloads methods work set & init list elements as
304250
// if they were fields of a struct. If BuildField is changed to use some kind of
305251
// accessor class instead of calling method pointers, then then maybe this could

include/mp/type-function.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
10+
namespace mp {
11+
//! Adapter to convert ProxyCallback object call to function object call.
12+
template <typename Result, typename... Args>
13+
class ProxyCallbackImpl final : public ProxyCallback<std::function<Result(Args...)>>
14+
{
15+
using Fn = std::function<Result(Args...)>;
16+
Fn m_fn;
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...)>>,
25+
Priority<1>,
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>
42+
struct ProxyCallFn
43+
{
44+
InvokeContext m_proxy;
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...)>>,
52+
Priority<1>,
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

0 commit comments

Comments
 (0)