diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2024-02-21 07:35:38 -0500 |
---|---|---|
committer | Ryan Ofsky <ryan@ofsky.org> | 2024-02-21 07:35:38 -0500 |
commit | 8d491ae9ecf1948ea29f67b50ca7259123f602aa (patch) | |
tree | 58e9a60fb4947f32c24436204e332794e88ac3a5 | |
parent | 951203bcc496c4415b7754cd764544659b76067f (diff) | |
download | bitcoin-8d491ae9ecf1948ea29f67b50ca7259123f602aa.tar.xz |
serialization: Add ParamsStream GetStream() method
Add GetStream() method useful for accessing underlying stream. Use to improve
ParamsStream test coverage.
-rw-r--r-- | src/serialize.h | 32 | ||||
-rw-r--r-- | src/test/serialize_tests.cpp | 5 |
2 files changed, 31 insertions, 6 deletions
diff --git a/src/serialize.h b/src/serialize.h index 8bca4d8ada..d3eb1acb1f 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1103,6 +1103,10 @@ size_t GetSerializeSize(const T& t) return (SizeComputer() << t).size(); } +//! Check if type contains a stream by seeing if has a GetStream() method. +template<typename T> +concept ContainsStream = requires(T t) { t.GetStream(); }; + /** Wrapper that overrides the GetParams() function of a stream. */ template <typename SubStream, typename Params> class ParamsStream @@ -1126,11 +1130,11 @@ public: template <typename U> ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; } template <typename U> ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; } - void write(Span<const std::byte> src) { m_substream.write(src); } - void read(Span<std::byte> dst) { m_substream.read(dst); } - void ignore(size_t num) { m_substream.ignore(num); } - bool eof() const { return m_substream.eof(); } - size_t size() const { return m_substream.size(); } + void write(Span<const std::byte> src) { GetStream().write(src); } + void read(Span<std::byte> dst) { GetStream().read(dst); } + void ignore(size_t num) { GetStream().ignore(num); } + bool eof() const { return GetStream().eof(); } + size_t size() const { return GetStream().size(); } //! Get reference to stream parameters. template <typename P> @@ -1142,6 +1146,24 @@ public: return m_substream.template GetParams<P>(); } } + + //! Get reference to underlying stream. + auto& GetStream() + { + if constexpr (ContainsStream<SubStream>) { + return m_substream.GetStream(); + } else { + return m_substream; + } + } + const auto& GetStream() const + { + if constexpr (ContainsStream<SubStream>) { + return m_substream.GetStream(); + } else { + return m_substream; + } + } }; /** diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index ad4852d13d..b28e1b4196 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -412,11 +412,14 @@ BOOST_AUTO_TEST_CASE(with_params_multi) //! Test creating a ParamsStream that moves from a stream argument. BOOST_AUTO_TEST_CASE(with_params_move) { - UncopyableStream stream{}; + UncopyableStream stream{MakeByteSpan(std::string_view{"abc"})}; ParamsStream pstream{std::move(stream), RAW, HEX, RAW}; + BOOST_CHECK_EQUAL(pstream.GetStream().str(), "abc"); + pstream.GetStream().clear(); Base base1{0x20}; pstream << base1; + BOOST_CHECK_EQUAL(pstream.GetStream().str(), "\x20"); Base base2; pstream >> base2; |