11 #ifndef TLX_CONTAINER_RING_BUFFER_HEADER 12 #define TLX_CONTAINER_RING_BUFFER_HEADER 35 template <
typename Type,
class Allocator = std::allocator<Type> >
44 using reference =
typename allocator_type::reference;
46 using pointer =
typename allocator_type::pointer;
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)
109 alloc_(std::move(rb.alloc_)),
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()));
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
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
std::allocator< Input > allocator_type
void emplace_back(Args &&... args)
emplace element at the end
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
typename allocator_type::const_reference const_reference
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())
typename allocator_type::reference reference
typename allocator_type::const_pointer const_pointer
reference front() noexcept
Returns a reference to the first element.
void pop_front()
remove element at the beginning
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
void emplace_front(Args &&... args)
emplace element at the beginning
std::allocator_traits< allocator_type > alloc_traits
void pop_back()
remove element at the end
typename allocator_type::pointer pointer
static int round_up_to_power_of_two(int i)
does what it says: round up to next power of two
void save(Archive &ar) const
void push_front(const value_type &t)
add element at the beginning
size_type end_
iterator at current begin of ring buffer
const_reference front() const noexcept
Returns a reference to the first element.
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.