Bitcoin Core  31.0.0
P2P Digital Currency
type-map.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_MAP_H
6 #define MP_PROXY_TYPE_MAP_H
7 
8 #include <mp/proxy-types.h>
9 #include <mp/type-pair.h>
10 #include <mp/util.h>
11 
12 namespace mp {
13 template <typename KeyLocalType, typename ValueLocalType, typename Value, typename Output>
14 void CustomBuildField(TypeList<std::map<KeyLocalType, ValueLocalType>>,
16  InvokeContext& invoke_context,
17  Value&& value,
18  Output&& output)
19 {
20  // FIXME dededup with vector handler above
21  auto list = output.init(value.size());
22  size_t i = 0;
23  for (const auto& elem : value) {
24  BuildField(TypeList<std::pair<KeyLocalType, ValueLocalType>>(), invoke_context,
25  ListOutput<typename decltype(list)::Builds>(list, i), elem);
26  ++i;
27  }
28 }
29 
30 // Replacement for `m.emplace(piecewise_construct, t1, t2)` to work around a
31 // Clang 22 regression triggered by libc++'s std::map piecewise emplace: when
32 // the key constructor argument tuple is empty (std::tuple<>), libc++'s internal
33 // "try key extraction" SFINAE probe instantiates std::tuple_element<0,
34 // std::tuple<>>, which Clang 22 diagnoses as an out-of-bounds pack access ("a
35 // parameter pack may not be accessed at an out of bounds index") instead of
36 // treating it as substitution failure. See LLVM issue #167709 and the upstream
37 // fix in llvm/llvm-project PR #183614.
38 // https://github.com/llvm/llvm-project/issues/167709
39 // https://github.com/llvm/llvm-project/pull/183614
40 template <class Map, class Tuple1, class Tuple2>
42  Map& m,
43  const std::piecewise_construct_t&,
44  Tuple1&& t1,
45  Tuple2&& t2)
46 {
47  if constexpr (std::tuple_size_v<std::remove_reference_t<Tuple1>> == 0) {
48  // Avoid tuple<> / tuple<> (LLVM 22 libc++ regression path)
49  return m.emplace(std::piecewise_construct,
50  std::forward_as_tuple(typename Map::key_type{}),
51  std::forward<Tuple2>(t2));
52  } else {
53  return m.emplace(std::piecewise_construct,
54  std::forward<Tuple1>(t1),
55  std::forward<Tuple2>(t2));
56  }
57 }
58 
59 template <typename KeyLocalType, typename ValueLocalType, typename Input, typename ReadDest>
60 decltype(auto) CustomReadField(TypeList<std::map<KeyLocalType, ValueLocalType>>,
61  Priority<1>,
62  InvokeContext& invoke_context,
63  Input&& input,
64  ReadDest&& read_dest)
65 {
66  return read_dest.update([&](auto& value) {
67  auto data = input.get();
68  value.clear();
69  for (auto item : data) {
70  ReadField(TypeList<std::pair<const KeyLocalType, ValueLocalType>>(), invoke_context,
71  Make<ValueField>(item),
73  TypeList<std::pair<const KeyLocalType, ValueLocalType>>(), [&](auto&&... args) -> auto& {
74  return *EmplacePiecewiseSafe(value, std::forward<decltype(args)>(args)...).first;
75  }));
76  }
77  });
78 }
79 } // namespace mp
80 
81 #endif // MP_PROXY_TYPE_MAP_H
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
Definition: util.h:108
Definition: common.h:29
Generic utility functions used by capnp code.
Definition: util.h:32
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
ArgsManager & args
Definition: bitcoind.cpp:277
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) 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...
decltype(auto) ReadField(TypeList< LocalTypes... >, InvokeContext &invoke_context, Input &&input, Args &&... args)
Definition: proxy-types.h:213
auto EmplacePiecewiseSafe(Map &m, const std::piecewise_construct_t &, Tuple1 &&t1, Tuple2 &&t2)
Definition: type-map.h:41