circularBuffer.h
Go to the documentation of this file.
/*
Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
*/
/*
ver: 2.x.x - build: 2026-04-28
*/
/*
The 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.
*/
#pragma once
#include "ringBuffer.h"
namespace sdi_toolBox::common::utils
{
//--------------------------------------------------------------
template<class T, std::size_t CAPACITY>
class CircularBuffer
{
static_assert(CAPACITY > 0, "CAPACITY must be > 0");
public:
bool push(const T &value);
std::optional<T> pop();
bool pop(T &value);
[[nodiscard]] std::optional<T> front() const;
[[nodiscard]] bool front(T &value) const;
[[nodiscard]] std::optional<T> back() const;
[[nodiscard]] bool back(T &value) const;
[[nodiscard]] bool empty() const;
[[nodiscard]] bool full() const;
[[nodiscard]] std::size_t size() const;
[[nodiscard]] constexpr std::size_t capacity() const;
void clear();
private:
RingBuffer<T, CAPACITY> m_buffer{};
};
//--------------------------------------------------------------
//--------------------------------------------------------------
/* Try to append a value to the buffer. If the buffer is full,
* the value is discarded. Return true if the value was inserted,
* false if the buffer was full. */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::push(const T &value)
{
if (m_buffer.full())
return false;
return m_buffer.push(value);
}
//--------------------------------------------------------------
/* Remove and return the oldest value from the buffer. If the
* buffer is empty, return std::nullopt */
template<class T, std::size_t CAPACITY>
std::optional<T> CircularBuffer<T, CAPACITY>::pop()
{
return m_buffer.pop();
}
//--------------------------------------------------------------
/* Remove the oldest value from the buffer and store it in
* 'value'. Return true if successful, false if the buffer is empty */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::pop(T &value)
{
return m_buffer.pop(value);
}
//--------------------------------------------------------------
/* Return the oldest value without removing it. If the buffer is
* empty, return std::nullopt */
template<class T, std::size_t CAPACITY>
std::optional<T> CircularBuffer<T, CAPACITY>::front() const
{
return m_buffer.front();
}
//--------------------------------------------------------------
/* Return the oldest value without removing it and store it in
* 'value'. Return true if successful, false if the buffer is empty */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::front(T &value) const
{
return m_buffer.front(value);
}
//--------------------------------------------------------------
/* Return the newest value without removing it. If the buffer is
* empty, return std::nullopt */
template<class T, std::size_t CAPACITY>
std::optional<T> CircularBuffer<T, CAPACITY>::back() const
{
return m_buffer.back();
}
//--------------------------------------------------------------
/* Return the newest value without removing it and store it in
* 'value'. Return true if successful, false if the buffer is empty */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::back(T &value) const
{
return m_buffer.back(value);
}
//--------------------------------------------------------------
/* Check if the buffer is empty */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::empty() const
{
return m_buffer.empty();
}
//--------------------------------------------------------------
/* Check if the buffer is full */
template<class T, std::size_t CAPACITY>
bool CircularBuffer<T, CAPACITY>::full() const
{
return m_buffer.full();
}
//--------------------------------------------------------------
/* Get the number of elements currently in the buffer */
template<class T, std::size_t CAPACITY>
std::size_t CircularBuffer<T, CAPACITY>::size() const
{
return m_buffer.size();
}
//--------------------------------------------------------------
/* Get the maximum capacity of the buffer */
template<class T, std::size_t CAPACITY>
[[nodiscard]] constexpr std::size_t CircularBuffer<T, CAPACITY>::capacity() const
{
return CAPACITY;
}
//--------------------------------------------------------------
/* Clear the buffer, resetting it to an empty state */
template<class T, std::size_t CAPACITY>
void CircularBuffer<T, CAPACITY>::clear()
{
m_buffer.clear();
}
//--------------------------------------------------------------
} // namespace sdi_toolBox::common::utils
sdi_toolBox::common::utils::CircularBuffer
Fixed-size circular buffer with compile-time capacity and no-overwrite behavior.
Definition circularBuffer.h:49
sdi_toolBox::common::utils::CircularBuffer::front
std::optional< T > front() const
Return (without removing) the oldest element.
Definition circularBuffer.h:204
sdi_toolBox::common::utils::CircularBuffer::push
bool push(const T &value)
Try to append a value to the buffer.
Definition circularBuffer.h:178
sdi_toolBox::common::utils::CircularBuffer::pop
std::optional< T > pop()
Remove and return the oldest element from the buffer.
Definition circularBuffer.h:188
sdi_toolBox::common::utils::CircularBuffer::empty
bool empty() const
Check whether the buffer is empty.
Definition circularBuffer.h:235
sdi_toolBox::common::utils::CircularBuffer::full
bool full() const
Check whether the buffer is full.
Definition circularBuffer.h:242
sdi_toolBox::common::utils::CircularBuffer::back
std::optional< T > back() const
Return (without removing) the newest element.
Definition circularBuffer.h:220
sdi_toolBox::common::utils::CircularBuffer::clear
void clear()
Clear the buffer and reset internal indices.
Definition circularBuffer.h:263
sdi_toolBox::common::utils::CircularBuffer::size
std::size_t size() const
Number of elements currently stored in the buffer.
Definition circularBuffer.h:249
sdi_toolBox::common::utils::CircularBuffer::capacity
constexpr std::size_t capacity() const
Compile-time capacity of the buffer.
Definition circularBuffer.h:256
sdi_toolBox::common::utils::RingBuffer
Fixed-size ring (circular) buffer with compile-time capacity and overwrite-on-full behavior.
Definition ringBuffer.h:50
sdi_toolBox::common::utils::RingBuffer::push
bool push(const T &value)
Append a value to the buffer.
Definition ringBuffer.h:196
sdi_toolBox::common::utils
General-purpose utility functions shared across all sdi_toolBox modules.
Definition circularBuffer.h:18
ringBuffer.h