Bitcoin Core 29.99.0
P2P Digital Currency
type-data.h
Go to the documentation of this file.
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_DATA_H
6#define MP_PROXY_TYPE_DATA_H
7
8#include <mp/util.h>
9
10namespace mp {
11template <typename T, typename U>
12concept IsSpanOf =
13 std::convertible_to<T, std::span<const U>> &&
14 std::constructible_from<T, const U*, const U*>;
15
16template <typename T>
17concept IsByteSpan =
22
27template <typename LocalType, typename Value, typename Output>
28void CustomBuildField(TypeList<LocalType>, Priority<2>, InvokeContext& invoke_context, Value&& value, Output&& output)
29requires (std::is_same_v<decltype(output.get()), ::capnp::Data::Builder> && IsByteSpan<LocalType>)
30{
31 auto data = std::span{value};
32 auto result = output.init(data.size());
33 memcpy(result.begin(), data.data(), data.size());
34}
35
36template <typename LocalType, typename Input, typename ReadDest>
37decltype(auto) CustomReadField(TypeList<LocalType>, Priority<2>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest)
38requires (std::is_same_v<decltype(input.get()), ::capnp::Data::Reader> && IsByteSpan<LocalType>)
39{
40 using ByteType = decltype(std::span{std::declval<LocalType>().begin(), std::declval<LocalType>().end()})::element_type;
41 const kj::byte *begin{input.get().begin()}, *end{input.get().end()};
42 return read_dest.construct(reinterpret_cast<const ByteType*>(begin), reinterpret_cast<const ByteType*>(end));
43}
44} // namespace mp
45
46#endif // MP_PROXY_TYPE_DATA_H
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
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
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
Generic utility functions used by capnp code.
Definition: util.h:33