Permalink
Browse files

Merge pull request #25 from steinwurf/add-read-peek-return-value

Add read peek return value
  • Loading branch information...
janusheide committed Nov 8, 2017
2 parents 40f1791 + a591f2e commit 40e7d1556dcbc3c6595bcfeb64714765137051ff
Showing with 65 additions and 14 deletions.
  1. +1 −1 NEWS.rst
  2. +46 −12 src/endian/stream_reader.hpp
  3. +18 −1 test/src/test_stream_reader.cpp
View
@@ -6,7 +6,7 @@ every change, see the Git log.
Latest
------
* tbd
* Minor: Added ValueType read() and ValueType peek().
8.0.1
-----
@@ -47,8 +47,8 @@ class stream_reader : public stream
template<uint8_t Bytes, class ValueType>
void read_bytes(ValueType& value)
{
// Make sure there is enough data to read in the underlying buffer
assert(Bytes <= remaining_size());
assert(sizeof(ValueType) <= remaining_size() &&
"Reading over the end of the underlying buffer");
// Get the value at the current position
peek_bytes<Bytes, ValueType>(value);
@@ -64,11 +64,27 @@ class stream_reader : public stream
template<class ValueType>
void read(ValueType& value)
{
// Make sure there is enough data to read in the underlying buffer
assert(sizeof(ValueType) <= remaining_size());
assert(sizeof(ValueType) <= remaining_size() &&
"Reading over the end of the underlying buffer");
read_bytes<sizeof(ValueType), ValueType>(value);
}
/// Reads a ValueType-sized integer from the stream and moves the read
/// position.
///
/// @return the read value
template<class ValueType>
ValueType read()
{
assert(sizeof(ValueType) <= remaining_size() &&
"Reading over the end of the underlying buffer");
ValueType value;
read(value);
return value;
}
/// Reads raw bytes from the stream to fill a buffer represented by
/// a mutable storage object.
///
@@ -79,8 +95,8 @@ class stream_reader : public stream
/// @param size The number of bytes to fill.
void read(uint8_t* data, uint64_t size)
{
// Make sure there is enough data to read in the underlying buffer
assert(size <= remaining_size());
assert(size <= remaining_size() &&
"Reading over the end of the underlying buffer");
// Copy the data from the buffer to the storage
std::copy_n(remaining_data(), (std::size_t)size, data);
@@ -97,9 +113,9 @@ class stream_reader : public stream
template<uint8_t Bytes, class ValueType>
void peek_bytes(ValueType& value, uint64_t offset=0) const
{
assert(remaining_size() >= offset);
// Make sure there is enough data to read in the underlying buffer
assert(Bytes <= remaining_size() - offset);
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
const uint8_t* data_position = remaining_data() + offset;
// Get the value at the current position
@@ -114,12 +130,30 @@ class stream_reader : public stream
template<class ValueType>
void peek(ValueType& value, uint64_t offset=0) const
{
assert(remaining_size() >= offset);
// Make sure there is enough data to read in the underlying buffer
assert(sizeof(ValueType) <= remaining_size() - offset);
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
peek_bytes<sizeof(ValueType), ValueType>(value, offset);
}
/// Peek a ValueType-sized integer in the stream without moving the read
/// position
///
/// @param offset number of bytes to offset the peeking with
/// @return the peeked value
template<class ValueType>
ValueType peek(uint64_t offset=0) const
{
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
ValueType value;
peek(value, offset);
return value;
}
/// A pointer to the stream's data.
///
/// @return pointer to the stream's data.
@@ -113,7 +113,7 @@ static void test_basic_api()
}
{
SCOPED_TRACE(testing::Message() << "peek");
SCOPED_TRACE(testing::Message() << "peek_read");
std::vector<uint8_t> buffer = { 1, 2 };
endian::stream_reader<EndianType> stream(buffer.data(), buffer.size());
uint8_t first_peek = 0;
@@ -133,6 +133,23 @@ static void test_basic_api()
EXPECT_EQ(third_peek, second_read);
}
{
SCOPED_TRACE(testing::Message() << "peek_read_return_value");
std::vector<uint8_t> buffer = { 1, 2 };
endian::stream_reader<EndianType> stream(buffer.data(), buffer.size());
uint8_t first_peek = stream.template peek<uint8_t>();
uint8_t second_peek = stream.template peek<uint8_t>();
uint8_t first_read = stream.template read<uint8_t>();
EXPECT_EQ(first_peek, second_peek);
EXPECT_EQ(first_peek, first_read);
auto third_peek = stream.template peek<uint8_t>();
auto second_read = stream.template read<uint8_t>();
EXPECT_NE(first_peek, third_peek);
EXPECT_EQ(third_peek, second_read);
}
{
SCOPED_TRACE(testing::Message() << "offset peek");
std::vector<uint8_t> buffer = { 1, 2, 3, 4 };

0 comments on commit 40e7d15

Please sign in to comment.