Bitcoin Core  31.0.0
P2P Digital Currency
type-data.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_DATA_H
6 #define MP_PROXY_TYPE_DATA_H
7 
8 #include <mp/util.h>
9 
10 #include <concepts>
11 #include <span>
12 
13 namespace mp {
14 template <typename T, typename U>
15 concept IsSpanOf =
16  std::convertible_to<T, std::span<const U>> &&
17  std::constructible_from<T, const U*, const U*>;
18 
19 template <typename T>
20 concept IsByteSpan =
21  IsSpanOf<T, std::byte> ||
22  IsSpanOf<T, char> ||
23  IsSpanOf<T, unsigned char> ||
24  IsSpanOf<T, signed char>;
25 
30 template <typename LocalType, typename Value, typename Output>
31 void CustomBuildField(TypeList<LocalType>, Priority<2>, InvokeContext& invoke_context, Value&& value, Output&& output)
32 requires (std::is_same_v<decltype(output.get()), ::capnp::Data::Builder> && IsByteSpan<LocalType>)
33 {
34  auto data = std::span{value};
35  auto result = output.init(data.size());
36  memcpy(result.begin(), data.data(), data.size());
37 }
38 
39 template <typename LocalType, typename Input, typename ReadDest>
40 decltype(auto) CustomReadField(TypeList<LocalType>, Priority<2>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest)
41 requires (std::is_same_v<decltype(input.get()), ::capnp::Data::Reader> && IsByteSpan<LocalType>)
42 {
43  using ByteType = decltype(std::span{std::declval<LocalType>().begin(), std::declval<LocalType>().end()})::element_type;
44  const kj::byte *begin{input.get().begin()}, *end{input.get().end()};
45  return read_dest.construct(reinterpret_cast<const ByteType*>(begin), reinterpret_cast<const ByteType*>(end));
46 }
47 } // namespace mp
48 
49 #endif // MP_PROXY_TYPE_DATA_H
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
Definition: util.h:108
Definition: common.h:29
memcpy(result.begin(), stream.data(), stream.size())
Generic utility functions used by capnp code.
Definition: util.h:32
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
auto result
Definition: common-types.h:74
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest) requires Unserializable< LocalType
Overload multiprocess library&#39;s CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:100
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output) requires Serializable< LocalType
Overload multiprocess library&#39;s CustomBuildField hook to allow any serializable object to be stored i...
concept IsSpanOf
Definition: type-data.h:15
concept IsByteSpan
Definition: type-data.h:20
concept ByteType
Definition: common.h:16