mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
multiprocess: Add serialization code for vector<char>
This commit is contained in:
parent
095286f790
commit
06882f8401
4 changed files with 33 additions and 0 deletions
|
@ -112,6 +112,33 @@ decltype(auto) CustomReadField(TypeList<UniValue>, Priority<1>, InvokeContext& i
|
|||
value.read(std::string_view{data.begin(), data.size()});
|
||||
});
|
||||
}
|
||||
|
||||
//! Generic ::capnp::Data field builder for any C++ type that can be converted
|
||||
//! to a span of bytes, like std::vector<char> or std::array<uint8_t>, or custom
|
||||
//! blob types like uint256 or PKHash with data() and size() methods pointing to
|
||||
//! bytes.
|
||||
//!
|
||||
//! Note: it might make sense to move this function into libmultiprocess, since
|
||||
//! it is fairly generic. However this would require decreasing its priority so
|
||||
//! it can be overridden, which would require more changes inside
|
||||
//! libmultiprocess to avoid conflicting with the Priority<1> CustomBuildField
|
||||
//! function it already provides for std::vector. Also, it might make sense to
|
||||
//! provide a CustomReadField counterpart to this function, which could be
|
||||
//! called to read C++ types that can be constructed from spans of bytes from
|
||||
//! ::capnp::Data fields. But so far there hasn't been a need for this.
|
||||
template <typename LocalType, typename Value, typename Output>
|
||||
void CustomBuildField(TypeList<LocalType>, Priority<2>, InvokeContext& invoke_context, Value&& value, Output&& output)
|
||||
requires
|
||||
(std::is_same_v<decltype(output.get()), ::capnp::Data::Builder>) &&
|
||||
(std::convertible_to<Value, std::span<const std::byte>> ||
|
||||
std::convertible_to<Value, std::span<const char>> ||
|
||||
std::convertible_to<Value, std::span<const unsigned char>> ||
|
||||
std::convertible_to<Value, std::span<const signed char>>)
|
||||
{
|
||||
auto data = std::span{value};
|
||||
auto result = output.init(data.size());
|
||||
memcpy(result.begin(), data.data(), data.size());
|
||||
}
|
||||
} // namespace mp
|
||||
|
||||
#endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H
|
||||
|
|
|
@ -16,4 +16,5 @@ interface FooInterface $Proxy.wrap("FooImplementation") {
|
|||
passOutPoint @1 (arg :Data) -> (result :Data);
|
||||
passUniValue @2 (arg :Text) -> (result :Text);
|
||||
passTransaction @3 (arg :Data) -> (result :Data);
|
||||
passVectorChar @4 (arg :Data) -> (result :Data);
|
||||
}
|
||||
|
|
|
@ -97,6 +97,10 @@ void IpcPipeTest()
|
|||
CTransactionRef tx2{foo->passTransaction(tx1)};
|
||||
BOOST_CHECK(*Assert(tx1) == *Assert(tx2));
|
||||
|
||||
std::vector<char> vec1{'H', 'e', 'l', 'l', 'o'};
|
||||
std::vector<char> vec2{foo->passVectorChar(vec1)};
|
||||
BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
|
||||
|
||||
// Test cleanup: disconnect pipe and join thread
|
||||
disconnect_client();
|
||||
thread.join();
|
||||
|
|
|
@ -16,6 +16,7 @@ public:
|
|||
COutPoint passOutPoint(COutPoint o) { return o; }
|
||||
UniValue passUniValue(UniValue v) { return v; }
|
||||
CTransactionRef passTransaction(CTransactionRef t) { return t; }
|
||||
std::vector<char> passVectorChar(std::vector<char> v) { return v; }
|
||||
};
|
||||
|
||||
void IpcPipeTest();
|
||||
|
|
Loading…
Add table
Reference in a new issue