diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-06-30 12:00:20 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-06-30 12:09:45 +0200 |
commit | fac6af16f4a254458b8cb3522317422b40362f8d (patch) | |
tree | 39ca5ca662157ed3fb66105c5a3d123e3ec6a640 | |
parent | fade43edc4405e7c51cec9325d8502f3786f7438 (diff) |
Allow std::byte serialization
-rw-r--r-- | src/serialize.h | 2 | ||||
-rw-r--r-- | src/test/serialize_tests.cpp | 6 |
2 files changed, 6 insertions, 2 deletions
diff --git a/src/serialize.h b/src/serialize.h index cf865eb3f4..0cda0ac7d5 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -192,6 +192,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; } #ifndef CHAR_EQUALS_INT8 template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t #endif +template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); } template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); } @@ -207,6 +208,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { #ifndef CHAR_EQUALS_INT8 template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t #endif +template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; } template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); } template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); } diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index b445ff8ffc..2e862621bc 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -244,11 +244,13 @@ BOOST_AUTO_TEST_CASE(class_methods) { DataStream ds; const std::string in{"ab"}; - ds << Span{in}; + ds << Span{in} << std::byte{'c'}; std::array<std::byte, 2> out; - ds >> Span{out}; + std::byte out_3; + ds >> Span{out} >> out_3; BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'}); BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'}); + BOOST_CHECK_EQUAL(out_3, std::byte{'c'}); } } |