OGLplus  (0.59.0) a C++ wrapper for rendering APIs

buffer.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MEMORY_BUFFER_HPP
10 #define EAGINE_MEMORY_BUFFER_HPP
11 
12 #include "../assert.hpp"
13 #include "block.hpp"
14 #include "default_alloc.hpp"
15 #include "shared_alloc.hpp"
16 
17 namespace eagine::memory {
18 //------------------------------------------------------------------------------
22 class buffer {
23 public:
25  using size_type = typename block::size_type;
26 
28  using pointer = typename block::pointer;
29 
31  explicit buffer(span_size_t align)
32  : _align(align)
33  , _alloc(default_byte_allocator()) {}
34 
37  : buffer(alignof(long double)) {}
38 
40  buffer(buffer&& temp) noexcept
41  : _size{temp._size}
42  , _align{temp._align}
43  , _storage{std::move(temp._storage)}
44  , _alloc{std::move(temp._alloc)} {
45  temp._size = 0;
46  }
47 
49  auto operator=(buffer&& temp) noexcept -> buffer& {
50  _size = temp._size;
51  temp._size = 0;
52  _align = temp._align;
53  _storage = std::move(temp._storage);
54  _alloc = std::move(temp._alloc);
55  return *this;
56  }
57 
59  buffer(const buffer&) = delete;
60 
62  auto operator=(const buffer&) = delete;
63 
64  ~buffer() noexcept {
65  free();
66  }
67 
69  auto addr() const noexcept {
70  return _storage.addr();
71  }
72 
74  auto data() const noexcept -> pointer {
75  return _storage.data();
76  }
77 
81  auto size() const noexcept -> span_size_t {
82  return _size;
83  }
84 
88  auto empty() const noexcept {
89  return size() == 0;
90  }
91 
95  auto capacity() const noexcept -> span_size_t {
96  return _storage.size();
97  }
98 
102  auto reserve(span_size_t new_size) -> auto& {
103  if(capacity() < new_size) {
104  _reallocate(new_size);
105  }
106  EAGINE_ASSERT(_is_ok());
107  return *this;
108  }
109 
114  auto resize(span_size_t new_size) -> auto& {
115  reserve(new_size);
116  _size = new_size;
117  EAGINE_ASSERT(_is_ok());
118  return *this;
119  }
120 
126  auto ensure(span_size_t new_size) -> auto& {
127  if(size() < new_size) {
128  return resize(new_size);
129  }
130  EAGINE_ASSERT(_is_ok());
131  return *this;
132  }
133 
139  auto enlarge_by(span_size_t inc_size) -> auto& {
140  return resize(size() + inc_size);
141  }
142 
149  auto clear() -> auto& {
150  return resize(0);
151  }
152 
159  void free() {
160  _alloc.deallocate(std::move(_storage), _align);
161  _size = 0;
162  }
163 
165  operator block() noexcept {
166  EAGINE_ASSERT(_is_ok());
167  return {_storage.begin(), _size};
168  }
169 
171  operator const_block() const noexcept {
172  EAGINE_ASSERT(_is_ok());
173  return {_storage.begin(), _size};
174  }
175 
176 private:
177  span_size_t _size{0};
178  span_size_t _align{0};
179  owned_block _storage{};
180  shared_byte_allocator _alloc{};
181 
182  auto _is_ok() const noexcept -> bool {
183  return bool(_alloc) && size() <= capacity();
184  }
185 
186  void _reallocate(span_size_t new_size) {
187  _alloc.do_reallocate(_storage, new_size, _align);
188  }
189 };
190 //------------------------------------------------------------------------------
191 } // namespace eagine::memory
192 
193 #endif // EAGINE_MEMORY_BUFFER_HPP
buffer()
Default constructor.
Definition: buffer.hpp:36
typename block::pointer pointer
Pointer type.
Definition: buffer.hpp:28
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Pointer pointer
The pointer type.
Definition: span.hpp:114
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
constexpr auto begin() const noexcept -> iterator
Returns an interator to the start of the span.
Definition: span.hpp:259
constexpr auto addr() const noexcept -> address_type
Returns the memory address of the start of the span.
Definition: span.hpp:279
basic_block< false > block
Alias for non-const byte memory span.
Definition: block.hpp:27
buffer(buffer &&temp) noexcept
Move constructor.
Definition: buffer.hpp:40
constexpr auto data() const noexcept -> pointer
Returns a pointer to the start of the span.
Definition: span.hpp:254
void free()
Deallocates the buffer.
Definition: buffer.hpp:159
auto enlarge_by(span_size_t inc_size) -> auto &
Enlarges the buffer by the specified number of bytes.
Definition: buffer.hpp:139
typename block::size_type size_type
Buffer size type.
Definition: buffer.hpp:25
auto empty() const noexcept
Indicates that the buffer is empty.
Definition: buffer.hpp:88
Reallocatable owning byte buffer.
Definition: buffer.hpp:22
SizeType size_type
The element count type.
Definition: span.hpp:108
auto clear() -> auto &
Clears the buffer.
Definition: buffer.hpp:149
auto capacity() const noexcept -> span_size_t
Returns the capacity of this buffer.
Definition: buffer.hpp:95
auto operator=(buffer &&temp) noexcept -> buffer &
Move assignment operator.
Definition: buffer.hpp:49
auto data() const noexcept -> pointer
Returns the pointer to the start of the allocated space.
Definition: buffer.hpp:74
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition: span.hpp:246
auto addr() const noexcept
Returns the memory address of the start of the allocated space.
Definition: buffer.hpp:69
auto ensure(span_size_t new_size) -> auto &
Ensure that the buuffer has at least the specified size in bytes.
Definition: buffer.hpp:126
auto size() const noexcept -> span_size_t
Returns the size of the buffer in bytes.
Definition: buffer.hpp:81
auto resize(span_size_t new_size) -> auto &
Resizes the buffer to the specified number of bytes.
Definition: buffer.hpp:114
buffer(span_size_t align)
Constructor with explicit alignment specification.
Definition: buffer.hpp:31
auto reserve(span_size_t new_size) -> auto &
Pre-allocate the specified number of bytes.
Definition: buffer.hpp:102

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).