Skip to content

Commit e1914bb

Browse files
type: add std::unordered_set support
1 parent 5ddf554 commit e1914bb

5 files changed

Lines changed: 53 additions & 0 deletions

File tree

include/mp/type-unordered-set.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_UNORDERED_SET_H
6+
#define MP_PROXY_TYPE_UNORDERED_SET_H
7+
8+
#include <mp/proxy-types.h>
9+
#include <mp/util.h>
10+
#include <unordered_set>
11+
12+
namespace mp {
13+
template <typename LocalType, typename Value, typename Output>
14+
void CustomBuildField(TypeList<std::unordered_set<LocalType>>,
15+
Priority<1>,
16+
InvokeContext& invoke_context,
17+
Value&& value,
18+
Output&& output)
19+
{
20+
BuildList(TypeList<LocalType>(), invoke_context, output, value);
21+
}
22+
23+
template <typename LocalType, typename Input, typename ReadDest>
24+
decltype(auto) CustomReadField(TypeList<std::unordered_set<LocalType>>,
25+
Priority<1>,
26+
InvokeContext& invoke_context,
27+
Input&& input,
28+
ReadDest&& read_dest)
29+
{
30+
return read_dest.update([&](auto& value) {
31+
auto data = input.get();
32+
value.clear();
33+
for (auto item : data) {
34+
ReadField(TypeList<LocalType>(), invoke_context, Make<ValueField>(item),
35+
ReadDestEmplace(TypeList<const LocalType>(), [&](auto&&... args) -> auto& {
36+
return *value.emplace(std::forward<decltype(args)>(args)...).first;
37+
}));
38+
}
39+
});
40+
}
41+
} // namespace mp
42+
43+
#endif // MP_PROXY_TYPE_UNORDERED_SET_H

test/mp/test/foo-types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <mp/type-string.h>
2626
#include <mp/type-struct.h>
2727
#include <mp/type-threadmap.h>
28+
#include <mp/type-unordered-set.h>
2829
#include <mp/type-vector.h>
2930
#include <string>
3031
#include <type_traits>

test/mp/test/foo.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct FooStruct $Proxy.wrap("mp::test::FooStruct") {
5555
name @0 :Text;
5656
setint @1 :List(Int32);
5757
vbool @2 :List(Bool);
58+
unorderedsetint @3 :List(Int32);
5859
}
5960

6061
struct FooCustom $Proxy.wrap("mp::test::FooCustom") {

test/mp/test/foo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212
#include <string>
1313
#include <set>
14+
#include <unordered_set>
1415
#include <vector>
1516

1617
namespace mp {
@@ -21,6 +22,7 @@ struct FooStruct
2122
std::string name;
2223
std::set<int> setint;
2324
std::vector<bool> vbool;
25+
std::unordered_set<int> unorderedsetint;
2426
};
2527

2628
enum class FooEnum : uint8_t { ONE = 1, TWO = 2, };

test/mp/test/test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ KJ_TEST("Call FooInterface methods")
143143
in.name = "name";
144144
in.setint.insert(2);
145145
in.setint.insert(1);
146+
in.unorderedsetint.insert(2);
147+
in.unorderedsetint.insert(1);
146148
in.vbool.push_back(false);
147149
in.vbool.push_back(true);
148150
in.vbool.push_back(false);
@@ -152,6 +154,10 @@ KJ_TEST("Call FooInterface methods")
152154
for (auto init{in.setint.begin()}, outit{out.setint.begin()}; init != in.setint.end() && outit != out.setint.end(); ++init, ++outit) {
153155
KJ_EXPECT(*init == *outit);
154156
}
157+
KJ_EXPECT(in.unorderedsetint.size() == out.unorderedsetint.size());
158+
for (const auto& elem : in.unorderedsetint) {
159+
KJ_EXPECT(out.unorderedsetint.count(elem) == 1);
160+
}
155161
KJ_EXPECT(in.vbool.size() == out.vbool.size());
156162
for (size_t i = 0; i < in.vbool.size(); ++i) {
157163
KJ_EXPECT(in.vbool[i] == out.vbool[i]);

0 commit comments

Comments
 (0)