Skip to content

Commit

Permalink
Added constructor to buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Mar 11, 2024
1 parent 878d5d8 commit af3504d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion include/sparrow/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ namespace sparrow

buffer() = default;
explicit buffer(size_type size);
buffer(size_type size, value_type);
buffer(pointer data, size_type size);

~buffer();
Expand Down Expand Up @@ -463,6 +464,13 @@ namespace sparrow
{
}

template <class T>
buffer<T>::buffer(size_type size, value_type value)
: base_type{allocate(size), size}
{
std::fill_n(base_type::data(), size, value);
}

template <class T>
buffer<T>::buffer(pointer data, size_type size)
: base_type{data, size}
Expand Down Expand Up @@ -527,7 +535,7 @@ namespace sparrow
resize(n);
if (old_size < n)
{
std::fill(base_type::data() + old_size, base_type::data() + n, value);
std::fill_n(base_type::data() + old_size, n, value);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/test_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ namespace sparrow
CHECK_EQ(b2.data(), mem);
CHECK_EQ(b2.size(), expected_size);
CHECK_EQ(b2.data()[2], uint8_t(2));

const uint8_t expected_value = 3;
buffer_test_type b3(expected_size, expected_value);
CHECK_NE(b3.data(), nullptr);
CHECK_EQ(b3.size(), expected_size);
for (std::size_t i = 0; i < expected_size; ++i)
{
CHECK_EQ(b3[i], expected_value);
}
}

TEST_CASE("copy semantic")
Expand Down

0 comments on commit af3504d

Please sign in to comment.