15#ifndef EASYNAV_COMMON__CIRCULARBUFFER_HPP_
16#define EASYNAV_COMMON__CIRCULARBUFFER_HPP_
50 : buffer_(other.capacity_),
51 capacity_(other.capacity_),
56 std::lock_guard<std::mutex> lock(other.mutex_);
57 buffer_ = other.buffer_;
71 std::scoped_lock<std::mutex, std::mutex> lock(mutex_, other.mutex_);
73 capacity_ = other.capacity_;
74 buffer_ = other.buffer_;
93 std::size_t
size() const noexcept
95 std::lock_guard<std::mutex> lock(mutex_);
102 std::lock_guard<std::mutex> lock(mutex_);
109 std::lock_guard<std::mutex> lock(mutex_);
110 return size_ == capacity_;
116 std::lock_guard<std::mutex> lock(mutex_);
125 std::lock_guard<std::mutex> lock(mutex_);
126 buffer_[head_] = value;
133 std::lock_guard<std::mutex> lock(mutex_);
134 buffer_[head_] = std::move(value);
144 std::lock_guard<std::mutex> lock(mutex_);
149 out = buffer_[tail_];
150 tail_ = (tail_ + 1) % capacity_;
161 std::lock_guard<std::mutex> lock(mutex_);
166 const std::size_t newest_index =
167 (head_ + capacity_ - 1) % capacity_;
168 out = buffer_[newest_index];
175 std::lock_guard<std::mutex> lock(mutex_);
177 const std::size_t newest_index =
178 (head_ + capacity_ - 1) % capacity_;
179 return buffer_[newest_index];
191 if (!buffer_[idx].engaged) {
204 head_ = (head_ + 1) % capacity_;
205 if (size_ < capacity_) {
209 tail_ = (tail_ + 1) % capacity_;
213 mutable std::mutex mutex_;
214 std::vector<T> buffer_;
215 std::size_t capacity_;
void clear() noexcept
Remove all elements, keeping the allocated storage.
Definition CircularBuffer.hpp:114
void push(T &&value)
Push a new element (move). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:131
const T & latest_ref() const
Get a const reference to the newest element without removing it.
Definition CircularBuffer.hpp:173
bool empty() const noexcept
True if the buffer is empty.
Definition CircularBuffer.hpp:100
std::size_t size() const noexcept
Current number of valid elements.
Definition CircularBuffer.hpp:93
CircularBuffer(std::size_t capacity)
Construct a buffer with the given capacity.
Definition CircularBuffer.hpp:36
void push(const T &value)
Push a new element (copy). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:123
DebugSlotView raw_slot(std::size_t idx) const
Definition CircularBuffer.hpp:189
bool latest(T &out) const
Get a copy of the newest element without removing it.
Definition CircularBuffer.hpp:159
bool pop(T &out)
Pop the oldest element.
Definition CircularBuffer.hpp:142
CircularBuffer & operator=(CircularBuffer &&)=delete
CircularBuffer & operator=(const CircularBuffer &other)
Copy assignment.
Definition CircularBuffer.hpp:64
std::size_t capacity() const noexcept
Maximum number of elements that can be stored.
Definition CircularBuffer.hpp:87
bool full() const noexcept
True if the buffer is full.
Definition CircularBuffer.hpp:107
CircularBuffer(CircularBuffer &&)=delete
Move operations are optional.
CircularBuffer(const CircularBuffer &other)
Copy constructor.
Definition CircularBuffer.hpp:49
Definition CircularBuffer.hpp:23
Definition CircularBuffer.hpp:184
const T & value
Definition CircularBuffer.hpp:186
bool has_value
Definition CircularBuffer.hpp:185