11 #ifndef TLX_CONTAINER_RING_BUFFER_HEADER 12 #define TLX_CONTAINER_RING_BUFFER_HEADER 35 template <
typename Type,
class Allocator = std::allocator<Type> >
49 using size_type =
typename allocator_type::size_type;
77 for (
size_t i = 0; i < rb.
size(); ++i) {
84 if (
this == &rb)
return *
this;
101 for (
size_t i = 0; i < rb.
size(); ++i)
115 rb.begin_ = rb.end_ = 0;
121 if (
this == &rb)
return *
this;
127 alloc_ = std::move(rb.alloc_);
133 rb.begin_ = rb.end_ = 0;
174 alloc_traits::construct(
180 template <
typename... Args>
184 std::forward<Args>(args) ...);
199 alloc_traits::construct(
204 template <
typename... Args>
209 std::forward<Args>(args) ...);
233 void copy_to(std::vector<value_type>* out)
const {
234 for (
size_t i = 0; i <
size(); ++i)
235 out->emplace_back(
operator [] (i));
241 out->emplace_back(std::move(
front()));
268 const_reference
front() const noexcept {
279 const_reference
back() const noexcept {
314 template <
class Archive>
316 uint32_t ar_size =
size();
318 for (
size_t i = 0; i < ar_size; ++i) ar(
operator [] (i));
321 template <
class Archive>
340 for (
size_t i = 0; i < ar_size; ++i) {
377 #endif // !TLX_CONTAINER_RING_BUFFER_HEADER
size_t capacity_
capacity of data buffer.
bool empty() const noexcept
returns true if no items are in the buffer
RingBuffer(const Allocator &alloc=allocator_type()) noexcept
void push_back(const value_type &t)
add element at the end
A ring (circular) buffer of static (non-growing) size.
size_t max_size() const noexcept
return the maximum number of items in the buffer.
void clear()
reset buffer contents
reference back() noexcept
Returns a reference to the last element.
size_t mask_
one-bits mask for calculating modulo of capacity using AND-mask.
allocator_type alloc_
used allocator
RingBuffer(const RingBuffer &rb)
copy-constructor: create new ring buffer
RingBuffer & operator=(const RingBuffer &rb)
copyable: create new ring buffer
typename allocator_type::size_type size_type
void deallocate()
deallocate buffer
size_type size() const noexcept
return the number of items in the buffer
void copy_to(std::vector< value_type > *out) const
copy all element into the vector
void push_front(value_type &&t)
add element at the beginning
RingBuffer(RingBuffer &&rb) noexcept
move-constructor: move buffer
RingBuffer(size_t max_size, const Allocator &alloc=allocator_type())
reference front() noexcept
Returns a reference to the first element.
void pop_front()
remove element at the beginning
void save(Archive &ar) const
size_t capacity() const noexcept
return actual capacity of the ring buffer.
size_type begin_
iterator at current begin of ring buffer
typename allocator_type::difference_type difference_type
reference operator[](size_type i) noexcept
Returns a reference to the i-th element.
void allocate(size_t max_size)
allocate buffer
Type * data_
the circular buffer of static size.
void push_back(value_type &&t)
add element at the end
size_t max_size_
target max_size of circular buffer prescribed by the user.
std::allocator_traits< allocator_type > alloc_traits
void pop_back()
remove element at the end
static int round_up_to_power_of_two(int i)
does what it says: round up to next power of two
void push_front(const value_type &t)
add element at the beginning
size_type end_
iterator at current begin of ring buffer
void emplace_back(Args &&...args)
emplace element at the end
const_reference front() const noexcept
Returns a reference to the first element.
const Type * const_pointer
void emplace_front(Args &&...args)
emplace element at the beginning
const Type & const_reference
void move_to(std::vector< value_type > *out)
move all element from the RingBuffer into the vector
const_reference back() const noexcept
Returns a reference to the last element.