diff --git a/include/etl/byte_stream.h b/include/etl/byte_stream.h index e4f3f713..8dfc06cc 100644 --- a/include/etl/byte_stream.h +++ b/include/etl/byte_stream.h @@ -188,6 +188,38 @@ namespace etl return success; } + //*************************************************************************** + /// Write a range of T to the stream. + //*************************************************************************** + template + typename etl::enable_if::value || etl::is_floating_point::value, void>::type + write_unchecked(const T* start, size_t length) + { + while (length-- != 0U) + { + to_bytes(*start++); + } + } + + //*************************************************************************** + /// Write a range of T to the stream. + //*************************************************************************** + template + typename etl::enable_if::value || etl::is_floating_point::value, bool>::type + write(const T* start, size_t length) + { + bool success = false; + + if (available() >= length) + { + write_unchecked(start, length); + + success = true; + } + + return success; + } + //*************************************************************************** /// Sets the index back to the position in the stream. Default = 0. //*************************************************************************** @@ -290,6 +322,10 @@ namespace etl template size_t available() const { + size_t cap = capacity(); + size_t size_b = size_bytes(); + size_t size_of_T = sizeof(T); + return (capacity() - size_bytes()) / sizeof(T); } @@ -628,7 +664,10 @@ namespace etl template size_t available() const { - return (length - etl::distance(pdata, reinterpret_cast(pcurrent))) / sizeof(T); + size_t used = etl::distance(pdata, reinterpret_cast(pcurrent)); + size_t size_of_T = sizeof(T); + + return (length - used) / sizeof(T); } private: @@ -680,6 +719,7 @@ namespace etl //*************************************************************************** /// Default implementation of the write function. + /// For integral and floating point types only. /// Overload this to support custom types. //*************************************************************************** template @@ -689,8 +729,7 @@ namespace etl } //*************************************************************************** - /// Default implementation of the write function. - /// Overload this to support custom types. + /// Implementation of the write function. //*************************************************************************** template bool write(etl::byte_stream_writer& stream, const T& value) @@ -698,85 +737,25 @@ namespace etl return stream.write(value); } - //*************************************************************************** - /// Default implementation of the write function. - /// Overload this to support custom types. - //*************************************************************************** - template - void write_unchecked(etl::byte_stream_writer& stream, const etl::span& range) - { - return stream.write_unchecked(range); - } - - //*************************************************************************** - /// Default implementation of the write function. - /// Overload this to support custom types. - //*************************************************************************** - template - bool write(etl::byte_stream_writer& stream, const etl::span& range) - { - return stream.write(range); - } - //*************************************************************************** /// Default implementation of the read function. + /// For integral and floating point types only. /// Overload this to support custom types. //*************************************************************************** template - T read_unchecked(etl::byte_stream_reader& stream) + T read_unchecked(etl::byte_stream_reader& stream) { return stream.read_unchecked(); } //*************************************************************************** - /// Default implementation of the read function. - /// Overload this to support custom types. + /// Implementation of the read function. //*************************************************************************** template etl::optional read(etl::byte_stream_reader& stream) { return stream.read(); } - - //*************************************************************************** - /// Default implementation of the read function. - /// Overload this to support custom types. - //*************************************************************************** - template - etl::span read_unchecked(etl::byte_stream_reader& stream, size_t n) - { - return stream.read_unchecked(n); - } - - //*************************************************************************** - /// Default implementation of the read function. - /// Overload this to support custom types. - //*************************************************************************** - template - etl::optional > read(etl::byte_stream_reader& stream, size_t n) - { - return stream.read(n); - } - - //*************************************************************************** - /// Default implementation of the read function. - /// Overload this to support custom types. - //*************************************************************************** - template - etl::span read_unchecked(etl::byte_stream_reader& stream, etl::span range) - { - return stream.read_unchecked(range); - } - - //*************************************************************************** - /// Default implementation of the read function. - /// Overload this to support custom types. - //*************************************************************************** - template - etl::optional > read(etl::byte_stream_reader& stream, etl::span range) - { - return stream.read(range); - } } #endif diff --git a/test/test_byte_stream.cpp b/test/test_byte_stream.cpp index 50ec7241..25370b25 100644 --- a/test/test_byte_stream.cpp +++ b/test/test_byte_stream.cpp @@ -61,6 +61,15 @@ namespace namespace etl { + //*********************************** + template <> + void write_unchecked(etl::byte_stream_writer& stream, const Object& object) + { + stream.write_unchecked(object.i); + stream.write_unchecked(object.d); + stream.write_unchecked(object.c); + } + //*********************************** template <> bool write(etl::byte_stream_writer& stream, const Object& object) @@ -74,11 +83,15 @@ namespace etl //*********************************** template <> - void write_unchecked(etl::byte_stream_writer& stream, const Object& object) + Object read_unchecked(etl::byte_stream_reader& stream) { - stream.write(object.i); - stream.write(object.d); - stream.write(object.c); + int16_t i = stream.read_unchecked(); + double d = stream.read_unchecked(); + uint8_t c = stream.read_unchecked(); + + Object object{ i, d, c }; + + return object; } //*********************************** @@ -91,24 +104,7 @@ namespace etl etl::optional d = stream.read(); etl::optional c = stream.read(); - Object object { i.value(), d.value(), c.value() }; - - result = object; - - return result; - } - - //*********************************** - template <> - Object read_unchecked(etl::byte_stream_reader& stream) - { - Object result; - - int16_t i = stream.read_unchecked(); - double d = stream.read_unchecked(); - uint8_t c = stream.read_unchecked(); - - Object object{ i, d, c }; + Object object{ i.value(), d.value(), c.value() }; result = object; diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 2332fa6b..32f2956d 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -8356,6 +8356,7 @@ + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index c34db278..f78b013e 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -3035,6 +3035,9 @@ Source Files + + Source Files +