Skip to content

Commit 3cb9d9f

Browse files
committed
moveonly: add mp/type-optional.h
1 parent b32e2b0 commit 3cb9d9f

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(MP_PUBLIC_HEADERS
4646
include/mp/proxy-types.h
4747
include/mp/proxy.h
4848
include/mp/type-context.h
49+
include/mp/type-optional.h
4950
include/mp/util.h)
5051
add_library(multiprocess STATIC
5152
${MP_PROXY_SRCS}

include/mp/proxy-types.h

-35
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,6 @@ struct ReadDestUpdate
141141
Value& m_value;
142142
};
143143

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

166145
template <typename LocalType, typename Input, typename ReadDest>
167146
decltype(auto) CustomReadField(TypeList<std::shared_ptr<LocalType>>,
@@ -824,20 +803,6 @@ LocalType BuildPrimitive(InvokeContext& invoke_context,
824803
return value;
825804
}
826805

827-
template <typename LocalType, typename Value, typename Output>
828-
void CustomBuildField(TypeList<std::optional<LocalType>>,
829-
Priority<1>,
830-
InvokeContext& invoke_context,
831-
Value&& value,
832-
Output&& output)
833-
{
834-
if (value) {
835-
output.setHas();
836-
// FIXME: should std::move value if destvalue is rref?
837-
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
838-
}
839-
}
840-
841806
template <typename Output>
842807
void CustomBuildField(TypeList<std::exception>,
843808
Priority<1>,

include/mp/type-optional.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_OPTIONAL_H
6+
#define MP_PROXY_TYPE_OPTIONAL_H
7+
8+
#include <mp/util.h>
9+
10+
namespace mp {
11+
template <typename LocalType, typename Value, typename Output>
12+
void CustomBuildField(TypeList<std::optional<LocalType>>,
13+
Priority<1>,
14+
InvokeContext& invoke_context,
15+
Value&& value,
16+
Output&& output)
17+
{
18+
if (value) {
19+
output.setHas();
20+
// FIXME: should std::move value if destvalue is rref?
21+
BuildField(TypeList<LocalType>(), invoke_context, output, *value);
22+
}
23+
}
24+
25+
template <typename LocalType, typename Input, typename ReadDest>
26+
decltype(auto) CustomReadField(TypeList<std::optional<LocalType>>,
27+
Priority<1>,
28+
InvokeContext& invoke_context,
29+
Input&& input,
30+
ReadDest&& read_dest)
31+
{
32+
return read_dest.update([&](auto& value) {
33+
if (!input.has()) {
34+
value.reset();
35+
} else if (value) {
36+
ReadField(TypeList<LocalType>(), invoke_context, input, ReadDestUpdate(*value));
37+
} else {
38+
ReadField(TypeList<LocalType>(), invoke_context, input,
39+
ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
40+
value.emplace(std::forward<decltype(args)>(args)...);
41+
return *value;
42+
}));
43+
}
44+
});
45+
}
46+
} // namespace mp
47+
48+
#endif // MP_PROXY_TYPE_OPTIONAL_H

0 commit comments

Comments
 (0)