15#ifndef EASYNAV_COMMON__CIRCULARBUFFER_HPP_
16#define EASYNAV_COMMON__CIRCULARBUFFER_HPP_
51 : buffer_(other.capacity_),
52 capacity_(other.capacity_),
57 std::lock_guard<std::mutex> lock(other.mutex_);
58 buffer_ = other.buffer_;
72 std::scoped_lock<std::mutex, std::mutex> lock(mutex_, other.mutex_);
74 capacity_ = other.capacity_;
75 buffer_ = other.buffer_;
94 std::size_t
size() const noexcept
96 std::lock_guard<std::mutex> lock(mutex_);
103 std::lock_guard<std::mutex> lock(mutex_);
110 std::lock_guard<std::mutex> lock(mutex_);
111 return size_ == capacity_;
117 std::lock_guard<std::mutex> lock(mutex_);
126 std::lock_guard<std::mutex> lock(mutex_);
127 buffer_[head_] = value;
134 std::lock_guard<std::mutex> lock(mutex_);
135 buffer_[head_] = std::move(value);
145 std::lock_guard<std::mutex> lock(mutex_);
150 out = buffer_[tail_];
151 tail_ = (tail_ + 1) % capacity_;
162 std::lock_guard<std::mutex> lock(mutex_);
167 const std::size_t newest_index =
168 (head_ + capacity_ - 1) % capacity_;
169 out = buffer_[newest_index];
182 if (!buffer_[idx].engaged) {
195 head_ = (head_ + 1) % capacity_;
196 if (size_ < capacity_) {
200 tail_ = (tail_ + 1) % capacity_;
204 mutable std::mutex mutex_;
205 std::vector<T> buffer_;
206 std::size_t capacity_;
void clear() noexcept
Remove all elements, keeping the allocated storage.
Definition CircularBuffer.hpp:115
void push(T &&value)
Push a new element (move). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:132
bool empty() const noexcept
True if the buffer is empty.
Definition CircularBuffer.hpp:101
std::size_t size() const noexcept
Current number of valid elements.
Definition CircularBuffer.hpp:94
CircularBuffer(std::size_t capacity)
Construct a buffer with the given capacity.
Definition CircularBuffer.hpp:37
void push(const T &value)
Push a new element (copy). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:124
DebugSlotView raw_slot(std::size_t idx) const
Definition CircularBuffer.hpp:180
bool latest(T &out) const
Get a copy of the newest element without removing it.
Definition CircularBuffer.hpp:160
bool pop(T &out)
Pop the oldest element.
Definition CircularBuffer.hpp:143
CircularBuffer & operator=(CircularBuffer &&)=delete
CircularBuffer & operator=(const CircularBuffer &other)
Copy assignment.
Definition CircularBuffer.hpp:65
std::size_t capacity() const noexcept
Maximum number of elements that can be stored.
Definition CircularBuffer.hpp:88
bool full() const noexcept
True if the buffer is full.
Definition CircularBuffer.hpp:108
CircularBuffer(CircularBuffer &&)=delete
Move operations are optional.
CircularBuffer(const CircularBuffer &other)
Copy constructor.
Definition CircularBuffer.hpp:50
Definition CircularBuffer.hpp:24
Definition CircularBuffer.hpp:175
const T & value
Definition CircularBuffer.hpp:177
bool has_value
Definition CircularBuffer.hpp:176