Skip to content

Commit 11b418f

Browse files
committed
moveonly: add mp/type-struct.h
1 parent 5df55a3 commit 11b418f

File tree

4 files changed

+87
-73
lines changed

4 files changed

+87
-73
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(MP_PUBLIC_HEADERS
5656
include/mp/type-pointer.h
5757
include/mp/type-set.h
5858
include/mp/type-string.h
59+
include/mp/type-struct.h
5960
include/mp/type-tuple.h
6061
include/mp/type-vector.h
6162
include/mp/util.h)

include/mp/proxy-types.h

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -141,43 +141,6 @@ struct ReadDestUpdate
141141
Value& m_value;
142142
};
143143

144-
template <size_t index, typename LocalType, typename Input, typename Value>
145-
void ReadOne(TypeList<LocalType> param,
146-
InvokeContext& invoke_context,
147-
Input&& input,
148-
Value&& value,
149-
typename std::enable_if<index != ProxyType<LocalType>::fields>::type* enable = nullptr)
150-
{
151-
using Index = std::integral_constant<size_t, index>;
152-
using Struct = typename ProxyType<LocalType>::Struct;
153-
using Accessor = typename std::tuple_element<index, typename ProxyStruct<Struct>::Accessors>::type;
154-
const auto& struc = input.get();
155-
auto&& field_value = value.*ProxyType<LocalType>::get(Index());
156-
ReadField(TypeList<RemoveCvRef<decltype(field_value)>>(), invoke_context, Make<StructField, Accessor>(struc),
157-
ReadDestUpdate(field_value));
158-
ReadOne<index + 1>(param, invoke_context, input, value);
159-
}
160-
161-
template <size_t index, typename LocalType, typename Input, typename Value>
162-
void ReadOne(TypeList<LocalType> param,
163-
InvokeContext& invoke_context,
164-
Input& input,
165-
Value& value,
166-
typename std::enable_if<index == ProxyType<LocalType>::fields>::type* enable = nullptr)
167-
{
168-
}
169-
170-
template <typename LocalType, typename Input, typename ReadDest>
171-
decltype(auto) CustomReadField(TypeList<LocalType> param,
172-
Priority<1>,
173-
InvokeContext& invoke_context,
174-
Input&& input,
175-
ReadDest&& read_dest,
176-
typename ProxyType<LocalType>::Struct* enable = nullptr)
177-
{
178-
return read_dest.update([&](auto& value) { ReadOne<0>(param, invoke_context, input, value); });
179-
}
180-
181144
//! Overload CustomReadField to serialize objects that have CustomReadMessage
182145
//! overloads. Defining a CustomReadMessage overload is simpler than defining a
183146
//! CustomReadField overload because it only requires defining a normal
@@ -317,42 +280,6 @@ void CustomBuildField(TypeList<LocalType>, Priority<0>, InvokeContext& invoke_co
317280
output.set(BuildPrimitive(invoke_context, std::forward<Value>(value), TypeList<decltype(output.get())>()));
318281
}
319282

320-
template <size_t index, typename LocalType, typename Value, typename Output>
321-
void BuildOne(TypeList<LocalType> param,
322-
InvokeContext& invoke_context,
323-
Output&& output,
324-
Value&& value,
325-
typename std::enable_if < index<ProxyType<LocalType>::fields>::type * enable = nullptr)
326-
{
327-
using Index = std::integral_constant<size_t, index>;
328-
using Struct = typename ProxyType<LocalType>::Struct;
329-
using Accessor = typename std::tuple_element<index, typename ProxyStruct<Struct>::Accessors>::type;
330-
auto&& field_output = Make<StructField, Accessor>(output);
331-
auto&& field_value = value.*ProxyType<LocalType>::get(Index());
332-
BuildField(TypeList<Decay<decltype(field_value)>>(), invoke_context, field_output, field_value);
333-
BuildOne<index + 1>(param, invoke_context, output, value);
334-
}
335-
336-
template <size_t index, typename LocalType, typename Value, typename Output>
337-
void BuildOne(TypeList<LocalType> param,
338-
InvokeContext& invoke_context,
339-
Output&& output,
340-
Value&& value,
341-
typename std::enable_if<index == ProxyType<LocalType>::fields>::type* enable = nullptr)
342-
{
343-
}
344-
345-
template <typename LocalType, typename Value, typename Output>
346-
void CustomBuildField(TypeList<LocalType> local_type,
347-
Priority<1>,
348-
InvokeContext& invoke_context,
349-
Value&& value,
350-
Output&& output,
351-
typename ProxyType<LocalType>::Struct* enable = nullptr)
352-
{
353-
BuildOne<0>(local_type, invoke_context, output.init(), value);
354-
}
355-
356283
//! PassField override for callable interface reference arguments.
357284
template <typename Accessor, typename LocalType, typename ServerContext, typename Fn, typename... Args>
358285
auto PassField(Priority<1>, TypeList<LocalType&>, ServerContext& server_context, Fn&& fn, Args&&... args)

