Bitcoin Core  27.99.0
P2P Digital Currency
common-types.h
Go to the documentation of this file.
1 // Copyright (c) 2023 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 BITCOIN_IPC_CAPNP_COMMON_TYPES_H
6 #define BITCOIN_IPC_CAPNP_COMMON_TYPES_H
7 
8 #include <clientversion.h>
9 #include <streams.h>
10 #include <univalue.h>
11 
12 #include <cstddef>
13 #include <mp/proxy-types.h>
14 #include <type_traits>
15 #include <utility>
16 
17 namespace ipc {
18 namespace capnp {
21 template <typename T>
22 struct Serializable {
23 private:
24  template <typename C>
25  static std::true_type test(decltype(std::declval<C>().Serialize(std::declval<std::nullptr_t&>()))*);
26  template <typename>
27  static std::false_type test(...);
28 
29 public:
30  static constexpr bool value = decltype(test<T>(nullptr))::value;
31 };
32 
35 template <typename T>
37 private:
38  template <typename C>
39  static std::true_type test(decltype(std::declval<C>().Unserialize(std::declval<std::nullptr_t&>()))*);
40  template <typename>
41  static std::false_type test(...);
42 
43 public:
44  static constexpr bool value = decltype(test<T>(nullptr))::value;
45 };
46 } // namespace capnp
47 } // namespace ipc
48 
50 namespace mp {
55 template <typename LocalType, typename Value, typename Output>
57  TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output,
58  // Enable if serializeable and if LocalType is not cv or reference
59  // qualified. If LocalType is cv or reference qualified, it is important to
60  // fall back to lower-priority Priority<0> implementation of this function
61  // that strips cv references, to prevent this CustomBuildField overload from
62  // taking precedence over more narrow overloads for specific LocalTypes.
64  std::is_same_v<LocalType, std::remove_cv_t<std::remove_reference_t<LocalType>>>>* enable = nullptr)
65 {
66  DataStream stream;
67  value.Serialize(stream);
68  auto result = output.init(stream.size());
69  memcpy(result.begin(), stream.data(), stream.size());
70 }
71 
76 template <typename LocalType, typename Input, typename ReadDest>
77 decltype(auto)
78 CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest,
79  std::enable_if_t<ipc::capnp::Unserializable<LocalType>::value>* enable = nullptr)
80 {
81  return read_dest.update([&](auto& value) {
82  if (!input.has()) return;
83  auto data = input.get();
84  SpanReader stream({data.begin(), data.end()});
85  value.Unserialize(stream);
86  });
87 }
88 
89 template <typename Value, typename Output>
90 void CustomBuildField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output)
91 {
92  std::string str = value.write();
93  auto result = output.init(str.size());
94  memcpy(result.begin(), str.data(), str.size());
95 }
96 
97 template <typename Input, typename ReadDest>
98 decltype(auto) CustomReadField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Input&& input,
99  ReadDest&& read_dest)
100 {
101  return read_dest.update([&](auto& value) {
102  auto data = input.get();
103  value.read(std::string_view{data.begin(), data.size()});
104  });
105 }
106 } // namespace mp
107 
108 #endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
size_type size() const
Definition: streams.h:181
value_type * data()
Definition: streams.h:188
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:101
Definition: ipc.h:12
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:50
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest, std::enable_if_t< ipc::capnp::Unserializable< LocalType >::value > *enable=nullptr)
Overload multiprocess library's CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:78
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output, std::enable_if_t< ipc::capnp::Serializable< LocalType >::value &&std::is_same_v< LocalType, std::remove_cv_t< std::remove_reference_t< LocalType >>>> *enable=nullptr)
Overload multiprocess library's CustomBuildField hook to allow any serializable object to be stored i...
Definition: common-types.h:56
void Serialize(Stream &, V)=delete
void Unserialize(Stream &, V)=delete
concept Unserializable
Definition: serialize.h:756
Use SFINAE to define Serializeable<T> trait which is true if type T has a Serialize(stream) method,...
Definition: common-types.h:22
static std::true_type test(decltype(std::declval< C >().Serialize(std::declval< std::nullptr_t & >())) *)
static std::false_type test(...)
static constexpr bool value
Definition: common-types.h:30
Use SFINAE to define Unserializeable<T> trait which is true if type T has an Unserialize(stream) meth...
Definition: common-types.h:36
static std::false_type test(...)
static constexpr bool value
Definition: common-types.h:44
static std::true_type test(decltype(std::declval< C >().Unserialize(std::declval< std::nullptr_t & >())) *)