Skip to content

Commit 619d2c7

Browse files
committed
moveonly: add mp/type-pointer.h
1 parent 3cb9d9f commit 619d2c7

File tree

3 files changed

+114
-99
lines changed

3 files changed

+114
-99
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-context.h
4949
include/mp/type-optional.h
50+
include/mp/type-pointer.h
5051
include/mp/util.h)
5152
add_library(multiprocess STATIC
5253
${MP_PROXY_SRCS}

include/mp/proxy-types.h

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -142,61 +142,7 @@ struct ReadDestUpdate
142142
};
143143

144144

145-
template <typename LocalType, typename Input, typename ReadDest>
146-
decltype(auto) CustomReadField(TypeList<std::shared_ptr<LocalType>>,
147-
Priority<0>,
148-
InvokeContext& invoke_context,
149-
Input&& input,
150-
ReadDest&& read_dest)
151-
{
152-
return read_dest.update([&](auto& value) {
153-
if (!input.has()) {
154-
value.reset();
155-
} else if (value) {
156-
ReadField(TypeList<LocalType>(), invoke_context, input, ReadDestUpdate(*value));
157-
} else {
158-
ReadField(TypeList<LocalType>(), invoke_context, input,
159-
ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
160-
value = std::make_shared<LocalType>(std::forward<decltype(args)>(args)...);
161-
return *value;
162-
}));
163-
}
164-
});
165-
}
166145

167-
template <typename LocalType, typename Input, typename ReadDest>
168-
decltype(auto) CustomReadField(TypeList<LocalType*>,
169-
Priority<1>,
170-
InvokeContext& invoke_context,
171-
Input&& input,
172-
ReadDest&& read_dest)
173-
{
174-
return read_dest.update([&](auto& value) {
175-
if (value) {
176-
ReadField(TypeList<LocalType>(), invoke_context, std::forward<Input>(input), ReadDestUpdate(*value));
177-
}
178-
});
179-
}
180-
181-
template <typename LocalType, typename Input, typename ReadDest>
182-
decltype(auto) CustomReadField(TypeList<std::shared_ptr<const LocalType>>,
183-
Priority<1>,
184-
InvokeContext& invoke_context,
185-
Input&& input,
186-
ReadDest&& read_dest)
187-
{
188-
return read_dest.update([&](auto& value) {
189-
if (!input.has()) {
190-
value.reset();
191-
return;
192-
}
193-
ReadField(TypeList<LocalType>(), invoke_context, std::forward<Input>(input),
194-
ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
195-
value = std::make_shared<LocalType>(std::forward<decltype(args)>(args)...);
196-
return *value;
197-
}));
198-
});
199-
}
200146