include/mp/type-struct.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_STRUCT_H
6+
#define MP_PROXY_TYPE_STRUCT_H
7+
8+
#include <mp/util.h>
9+
10+
namespace mp {
11+
template <size_t index, typename LocalType, typename Value, typename Output>
12+
void BuildOne(TypeList<LocalType> param,
13+
InvokeContext& invoke_context,
14+
Output&& output,
15+
Value&& value,
16+
typename std::enable_if < index<ProxyType<LocalType>::fields>::type * enable = nullptr)
17+
{
18+
using Index = std::integral_constant<size_t, index>;
19+
using Struct = typename ProxyType<LocalType>::Struct;
20+
using Accessor = typename std::tuple_element<index, typename ProxyStruct<Struct>::Accessors>::type;
21+
auto&& field_output = Make<StructField, Accessor>(output);
22+
auto&& field_value = value.*ProxyType<LocalType>::get(Index());
23+
BuildField(TypeList<Decay<decltype(field_value)>>(), invoke_context, field_output, field_value);
24+
BuildOne<index + 1>(param, invoke_context, output, value);
25+
}
26+
27+
template <size_t index, typename LocalType, typename Value, typename Output>
28+
void BuildOne(TypeList<LocalType> param,
29+
InvokeContext& invoke_context,
30+
Output&& output,
31+
Value&& value,
32+
typename std::enable_if<index == ProxyType<LocalType>::fields>::type* enable = nullptr)
33+
{
34+
}
35+
36+
template <typename LocalType, typename Value, typename Output>
37+
void CustomBuildField(TypeList<LocalType> local_type,
38+
Priority<1>,
39+
InvokeContext& invoke_context,
40+
Value&& value,
41+
Output&& output,
42+
typename ProxyType<LocalType>::Struct* enable = nullptr)
43+
{
44+
BuildOne<0>(local_type, invoke_context, output.init(), value);
45+
}
46+
47+
template <size_t index, typename LocalType, typename Input, typename Value>
48+
void ReadOne(TypeList<LocalType> param,
49+
InvokeContext& invoke_context,
50+
Input&& input,
51+
Value&& value,
52+
typename std::enable_if<index != ProxyType<LocalType>::fields>::type* enable = nullptr)
53+
{
54+
using Index = std::integral_constant<size_t, index>;
55+
using Struct = typename ProxyType<LocalType>::Struct;
56+
using Accessor = typename std::tuple_element<index, typename ProxyStruct<Struct>::Accessors>::type;
57+
const auto& struc = input.get();
58+
auto&& field_value = value.*ProxyType<LocalType>::get(Index());
59+
ReadField(TypeList<RemoveCvRef<decltype(field_value)>>(), invoke_context, Make<StructField, Accessor>(struc),
60+
ReadDestUpdate(field_value));
61+
ReadOne<index + 1>(param, invoke_context, input, value);
62+
}
63+
64+
template <size_t index, typename LocalType, typename Input, typename Value>
65+
void ReadOne(TypeList<LocalType> param,
66+
InvokeContext& invoke_context,
67+
Input& input,
68+
Value& value,
69+
typename std::enable_if<index == ProxyType<LocalType>::fields>::type* enable = nullptr)
70+
{
71+
}
72+
73+
template <typename LocalType, typename Input, typename ReadDest>
74+
decltype(auto) CustomReadField(TypeList<LocalType> param,
75+
Priority<1>,
76+
InvokeContext& invoke_context,
77+
Input&& input,
78+
ReadDest&& read_dest,
79+
typename ProxyType<LocalType>::Struct* enable = nullptr)
80+
{
81+
return read_dest.update([&](auto& value) { ReadOne<0>(param, invoke_context, input, value); });
82+
}
83+
} // namespace mp
84+
85+
#endif // MP_PROXY_TYPE_STRUCT_H

test/mp/test/foo-types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <mp/type-number.h>
1313
#include <mp/type-set.h>
1414
#include <mp/type-string.h>
15+
#include <mp/type-struct.h>
1516
#include <mp/type-vector.h>
1617

1718
namespace mp {

0 commit comments

Comments
 (0)