20#ifndef EASYNAV_COMMON__CIRCULARBUFFER_HPP_
21#define EASYNAV_COMMON__CIRCULARBUFFER_HPP_
55 : buffer_(other.capacity_),
56 capacity_(other.capacity_),
61 std::lock_guard<std::mutex> lock(other.mutex_);
62 buffer_ = other.buffer_;
76 std::scoped_lock<std::mutex, std::mutex> lock(mutex_, other.mutex_);
78 capacity_ = other.capacity_;
79 buffer_ = other.buffer_;
98 std::size_t
size() const noexcept
100 std::lock_guard<std::mutex> lock(mutex_);
107 std::lock_guard<std::mutex> lock(mutex_);
114 std::lock_guard<std::mutex> lock(mutex_);
115 return size_ == capacity_;
121 std::lock_guard<std::mutex> lock(mutex_);
130 std::lock_guard<std::mutex> lock(mutex_);
131 buffer_[head_] = value;
138 std::lock_guard<std::mutex> lock(mutex_);
139 buffer_[head_] = std::move(value);
149 std::lock_guard<std::mutex> lock(mutex_);
154 out = buffer_[tail_];
155 tail_ = (tail_ + 1) % capacity_;
166 std::lock_guard<std::mutex> lock(mutex_);
171 const std::size_t newest_index =
172 (head_ + capacity_ - 1) % capacity_;
173 out = buffer_[newest_index];
180 std::lock_guard<std::mutex> lock(mutex_);
182 const std::size_t newest_index =
183 (head_ + capacity_ - 1) % capacity_;
184 return buffer_[newest_index];
196 if (!buffer_[idx].engaged) {
209 head_ = (head_ + 1) % capacity_;
210 if (size_ < capacity_) {
214 tail_ = (tail_ + 1) % capacity_;
218 mutable std::mutex mutex_;
219 std::vector<T> buffer_;
220 std::size_t capacity_;
void clear() noexcept
Remove all elements, keeping the allocated storage.
Definition CircularBuffer.hpp:119
void push(T &&value)
Push a new element (move). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:136
const T & latest_ref() const
Get a const reference to the newest element without removing it.
Definition CircularBuffer.hpp:178
bool empty() const noexcept
True if the buffer is empty.
Definition CircularBuffer.hpp:105
std::size_t size() const noexcept
Current number of valid elements.
Definition CircularBuffer.hpp:98
CircularBuffer(std::size_t capacity)
Construct a buffer with the given capacity.
Definition CircularBuffer.hpp:41
void push(const T &value)
Push a new element (copy). Overwrites the oldest if the buffer is full.
Definition CircularBuffer.hpp:128
DebugSlotView raw_slot(std::size_t idx) const
Definition CircularBuffer.hpp:194
bool latest(T &out) const
Get a copy of the newest element without removing it.
Definition CircularBuffer.hpp:164
bool pop(T &out)
Pop the oldest element.
Definition CircularBuffer.hpp:147
CircularBuffer & operator=(CircularBuffer &&)=delete
CircularBuffer & operator=(const CircularBuffer &other)
Copy assignment.
Definition CircularBuffer.hpp:69
std::size_t capacity() const noexcept
Maximum number of elements that can be stored.
Definition CircularBuffer.hpp:92
bool full() const noexcept
True if the buffer is full.
Definition CircularBuffer.hpp:112
CircularBuffer(CircularBuffer &&)=delete
Move operations are optional.
CircularBuffer(const CircularBuffer &other)
Copy constructor.
Definition CircularBuffer.hpp:54
Definition CircularBuffer.hpp:28
Definition CircularBuffer.hpp:189
const T & value
Definition CircularBuffer.hpp:191
bool has_value
Definition CircularBuffer.hpp:190