201147
template <typename LocalType, typename Input, typename ReadDest>
202148
decltype(auto) CustomReadField(TypeList<std::vector<LocalType>>,
@@ -664,26 +610,6 @@ void CustomBuildField(TypeList<Impl&>,
664610
output.set(CustomMakeProxyServer<Interface, Impl>(invoke_context, std::shared_ptr<Impl>(&value, [](Impl*){})));
665611
}
666612

667-
template <typename LocalType, typename Value, typename Output>
668-
void CustomBuildField(TypeList<LocalType*>, Priority<3>, InvokeContext& invoke_context, Value&& value, Output&& output)
669-
{
670-
if (value) {
671-
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
672-
}
673-
}
674-
675-
template <typename LocalType, typename Value, typename Output>
676-
void CustomBuildField(TypeList<std::shared_ptr<LocalType>>,
677-
Priority<1>,
678-
InvokeContext& invoke_context,
679-
Value&& value,
680-
Output&& output)
681-
{
682-
if (value) {
683-
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
684-
}
685-
}
686-
687613
// Adapter to let BuildField overloads methods work set & init list elements as
688614
// if they were fields of a struct. If BuildField is changed to use some kind of
689615
// accessor class instead of calling method pointers, then then maybe this could
@@ -909,31 +835,6 @@ void CustomBuildField(TypeList<LocalType> local_type,
909835
BuildOne<0>(local_type, invoke_context, output.init(), value);
910836
}
911837

912-
//! PassField override for C++ pointer arguments.
913-
template <typename Accessor, typename LocalType, typename ServerContext, typename Fn, typename... Args>
914-
void PassField(Priority<1>, TypeList<LocalType*>, ServerContext& server_context, const Fn& fn, Args&&... args)
915-
{
916-
const auto& params = server_context.call_context.getParams();
917-
const auto& input = Make<StructField, Accessor>(params);
918-
919-
if (!input.want()) {
920-
fn.invoke(server_context, std::forward<Args>(args)..., nullptr);
921-
return;
922-
}
923-
924-
InvokeContext& invoke_context = server_context;
925-
Decay<LocalType> param;
926-
927-
MaybeReadField(std::integral_constant<bool, Accessor::in>(), TypeList<LocalType>(), invoke_context, input,
928-
ReadDestUpdate(param));
929-
930-
fn.invoke(server_context, std::forward<Args>(args)..., &param);
931-
932-
auto&& results = server_context.call_context.getResults();
933-
MaybeBuildField(std::integral_constant<bool, Accessor::out>(), TypeList<LocalType>(), invoke_context,
934-
Make<StructField, Accessor>(results), param);
935-
}
936-
937838
//! PassField override for callable interface reference arguments.
938839
template <typename Accessor, typename LocalType, typename ServerContext, typename Fn, typename... Args>
939840
auto PassField(Priority<1>, TypeList<LocalType&>, ServerContext& server_context, Fn&& fn, Args&&... args)

include/mp/type-pointer.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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_POINTER_H
6+
#define MP_PROXY_TYPE_POINTER_H
7+
8+
#include <mp/util.h>
9+
10+
namespace mp {
11+
template <typename LocalType, typename Value, typename Output>
12+
void CustomBuildField(TypeList<LocalType*>, Priority<3>, InvokeContext& invoke_context, Value&& value, Output&& output)
13+
{
14+
if (value) {
15+
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
16+
}
17+
}
18+
19+
template <typename LocalType, typename Value, typename Output>
20+
void CustomBuildField(TypeList<std::shared_ptr<LocalType>>,
21+
Priority<1>,
22+
InvokeContext& invoke_context,
23+
Value&& value,
24+
Output&& output)
25+
{
26+
if (value) {
27+
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
28+
}
29+
}
30+
31+
template <typename LocalType, typename Input, typename ReadDest>
32+
decltype(auto) CustomReadField(TypeList<LocalType*>,
33+
Priority<1>,
34+
InvokeContext& invoke_context,
35+
Input&& input,
36+
ReadDest&& read_dest)
37+
{
38+
return read_dest.update([&](auto& value) {
39+
if (value) {
40+
ReadField(TypeList<LocalType>(), invoke_context, std::forward<Input>(input), ReadDestUpdate(*value));
41+
}
42+
});
43+
}
44+
45+
template <typename LocalType, typename Input, typename ReadDest>
46+
decltype(auto) CustomReadField(TypeList<std::shared_ptr<LocalType>>,
47+
Priority<0>,
48+
InvokeContext& invoke_context,
49+
Input&& input,
50+
ReadDest&& read_dest)
51+
{
52+
return read_dest.update([&](auto& value) {
53+
if (!input.has()) {
54+
value.reset();
55+
} else if (value) {
56+
ReadField(TypeList<LocalType>(), invoke_context, input, ReadDestUpdate(*value));
57+
} else {
58+
ReadField(TypeList<LocalType>(), invoke_context, input,
59+
ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
60+
value = std::make_shared<LocalType>(std::forward<decltype(args)>(args)...);
61+
return *value;
62+
}));
63+
}
64+
});
65+
}
66+
67+
template <typename LocalType, typename Input, typename ReadDest>
68+
decltype(auto) CustomReadField(TypeList<std::shared_ptr<const LocalType>>,
69+
Priority<1>,
70+
InvokeContext& invoke_context,
71+
Input&& input,
72+
ReadDest&& read_dest)
73+
{
74+
return read_dest.update([&](auto& value) {
75+
if (!input.has()) {
76+
value.reset();
77+
return;
78+
}
79+
ReadField(TypeList<LocalType>(), invoke_context, std::forward<Input>(input),
80+
ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
81+
value = std::make_shared<LocalType>(std::forward<decltype(args)>(args)...);
82+
return *value;
83+
}));
84+
});
85+
}
86+
87+
//! PassField override for C++ pointer arguments.
88+
template <typename Accessor, typename LocalType, typename ServerContext, typename Fn, typename... Args>
89+
void PassField(Priority<1>, TypeList<LocalType*>, ServerContext& server_context, const Fn& fn, Args&&... args)
90+
{
91+
const auto& params = server_context.call_context.getParams();
92+
const auto& input = Make<StructField, Accessor>(params);
93+
94+
if (!input.want()) {
95+
fn.invoke(server_context, std::forward<Args>(args)..., nullptr);
96+
return;
97+
}
98+
99+
InvokeContext& invoke_context = server_context;
100+
Decay<LocalType> param;
101+
102+
MaybeReadField(std::integral_constant<bool, Accessor::in>(), TypeList<LocalType>(), invoke_context, input,
103+
ReadDestUpdate(param));
104+
105+
fn.invoke(server_context, std::forward<Args>(args)..., &param);
106+
107+
auto&& results = server_context.call_context.getResults();
108+
MaybeBuildField(std::integral_constant<bool, Accessor::out>(), TypeList<LocalType>(), invoke_context,
109+
Make<StructField, Accessor>(results), param);
110+
}
111+
} // namespace mp
112+
113+
#endif // MP_PROXY_TYPE_POINTER_H

0 commit comments

Comments
 (0)