Bitcoin Core 31.99.0
P2P Digital Currency
type-optional.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_OPTIONAL_H
6#define MP_PROXY_TYPE_OPTIONAL_H
7
8#include <mp/util.h>
9
10namespace mp {
11template <typename LocalType, typename Value, typename Output>
12void CustomBuildField(TypeList<std::optional<LocalType>>,
14 InvokeContext& invoke_context,
15 Value&& value,
16 Output&& output)
17{
18 if (value) {
19 output.setHas();
20 BuildField(TypeList<LocalType>(), invoke_context, output, *std::forward<Value>(value));
21 }
22}
23
24template <typename LocalType, typename Input, typename ReadDest>
25decltype(auto) CustomReadField(TypeList<std::optional<LocalType>>,
27 InvokeContext& invoke_context,
28 Input&& input,
29 ReadDest&& read_dest)
30{
31 return read_dest.update([&](auto& value) {
32 if (!CustomHasField(TypeList<LocalType>(), invoke_context, input)) {
33 value.reset();
34 } else if (value) {
35 ReadField(TypeList<LocalType>(), invoke_context, input, ReadDestUpdate(*value));
36 } else {
37 ReadField(TypeList<LocalType>(), invoke_context, input,
38 ReadDestEmplace(TypeList<LocalType>(), [&](auto&&... args) -> auto& {
39 value.emplace(std::forward<decltype(args)>(args)...);
40 return *value;
41 }));
42 }
43 });
44}
45} // namespace mp
46
47#endif // MP_PROXY_TYPE_OPTIONAL_H
ArgsManager & args
Definition: bitcoind.cpp:278
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
bool CustomHasField(TypeList< LocalTypes... >, InvokeContext &invoke_context, const Input &input)
Return whether to read a C++ value from a Cap'n Proto field.
Definition: proxy-types.h:207
void BuildField(TypeList< LocalTypes... >, Context &context, Output &&output, Values &&... values)
Definition: proxy-types.h:250
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest)
Overload multiprocess library's CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:83
decltype(auto) ReadField(TypeList< LocalTypes... >, InvokeContext &invoke_context, Input &&input, Args &&... args)
Definition: proxy-types.h:213
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output)
Overload multiprocess library's CustomBuildField hook to allow any serializable object to be stored i...
Definition: common-types.h:63
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Definition: util.h:109
Destination parameter type that can be passed to ReadField function as an alternative to ReadDestEmpl...
Definition: proxy-types.h:146
Generic utility functions used by capnp code.
Definition: util.h:33