Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/hexi/binary_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class binary_stream final {

template<typename T>
inline void advance_write(T&& arg) {
total_write_ += sizeof(std::decay_t<T>);
total_write_ += sizeof(T);
}

template<typename T, typename U>
Expand Down
2 changes: 1 addition & 1 deletion include/hexi/buffer_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class buffer_adaptor final {
* @return The number of bytes of data available to read within the container.
*/
size_type size() const {
return buffer_.size() - read_;
return write_ - read_;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions include/hexi/pmc/buffer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
namespace hexi::pmc {

class buffer_base {
protected:
std::size_t read_ = 0;
std::size_t write_ = 0;

public:
virtual std::size_t size() const = 0;
virtual bool empty() const = 0;
Expand Down
8 changes: 3 additions & 5 deletions include/hexi/pmc/buffer_read_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ template<byte_oriented buf_type>
requires std::ranges::contiguous_range<buf_type>
class buffer_read_adaptor : public buffer_read {
buf_type& buffer_;
std::size_t read_;

public:
buffer_read_adaptor(buf_type& buffer)
: buffer_(buffer),
read_(0) {}
: buffer_(buffer) {}

/**
* @brief Reads a number of bytes to the provided buffer.
Expand Down Expand Up @@ -95,7 +93,7 @@ class buffer_read_adaptor : public buffer_read {
* @return The number of bytes of data available to read within the stream.
*/
std::size_t size() const override {
return buffer_.size() - read_;
return write_ - read_;
}

/**
Expand All @@ -105,7 +103,7 @@ class buffer_read_adaptor : public buffer_read {
*/
[[nodiscard]]
bool empty() const override {
return !(buffer_.size() - read_);
return read_ == write_;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions include/hexi/pmc/buffer_write_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ template<byte_oriented buf_type>
requires std::ranges::contiguous_range<buf_type>
class buffer_write_adaptor : public buffer_write {
buf_type& buffer_;
std::size_t write_;

public:
buffer_write_adaptor(buf_type& buffer)
: buffer_(buffer),
write_(buffer.size()) {}
: buffer_(buffer) {
write_ = buffer.size();
}

buffer_write_adaptor(buf_type& buffer, init_empty_t)
: buffer_(buffer),
write_(0) {}
: buffer_(buffer) {}

/**
* @brief Write data to the container.
Expand Down
26 changes: 13 additions & 13 deletions single_include/hexi.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ concept memcpy_read =
&& !has_shr_override<typename T::value_type, U>
&& !has_deserialise<typename T::value_type, U>;


template<typename T, typename U>
concept memcpy_write =
pod<typename T::value_type> && std::ranges::contiguous_range<T>
Expand Down Expand Up @@ -661,7 +660,7 @@ class binary_stream final {

template<typename T>
inline void advance_write(T&& arg) {
total_write_ += sizeof(std::decay_t<T>);
total_write_ += sizeof(T);
}

template<typename T, typename U>
Expand Down Expand Up @@ -1603,7 +1602,7 @@ class buffer_adaptor final {
* @return The number of bytes of data available to read within the container.
*/
size_type size() const {
return buffer_.size() - read_;
return write_ - read_;
}

/**
Expand Down Expand Up @@ -1887,6 +1886,10 @@ friend class const_iterator;
namespace hexi::pmc {

class buffer_base {
protected:
std::size_t read_ = 0;
std::size_t write_ = 0;

public:
virtual std::size_t size() const = 0;
virtual bool empty() const = 0;
Expand Down Expand Up @@ -5000,12 +5003,10 @@ template<byte_oriented buf_type>
requires std::ranges::contiguous_range<buf_type>
class buffer_read_adaptor : public buffer_read {
buf_type& buffer_;
std::size_t read_;

public:
buffer_read_adaptor(buf_type& buffer)
: buffer_(buffer),
read_(0) {}
: buffer_(buffer) {}

/**
* @brief Reads a number of bytes to the provided buffer.
Expand Down Expand Up @@ -5071,7 +5072,7 @@ class buffer_read_adaptor : public buffer_read {
* @return The number of bytes of data available to read within the stream.
*/
std::size_t size() const override {
return buffer_.size() - read_;
return write_ - read_;
}

/**
Expand All @@ -5081,7 +5082,7 @@ class buffer_read_adaptor : public buffer_read {
*/
[[nodiscard]]
bool empty() const override {
return !(buffer_.size() - read_);
return read_ == write_;
}

/**
Expand Down Expand Up @@ -5172,16 +5173,15 @@ template<byte_oriented buf_type>
requires std::ranges::contiguous_range<buf_type>
class buffer_write_adaptor : public buffer_write {
buf_type& buffer_;
std::size_t write_;

public:
buffer_write_adaptor(buf_type& buffer)
: buffer_(buffer),
write_(buffer.size()) {}
: buffer_(buffer) {
write_ = buffer.size();
}

buffer_write_adaptor(buf_type& buffer, init_empty_t)
: buffer_(buffer),
write_(0) {}
: buffer_(buffer) {}

/**
* @brief Write data to the container.
Expand Down
9 changes: 9 additions & 0 deletions tests/binary_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,4 +1076,13 @@ TEST(binary_stream, prefixed_containers) {
stream >> hexi::prefixed(output_objs);
EXPECT_EQ(objects.size(), output_objs.size());
ASSERT_EQ(objects, output_objs);
}

TEST(binary_stream, std_array_size) {
std::array<char, 16> buffer;
hexi::buffer_adaptor adaptor(buffer, hexi::init_empty);
hexi::binary_stream stream(adaptor);
ASSERT_TRUE(adaptor.empty());
ASSERT_EQ(adaptor.size(), 0);
ASSERT_EQ(stream.size(), 0);
}
35 changes: 22 additions & 13 deletions tests/binary_stream_pmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ TEST(binary_stream_pmc, set_error_state) {
ASSERT_TRUE(stream.state() == hexi::stream_state::user_defined_err);
}

TEST(binary_stream_pmr, string_adaptor_prefixed_varint_long) {
TEST(binary_stream_pmc, string_adaptor_prefixed_varint_long) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -322,7 +322,7 @@ TEST(binary_stream_pmr, string_adaptor_prefixed_varint_long) {
ASSERT_TRUE(stream);
}

TEST(binary_stream_pmr, string_adaptor_prefixed_varint_medium) {
TEST(binary_stream_pmc, string_adaptor_prefixed_varint_medium) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -346,7 +346,7 @@ TEST(binary_stream_pmr, string_adaptor_prefixed_varint_medium) {
ASSERT_TRUE(stream);
}

TEST(binary_stream_pmr, string_adaptor_prefixed_varint_short) {
TEST(binary_stream_pmc, string_adaptor_prefixed_varint_short) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -370,7 +370,7 @@ TEST(binary_stream_pmr, string_adaptor_prefixed_varint_short) {
ASSERT_TRUE(stream);
}

TEST(binary_stream_pmr, string_adaptor_prefixed) {
TEST(binary_stream_pmc, string_adaptor_prefixed) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -382,7 +382,7 @@ TEST(binary_stream_pmr, string_adaptor_prefixed) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, string_adaptor_default) {
TEST(binary_stream_pmc, string_adaptor_default) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -394,7 +394,7 @@ TEST(binary_stream_pmr, string_adaptor_default) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, string_adaptor_raw) {
TEST(binary_stream_pmc, string_adaptor_raw) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -407,7 +407,7 @@ TEST(binary_stream_pmr, string_adaptor_raw) {
ASSERT_FALSE(stream.empty());
}

TEST(binary_stream_pmr, string_adaptor_null_terminated) {
TEST(binary_stream_pmc, string_adaptor_null_terminated) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -419,7 +419,7 @@ TEST(binary_stream_pmr, string_adaptor_null_terminated) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, string_view_adaptor_prefixed) {
TEST(binary_stream_pmc, string_view_adaptor_prefixed) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -431,7 +431,7 @@ TEST(binary_stream_pmr, string_view_adaptor_prefixed) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, string_view_adaptor_default) {
TEST(binary_stream_pmc, string_view_adaptor_default) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -443,7 +443,7 @@ TEST(binary_stream_pmr, string_view_adaptor_default) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, string_view_adaptor_raw) {
TEST(binary_stream_pmc, string_view_adaptor_raw) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -457,7 +457,7 @@ TEST(binary_stream_pmr, string_view_adaptor_raw) {
ASSERT_FALSE(stream.empty());
}

TEST(binary_stream_pmr, string_view_adaptor_null_terminated) {
TEST(binary_stream_pmc, string_view_adaptor_null_terminated) {
std::vector<char> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -470,7 +470,7 @@ TEST(binary_stream_pmr, string_view_adaptor_null_terminated) {
ASSERT_TRUE(stream.empty());
}

TEST(binary_stream_pmr, std_array) {
TEST(binary_stream_pmc, std_array) {
std::array<char, 128> buffer{};
hexi::pmc::buffer_adaptor adaptor(buffer);
hexi::pmc::binary_stream stream(adaptor);
Expand All @@ -490,7 +490,7 @@ TEST(binary_stream_pmr, std_array) {
ASSERT_EQ(input, output);
}

TEST(binary_stream_pmr, total_write_consistency) {
TEST(binary_stream_pmc, total_write_consistency) {
std::array<char, 1024> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer, hexi::init_empty);
hexi::pmc::binary_stream stream(adaptor);
Expand Down Expand Up @@ -709,4 +709,13 @@ TEST(binary_stream_pmc, prefixed_containers) {
stream >> hexi::prefixed(output_objs);
EXPECT_EQ(objects.size(), output_objs.size());
ASSERT_EQ(objects, output_objs);
}

TEST(binary_stream_pmc, std_array_size) {
std::array<char, 16> buffer;
hexi::pmc::buffer_adaptor adaptor(buffer, hexi::init_empty);
hexi::pmc::binary_stream stream(adaptor);
ASSERT_TRUE(adaptor.empty());
ASSERT_EQ(adaptor.size(), 0);
ASSERT_EQ(stream.size(), 0);
}
1 change: 1 addition & 0 deletions tests/buffer_adaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TEST(buffer_adaptor, resize_match) {
hexi::buffer_adaptor adaptor(buffer);
ASSERT_EQ(adaptor.size(), buffer.size());
buffer.emplace_back(6);
adaptor.advance_write(1);
ASSERT_EQ(adaptor.size(), buffer.size());
}

Expand Down
1 change: 1 addition & 0 deletions tests/buffer_adaptor_pmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TEST(buffer_adaptor_pmc, resize_match) {
hexi::pmc::buffer_adaptor adaptor(buffer);
ASSERT_EQ(adaptor.size(), buffer.size());
buffer.emplace_back(6);
adaptor.advance_write(1);
ASSERT_EQ(adaptor.size(), buffer.size());
}

Expand Down