Skip to content

Commit

Permalink
Added missing read() member functions in byte_stream_reader
Browse files Browse the repository at this point in the history
Updated version numbers
  • Loading branch information
jwellbelove committed Oct 7, 2021
1 parent c1c5fbb commit 0d612f1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 19 deletions.
49 changes: 35 additions & 14 deletions include/etl/byte_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ namespace etl
if (available<T>() >= range.size())
{
write_unchecked(range);

success = true;
}

Expand Down Expand Up @@ -213,7 +212,6 @@ namespace etl
if (available<T>() >= length)
{
write_unchecked(start, length);

success = true;
}

Expand Down Expand Up @@ -472,7 +470,7 @@ namespace etl
// Do we have enough room?
if (available<T>() > 0U)
{
result = from_bytes<T>();
result = read_unchecked<T>();
}

return result;
Expand Down Expand Up @@ -507,10 +505,7 @@ namespace etl
// Do we have enough room?
if (available<T>() >= n)
{
const char* pend = pcurrent + (n * sizeof(T));

result = etl::span<const T>(reinterpret_cast<const T*>(pcurrent), reinterpret_cast<const T*>(pend));
pcurrent = pend;
result = read_unchecked<T>(n);
}

return result;
Expand All @@ -530,7 +525,7 @@ namespace etl
*destination++ = from_bytes<T>();
}

return etl::optional<etl::span<const T> >(etl::span<const T>(range.begin(), range.end()));
return etl::span<const T>(range.begin(), range.end());
}

//***************************************************************************
Expand All @@ -543,14 +538,40 @@ namespace etl
// Do we have enough room?
if (available<T>() >= range.size())
{
typename etl::span<T>::iterator destination = range.begin();
return etl::optional<etl::span<const T> >(read_unchecked<T>(range));
}

while (destination != range.end())
{
*destination++ = from_bytes<T>();
}
return etl::optional<etl::span<const T> >();
}

return etl::optional<etl::span<const T> >(etl::span<const T>(range.begin(), range.end()));
//***************************************************************************
/// Read a range of T from the stream.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::span<const T> >::type
read_unchecked(T* start, size_t length)
{
T* destination = start;

while (length-- != 0U)
{
*destination++ = from_bytes<T>();
}

return etl::span<const T>(start, length);
}

//***************************************************************************
/// Read a range of T from the stream.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::optional<etl::span<const T> > >::type
read(T* start, size_t length)
{
// Do we have enough room?
if (available<T>() >= length)
{
return etl::optional<etl::span<const T> >(read_unchecked<T>(start, length));
}

return etl::optional<etl::span<const T> >();
Expand Down
2 changes: 1 addition & 1 deletion include/etl/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SOFTWARE.

#define ETL_VERSION_MAJOR 20
#define ETL_VERSION_MINOR 18
#define ETL_VERSION_PATCH 0
#define ETL_VERSION_PATCH 1
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_U16 ETL_STRINGIFY(ETL_VERSION_MAJOR) u"." ETL_STRINGIFY(ETL_VERSION_MINOR) u"." ETL_STRINGIFY(ETL_VERSION_PATCH)
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ETL Embedded Template Library",
"version": "20.18.0",
"version": "20.18.1",
"author s": {
"name": "John Wellbelove",
"email": "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Embedded Template Library ETL
version=20.18.0
version=20.18.1
author= John Wellbelove <[email protected]>
maintainer=John Wellbelove <[email protected]>
license=MIT
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ project('PROJECT_NAME',
'cpp_std=c++17', 'build.cpp_std=c++17',
],
meson_version: '>=0.54.0',
version: '20.18.0'
version: '20.18.1'
)

######################
Expand Down
5 changes: 5 additions & 0 deletions support/Release notes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
===============================================================================
20.18.1
Added missing 'start, length' read() variants for byte_stream_reader.
Added multi_span to 'sanity checks'.

===============================================================================
20.18.0
Added front() access to locked queues.
Expand Down
26 changes: 25 additions & 1 deletion test/test_byte_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ namespace
}

//*************************************************************************
TEST(write_read_int32_t_range)
TEST(write_read_int32_t_span_range)
{
std::array<char, 5 * sizeof(int32_t)> storage;
std::array<int32_t, 4> put_data = { int32_t(0x00000001), int32_t(0xA55AA55A), int32_t(0x5AA55AA5), int32_t(0xFFFFFFFF) };
Expand All @@ -940,6 +940,30 @@ namespace
CHECK_EQUAL(0x12345678, i.value());
}

//*************************************************************************
TEST(write_read_int32_t_start_length_range)
{
std::array<char, 5 * sizeof(int32_t)> storage;
std::array<int32_t, 4> put_data = { int32_t(0x00000001), int32_t(0xA55AA55A), int32_t(0x5AA55AA5), int32_t(0xFFFFFFFF) };
std::array<int32_t, 4> get_data = { int32_t(0x00000000), int32_t(0x00000000), int32_t(0x00000000), int32_t(0x00000000) };

etl::byte_stream_writer writer(storage.data(), storage.size());
CHECK(writer.write(put_data.data(), put_data.size()));
CHECK(writer.write(0x12345678)); // Write an extra value.

etl::byte_stream_reader reader(storage.data(), writer.size_bytes());

etl::optional<etl::span<const int32_t> > result = reader.read<int32_t>(get_data.data(), get_data.size());
CHECK(result.has_value());
CHECK_EQUAL(put_data[0], get_data[0]);
CHECK_EQUAL(put_data[1], get_data[1]);
CHECK_EQUAL(put_data[2], get_data[2]);
CHECK_EQUAL(put_data[3], get_data[3]);

etl::optional<int32_t> i = reader.read<int32_t>(); // Read back the extra value to ensure that the current index is correct.
CHECK_EQUAL(0x12345678, i.value());
}

//*************************************************************************
TEST(write_read_multiple)
{
Expand Down

0 comments on commit 0d612f1

Please sign in to comment.