Skip to content

Commit 1c6fb04

Browse files
committed
Add support for vector<bool> serialization
Needed to support vector<bool> txout_is_change field added in bitcoin-core/gui#119
1 parent aea56f0 commit 1c6fb04

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

include/mp/proxy-types.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,23 @@ decltype(auto) CustomReadField(TypeList<std::vector<LocalType>>,
362362
});
363363
}
364364

365+
template <typename Input, typename ReadDest>
366+
decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
367+
Priority<1>,
368+
InvokeContext& invoke_context,
369+
Input&& input,
370+
ReadDest&& read_dest)
371+
{
372+
return read_dest.update([&](auto& value) {
373+
auto data = input.get();
374+
value.clear();
375+
value.reserve(data.size());
376+
for (auto item : data) {
377+
value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
378+
}
379+
});
380+
}
381+
365382
template <typename LocalType, typename Input, typename ReadDest>
366383
decltype(auto) CustomReadField(TypeList<std::set<LocalType>>,
367384
Priority<1>,
@@ -820,10 +837,8 @@ void CustomBuildField(TypeList<std::vector<LocalType>>,
820837
// FIXME dedup with set handler below
821838
auto list = output.init(value.size());
822839
size_t i = 0;
823-
for (auto& elem : value) {
824-
BuildField(TypeList<LocalType>(), invoke_context, ListOutput<typename decltype(list)::Builds>(list, i),
825-
std::move(elem));
826-
++i;
840+
for (auto it = value.begin(); it != value.end(); ++it, ++i) {
841+
BuildField(TypeList<LocalType>(), invoke_context, ListOutput<typename decltype(list)::Builds>(list, i), *it);
827842
}
828843
}
829844

@@ -865,6 +880,11 @@ ::capnp::Void BuildPrimitive(InvokeContext& invoke_context, Value&&, TypeList<::
865880
return {};
866881
}
867882

883+
inline static bool BuildPrimitive(InvokeContext& invoke_context, std::vector<bool>::reference value, TypeList<bool>)
884+
{
885+
return value;
886+
}
887+
868888
template <typename LocalType, typename Value>
869889
LocalType BuildPrimitive(InvokeContext& invoke_context,
870890
const Value& value,

test/mp/test/foo.capnp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ interface ExtendedCallback extends(FooCallback) $Proxy.wrap("mp::test::ExtendedC
3737

3838
struct FooStruct $Proxy.wrap("mp::test::FooStruct") {
3939
name @0 :Text;
40+
setint @1 :List(Int32);
41+
vbool @2 :List(Bool);
4042
}
4143

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

test/mp/test/foo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <map>
99
#include <memory>
1010
#include <string>
11+
#include <set>
1112
#include <vector>
1213

1314
namespace mp {
@@ -16,7 +17,8 @@ namespace test {
1617
struct FooStruct
1718
{
1819
std::string name;
19-
std::vector<int> num_set;
20+
std::set<int> setint;
21+
std::vector<bool> vbool;
2022
};
2123

2224
struct FooCustom

test/mp/test/test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,21 @@ KJ_TEST("Call FooInterface methods")
4343

4444
FooStruct in;
4545
in.name = "name";
46+
in.setint.insert(2);
47+
in.setint.insert(1);
48+
in.vbool.push_back(false);
49+
in.vbool.push_back(true);
50+
in.vbool.push_back(false);
4651
FooStruct out = foo->pass(in);
4752
KJ_EXPECT(in.name == out.name);
53+
KJ_EXPECT(in.setint.size() == out.setint.size());
54+
for (auto init{in.setint.begin()}, outit{out.setint.begin()}; init != in.setint.end() && outit != out.setint.end(); ++init, ++outit) {
55+
KJ_EXPECT(*init == *outit);
56+
}
57+
KJ_EXPECT(in.vbool.size() == out.vbool.size());
58+
for (size_t i = 0; i < in.vbool.size(); ++i) {
59+
KJ_EXPECT(in.vbool[i] == out.vbool[i]);
60+
}
4861

4962
FooStruct err;
5063
try {

0 commit comments

Comments
 (0)