Skip to content

Commit c999100

Browse files
committed
moveonly: add mp/type-pair.h
1 parent 079277f commit c999100

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ set(MP_PUBLIC_HEADERS
4848
include/mp/type-context.h
4949
include/mp/type-map.h
5050
include/mp/type-optional.h
51+
include/mp/type-pair.h
5152
include/mp/type-pointer.h
5253
include/mp/type-set.h
5354
include/mp/type-vector.h

include/mp/proxy-types.h

-38
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,6 @@ struct ReadDestUpdate
143143

144144

145145

146-
template <typename KeyLocalType, typename ValueLocalType, typename Input, typename ReadDest>
147-
decltype(auto) CustomReadField(TypeList<std::pair<KeyLocalType, ValueLocalType>>,
148-
Priority<1>,
149-
InvokeContext& invoke_context,
150-
Input&& input,
151-
ReadDest&& read_dest)
152-
{
153-
const auto& pair = input.get();
154-
using Accessors = typename ProxyStruct<typename Decay<decltype(pair)>::Reads>::Accessors;
155-
156-
ReadField(TypeList<KeyLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<0, Accessors>>(pair),
157-
ReadDestEmplace(TypeList<KeyLocalType>(), [&](auto&&... key_args) -> auto& {
158-
KeyLocalType* key = nullptr;
159-
ReadField(TypeList<ValueLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<1, Accessors>>(pair),
160-
ReadDestEmplace(TypeList<ValueLocalType>(), [&](auto&&... value_args) -> auto& {
161-
auto& ret = read_dest.construct(std::piecewise_construct, std::forward_as_tuple(key_args...),
162-
std::forward_as_tuple(value_args...));
163-
key = &ret.first;
164-
return ret.second;
165-
}));
166-
return *key;
167-
}));
168-
}
169146

170147
// TODO: Should generalize this to work with arbitrary length tuples, not just length 2-tuples.
171148
template <typename KeyLocalType, typename ValueLocalType, typename Input, typename ReadDest>
@@ -607,21 +584,6 @@ void CustomBuildField(TypeList<std::exception>,
607584
{
608585
BuildField(TypeList<std::string>(), invoke_context, output, std::string(value.what()));
609586
}
610-
611-
// FIXME: Overload on output type instead of value type and switch to std::get and merge with next overload
612-
template <typename KeyLocalType, typename ValueLocalType, typename Value, typename Output>
613-
void CustomBuildField(TypeList<std::pair<KeyLocalType, ValueLocalType>>,
614-
Priority<1>,
615-
InvokeContext& invoke_context,
616-
Value&& value,
617-
Output&& output)
618-
{
619-
auto pair = output.init();
620-
using Accessors = typename ProxyStruct<typename decltype(pair)::Builds>::Accessors;
621-
BuildField(TypeList<KeyLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<0, Accessors>>(pair), value.first);
622-
BuildField(TypeList<ValueLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<1, Accessors>>(pair), value.second);
623-
}
624-
625587
// TODO: Should generalize this to work with arbitrary length tuples, not just length 2-tuples.
626588
template <typename KeyLocalType, typename ValueLocalType, typename Value, typename Output>
627589
void CustomBuildField(TypeList<std::tuple<KeyLocalType, ValueLocalType>>,

include/mp/type-map.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define MP_PROXY_TYPE_MAP_H
77

88
#include <mp/proxy-types.h>
9+
#include <mp/type-pair.h>
910
#include <mp/util.h>
1011

1112
namespace mp {

include/mp/type-pair.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_PAIR_H
6+
#define MP_PROXY_TYPE_PAIR_H
7+
8+
#include <mp/util.h>
9+
10+
namespace mp {
11+
// FIXME: Overload on output type instead of value type and switch to std::get and merge with next overload
12+
template <typename KeyLocalType, typename ValueLocalType, typename Value, typename Output>
13+
void CustomBuildField(TypeList<std::pair<KeyLocalType, ValueLocalType>>,
14+
Priority<1>,
15+
InvokeContext& invoke_context,
16+
Value&& value,
17+
Output&& output)
18+
{
19+
auto pair = output.init();
20+
using Accessors = typename ProxyStruct<typename decltype(pair)::Builds>::Accessors;
21+
BuildField(TypeList<KeyLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<0, Accessors>>(pair), value.first);
22+
BuildField(TypeList<ValueLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<1, Accessors>>(pair), value.second);
23+
}
24+
25+
template <typename KeyLocalType, typename ValueLocalType, typename Input, typename ReadDest>
26+
decltype(auto) CustomReadField(TypeList<std::pair<KeyLocalType, ValueLocalType>>,
27+
Priority<1>,
28+
InvokeContext& invoke_context,
29+
Input&& input,
30+
ReadDest&& read_dest)
31+
{
32+
const auto& pair = input.get();
33+
using Accessors = typename ProxyStruct<typename Decay<decltype(pair)>::Reads>::Accessors;
34+
35+
ReadField(TypeList<KeyLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<0, Accessors>>(pair),
36+
ReadDestEmplace(TypeList<KeyLocalType>(), [&](auto&&... key_args) -> auto& {
37+
KeyLocalType* key = nullptr;
38+
ReadField(TypeList<ValueLocalType>(), invoke_context, Make<StructField, std::tuple_element_t<1, Accessors>>(pair),
39+
ReadDestEmplace(TypeList<ValueLocalType>(), [&](auto&&... value_args) -> auto& {
40+
auto& ret = read_dest.construct(std::piecewise_construct, std::forward_as_tuple(key_args...),
41+
std::forward_as_tuple(value_args...));
42+
key = &ret.first;
43+
return ret.second;
44+
}));
45+
return *key;
46+
}));
47+
}
48+
} // namespace mp
49+
50+
#endif // MP_PROXY_TYPE_PAIR_H

0 commit comments

Comments
 (0)