Modified read_unchecked & write_unchecked

Added start/length read/write implementations
This commit is contained in:
John Wellbelove 2021-10-05 14:24:46 +01:00
parent 4e4c781e18
commit 2d64b77e4a
4 changed files with 67 additions and 88 deletions

View File

@ -188,6 +188,38 @@ namespace etl
return success;
}
//***************************************************************************
/// Write a range of T to the stream.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::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 T>
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, bool>::type
write(const T* start, size_t length)
{
bool success = false;
if (available<T>() >= 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 <typename T>
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 <typename T>
size_t available() const
{
return (length - etl::distance(pdata, reinterpret_cast<const char* const>(pcurrent))) / sizeof(T);
size_t used = etl::distance(pdata, reinterpret_cast<const char* const>(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 <typename T>
@ -689,8 +729,7 @@ namespace etl
}
//***************************************************************************
/// Default implementation of the write function.
/// Overload this to support custom types.
/// Implementation of the write function.
//***************************************************************************
template <typename T>
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 <typename T>
void write_unchecked(etl::byte_stream_writer& stream, const etl::span<T>& range)
{
return stream.write_unchecked(range);
}
//***************************************************************************
/// Default implementation of the write function.
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
bool write(etl::byte_stream_writer& stream, const etl::span<T>& 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 <typename T>
T read_unchecked(etl::byte_stream_reader& stream)
T read_unchecked(etl::byte_stream_reader& stream)
{
return stream.read_unchecked<T>();
}
//***************************************************************************
/// Default implementation of the read function.
/// Overload this to support custom types.
/// Implementation of the read function.
//***************************************************************************
template <typename T>
etl::optional<T> read(etl::byte_stream_reader& stream)
{
return stream.read<T>();
}
//***************************************************************************
/// Default implementation of the read function.
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
etl::span<T> read_unchecked(etl::byte_stream_reader& stream, size_t n)
{
return stream.read_unchecked<T>(n);
}
//***************************************************************************
/// Default implementation of the read function.
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
etl::optional<etl::span<T> > read(etl::byte_stream_reader& stream, size_t n)
{
return stream.read<T>(n);
}
//***************************************************************************
/// Default implementation of the read function.
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
etl::span<T> read_unchecked(etl::byte_stream_reader& stream, etl::span<T> range)
{
return stream.read_unchecked<T>(range);
}
//***************************************************************************
/// Default implementation of the read function.
/// Overload this to support custom types.
//***************************************************************************
template <typename T>
etl::optional<etl::span<T> > read(etl::byte_stream_reader& stream, etl::span<T> range)
{
return stream.read<T>(range);
}
}
#endif

View File

@ -61,6 +61,15 @@ namespace
namespace etl
{
//***********************************
template <>
void write_unchecked<Object>(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<Object>(etl::byte_stream_writer& stream, const Object& object)
@ -74,11 +83,15 @@ namespace etl
//***********************************
template <>
void write_unchecked<Object>(etl::byte_stream_writer& stream, const Object& object)
Object read_unchecked<Object>(etl::byte_stream_reader& stream)
{
stream.write(object.i);
stream.write(object.d);
stream.write(object.c);
int16_t i = stream.read_unchecked<int16_t>();
double d = stream.read_unchecked<double>();
uint8_t c = stream.read_unchecked<uint8_t>();
Object object{ i, d, c };
return object;
}
//***********************************
@ -91,24 +104,7 @@ namespace etl
etl::optional<double> d = stream.read<double>();
etl::optional<uint8_t> c = stream.read<uint8_t>();
Object object { i.value(), d.value(), c.value() };
result = object;
return result;
}
//***********************************
template <>
Object read_unchecked<Object>(etl::byte_stream_reader& stream)
{
Object result;
int16_t i = stream.read_unchecked<int16_t>();
double d = stream.read_unchecked<double>();
uint8_t c = stream.read_unchecked<uint8_t>();
Object object{ i, d, c };
Object object{ i.value(), d.value(), c.value() };
result = object;

View File

@ -8356,6 +8356,7 @@
<ClCompile Include="..\test_utility.cpp" />
<ClCompile Include="..\test_variance.cpp" />
<ClCompile Include="..\test_variant_legacy.cpp" />
<ClCompile Include="..\test_variant_pool_external_buffer.cpp" />
<ClCompile Include="..\test_variant_variadic.cpp" />
<ClCompile Include="..\test_variant_pool.cpp" />
<ClCompile Include="..\test_vector.cpp" />

View File

@ -3035,6 +3035,9 @@
<ClCompile Include="..\test_multi_span.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_variant_pool_external_buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">