mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-14 16:09:06 +07:00
more general buffer iterators
This commit is contained in:
@@ -11,6 +11,18 @@
|
||||
namespace e2d
|
||||
{
|
||||
class buffer final {
|
||||
public:
|
||||
using value_type = u8;
|
||||
|
||||
using pointer = u8*;
|
||||
using const_pointer = const u8*;
|
||||
using reference = u8&;
|
||||
using const_reference = const u8&;
|
||||
|
||||
using iterator = pointer;
|
||||
using const_iterator = const_pointer;
|
||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
public:
|
||||
buffer() = default;
|
||||
|
||||
@@ -23,6 +35,22 @@ namespace e2d
|
||||
explicit buffer(std::size_t size);
|
||||
buffer(const void* src, std::size_t size);
|
||||
|
||||
iterator begin() noexcept;
|
||||
const_iterator begin() const noexcept;
|
||||
const_iterator cbegin() const noexcept;
|
||||
|
||||
iterator end() noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
const_iterator cend() const noexcept;
|
||||
|
||||
reverse_iterator rbegin() noexcept;
|
||||
const_reverse_iterator rbegin() const noexcept;
|
||||
const_reverse_iterator crbegin() const noexcept;
|
||||
|
||||
reverse_iterator rend() noexcept;
|
||||
const_reverse_iterator rend() const noexcept;
|
||||
const_reverse_iterator crend() const noexcept;
|
||||
|
||||
buffer& fill(u8 ch) noexcept;
|
||||
buffer& resize(std::size_t nsize);
|
||||
|
||||
@@ -37,11 +65,6 @@ namespace e2d
|
||||
u8* data() noexcept;
|
||||
const u8* data() const noexcept;
|
||||
std::size_t size() const noexcept;
|
||||
|
||||
const u8* begin() const noexcept;
|
||||
const u8* end() const noexcept;
|
||||
u8* begin() noexcept;
|
||||
u8* end() noexcept;
|
||||
private:
|
||||
using data_t = std::unique_ptr<u8[]>;
|
||||
data_t data_;
|
||||
|
||||
@@ -32,6 +32,54 @@ namespace e2d
|
||||
assign(src, size);
|
||||
}
|
||||
|
||||
buffer::iterator buffer::begin() noexcept {
|
||||
return data_.get();
|
||||
}
|
||||
|
||||
buffer::const_iterator buffer::begin() const noexcept {
|
||||
return data_.get();
|
||||
}
|
||||
|
||||
buffer::const_iterator buffer::cbegin() const noexcept {
|
||||
return data_.get();
|
||||
}
|
||||
|
||||
buffer::iterator buffer::end() noexcept {
|
||||
return data_.get() + size_;
|
||||
}
|
||||
|
||||
buffer::const_iterator buffer::end() const noexcept {
|
||||
return data_.get() + size_;
|
||||
}
|
||||
|
||||
buffer::const_iterator buffer::cend() const noexcept {
|
||||
return data_.get() + size_;
|
||||
}
|
||||
|
||||
buffer::reverse_iterator buffer::rbegin() noexcept {
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
|
||||
buffer::const_reverse_iterator buffer::rbegin() const noexcept {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
|
||||
buffer::const_reverse_iterator buffer::crbegin() const noexcept {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
|
||||
buffer::reverse_iterator buffer::rend() noexcept {
|
||||
return reverse_iterator(begin());
|
||||
}
|
||||
|
||||
buffer::const_reverse_iterator buffer::rend() const noexcept {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
buffer::const_reverse_iterator buffer::crend() const noexcept {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
buffer& buffer::fill(u8 ch) noexcept {
|
||||
if ( data_ && size_) {
|
||||
std::memset(data_.get(), ch, size_);
|
||||
@@ -107,22 +155,6 @@ namespace e2d
|
||||
std::size_t buffer::size() const noexcept {
|
||||
return size_;
|
||||
}
|
||||
|
||||
const u8* buffer::begin() const noexcept {
|
||||
return data_.get();
|
||||
}
|
||||
|
||||
const u8* buffer::end() const noexcept {
|
||||
return data_.get() + size_;
|
||||
}
|
||||
|
||||
u8* buffer::begin() noexcept {
|
||||
return data_.get();
|
||||
}
|
||||
|
||||
u8* buffer::end() noexcept {
|
||||
return data_.get() + size_;
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d
|
||||
|
||||
@@ -170,6 +170,22 @@ TEST_CASE("buffer") {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
{
|
||||
const u8 data[] = {1,2,3};
|
||||
buffer b(data, std::size(data) * sizeof(data[0]));
|
||||
|
||||
const auto con_str = [](std::string acc, u8 ch){
|
||||
return acc + std::to_string(ch);
|
||||
};
|
||||
|
||||
REQUIRE(std::accumulate(b.begin(), b.end(), std::string(), con_str) == "123");
|
||||
REQUIRE(std::accumulate(std::as_const(b).begin(), std::as_const(b).end(), std::string(), con_str) == "123");
|
||||
REQUIRE(std::accumulate(b.cbegin(), b.cend(), std::string(), con_str) == "123");
|
||||
|
||||
REQUIRE(std::accumulate(b.rbegin(), b.rend(), std::string(), con_str) == "321");
|
||||
REQUIRE(std::accumulate(std::as_const(b).rbegin(), std::as_const(b).rend(), std::string(), con_str) == "321");
|
||||
REQUIRE(std::accumulate(b.crbegin(), b.crend(), std::string(), con_str) == "321");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("buffer_view") {
|
||||
|
||||
Reference in New Issue
Block a user