sdi_toolBox
circularBuffer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
3*/
4
5/*
6ver: 2.x.x - build: 2026-04-28
7*/
8
9/*
10The zlib License Copyright (c) 2026 SD-Innovation S.A.S. This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
11*/
12
13#pragma once
14
15#include "ringBuffer.h"
16
18{
19//--------------------------------------------------------------
47template<class T, std::size_t CAPACITY>
49{
50 static_assert(CAPACITY > 0, "CAPACITY must be > 0");
51
52 public:
55
64 bool push(const T &value);
65
69
76 std::optional<T> pop();
84 bool pop(T &value);
85
91 [[nodiscard]] std::optional<T> front() const;
92
100 [[nodiscard]] bool front(T &value) const;
101
107 [[nodiscard]] std::optional<T> back() const;
108
116 [[nodiscard]] bool back(T &value) const;
117
121
127 [[nodiscard]] bool empty() const;
128
134 [[nodiscard]] bool full() const;
135
141 [[nodiscard]] std::size_t size() const;
142
148 [[nodiscard]] constexpr std::size_t capacity() const;
149
153
159 void clear();
160
162
163 private:
166
167 RingBuffer<T, CAPACITY> m_buffer{};
168
170};
171//--------------------------------------------------------------
172
173//--------------------------------------------------------------
174/* Try to append a value to the buffer. If the buffer is full,
175 * the value is discarded. Return true if the value was inserted,
176 * false if the buffer was full. */
177template<class T, std::size_t CAPACITY>
179{
180 if (m_buffer.full())
181 return false;
182 return m_buffer.push(value);
183}
184//--------------------------------------------------------------
185/* Remove and return the oldest value from the buffer. If the
186 * buffer is empty, return std::nullopt */
187template<class T, std::size_t CAPACITY>
189{
190 return m_buffer.pop();
191}
192//--------------------------------------------------------------
193/* Remove the oldest value from the buffer and store it in
194 * 'value'. Return true if successful, false if the buffer is empty */
195template<class T, std::size_t CAPACITY>
197{
198 return m_buffer.pop(value);
199}
200//--------------------------------------------------------------
201/* Return the oldest value without removing it. If the buffer is
202 * empty, return std::nullopt */
203template<class T, std::size_t CAPACITY>
205{
206 return m_buffer.front();
207}
208//--------------------------------------------------------------
209/* Return the oldest value without removing it and store it in
210 * 'value'. Return true if successful, false if the buffer is empty */
211template<class T, std::size_t CAPACITY>
213{
214 return m_buffer.front(value);
215}
216//--------------------------------------------------------------
217/* Return the newest value without removing it. If the buffer is
218 * empty, return std::nullopt */
219template<class T, std::size_t CAPACITY>
220std::optional<T> CircularBuffer<T, CAPACITY>::back() const
221{
222 return m_buffer.back();
223}
224//--------------------------------------------------------------
225/* Return the newest value without removing it and store it in
226 * 'value'. Return true if successful, false if the buffer is empty */
227template<class T, std::size_t CAPACITY>
229{
230 return m_buffer.back(value);
231}
232//--------------------------------------------------------------
233/* Check if the buffer is empty */
234template<class T, std::size_t CAPACITY>
236{
237 return m_buffer.empty();
238}
239//--------------------------------------------------------------
240/* Check if the buffer is full */
241template<class T, std::size_t CAPACITY>
243{
244 return m_buffer.full();
245}
246//--------------------------------------------------------------
247/* Get the number of elements currently in the buffer */
248template<class T, std::size_t CAPACITY>
250{
251 return m_buffer.size();
252}
253//--------------------------------------------------------------
254/* Get the maximum capacity of the buffer */
255template<class T, std::size_t CAPACITY>
256[[nodiscard]] constexpr std::size_t CircularBuffer<T, CAPACITY>::capacity() const
257{
258 return CAPACITY;
259}
260//--------------------------------------------------------------
261/* Clear the buffer, resetting it to an empty state */
262template<class T, std::size_t CAPACITY>
264{
265 m_buffer.clear();
266}
267//--------------------------------------------------------------
268} // namespace sdi_toolBox::common::utils
Fixed-size circular buffer with compile-time capacity and no-overwrite behavior.
std::optional< T > front() const
Return (without removing) the oldest element.
bool push(const T &value)
Try to append a value to the buffer.
std::optional< T > pop()
Remove and return the oldest element from the buffer.
bool empty() const
Check whether the buffer is empty.
bool full() const
Check whether the buffer is full.
std::optional< T > back() const
Return (without removing) the newest element.
void clear()
Clear the buffer and reset internal indices.
std::size_t size() const
Number of elements currently stored in the buffer.
constexpr std::size_t capacity() const
Compile-time capacity of the buffer.
Fixed-size ring (circular) buffer with compile-time capacity and overwrite-on-full behavior.
Definition ringBuffer.h:50
bool push(const T &value)
Append a value to the buffer.
Definition ringBuffer.h:196
General-purpose utility functions shared across all sdi_toolBox modules.