Skip to content

ludus::foundation::core::RingBuffer

Ludus Wiki · API overview · Browse API

Class

Bounded first-in, first-out queue in inline storage; never allocates.
TryAdd appends at the back and RemoveFirst removes from the front. Index 0 is always the oldest element. Adding to a full ring fails and changes nothing. Elements never move while they are live, so a borrow stays valid until that element is removed or the ring is cleared, copied over, moved from or destroyed. Moving the whole ring moves its elements; a moved-from ring is empty.

Note

One owner; synchronize access externally. This is not a concurrent queue. Element destruction must not throw.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

#include <ludus/foundation/containers/ring_buffer.hpp>
template <typename ElementType, usize Count>
class ludus::foundation::core::RingBuffer

Types

ValueType

using ludus::foundation::core::RingBuffer< ElementType, Count >::ValueType = ElementType
Stored element type.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

SizeType

using ludus::foundation::core::RingBuffer< ElementType, Count >::SizeType = usize
Size and logical index type.

Related types: usize

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

Methods

RingBuffer

ludus::foundation::core::RingBuffer< ElementType, Count >::RingBuffer() noexcept=default
Empty ring; no element is constructed.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

RingBuffer

ludus::foundation::core::RingBuffer< ElementType, Count >::RingBuffer(const RingBuffer &other)
requires std::is_copy_constructible_v<ElementType>
Copy-construct other's elements in FIFO order. The copy starts at the first slot.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

RingBuffer

ludus::foundation::core::RingBuffer< ElementType, Count >::RingBuffer(RingBuffer &&other) noexcept
requires std::is_nothrow_move_constructible_v<ElementType>
Move-construct other's elements in FIFO order, then leave other empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

operator=

RingBuffer & ludus::foundation::core::RingBuffer< ElementType, Count >::operator=(const RingBuffer &other)
requires std::is_copy_constructible_v<ElementType>
Replace the contents with copies of other's elements in FIFO order.
The current elements are destroyed first. Self-assignment does nothing.

Related types: RingBuffer

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

operator=

RingBuffer & ludus::foundation::core::RingBuffer< ElementType, Count >::operator=(RingBuffer &&other) noexcept
requires std::is_nothrow_move_constructible_v<ElementType>
Destroy the current elements, then move other's elements in and leave other empty.
Self-assignment does nothing.

Related types: RingBuffer

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

~RingBuffer

ludus::foundation::core::RingBuffer< ElementType, Count >::~RingBuffer()=default
requires std::is_trivially_destructible_v<ElementType>
Trivial destruction when ElementType is trivially destructible.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

~RingBuffer

ludus::foundation::core::RingBuffer< ElementType, Count >::~RingBuffer()
Destroy the live elements.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetSize

LUDUS_INLINE usize ludus::foundation::core::RingBuffer< ElementType, Count >::GetSize() const noexcept
Number of live elements.

Related types: usize

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

IsEmpty

LUDUS_INLINE bool ludus::foundation::core::RingBuffer< ElementType, Count >::IsEmpty() const noexcept
True when no element is live.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

IsFull

LUDUS_INLINE bool ludus::foundation::core::RingBuffer< ElementType, Count >::IsFull() const noexcept
True when every slot is live, so TryAdd fails. Always true for Count 0.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

operator[]

LUDUS_INLINE ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::operator[](usize index) noexcept
Element index positions after the oldest.

Precondition

index < GetSize() (asserted in checked builds).

Related types: usize

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

operator[]

LUDUS_INLINE const ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::operator[](usize index) const noexcept
Read-only element index positions after the oldest.

Precondition

index < GetSize() (asserted in checked builds).

Related types: usize

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetFirst

LUDUS_INLINE ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::GetFirst() noexcept
Oldest element, the next one RemoveFirst removes.

Precondition

The ring is not empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetFirst

LUDUS_INLINE const ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::GetFirst() const noexcept
Read-only oldest element.

Precondition

The ring is not empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetLast

LUDUS_INLINE ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::GetLast() noexcept
Newest element.

Precondition

The ring is not empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetLast

LUDUS_INLINE const ElementType & ludus::foundation::core::RingBuffer< ElementType, Count >::GetLast() const noexcept
Read-only newest element.

Precondition

The ring is not empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetLeadingSpan

std::span< ElementType > ludus::foundation::core::RingBuffer< ElementType, Count >::GetLeadingSpan() noexcept
The oldest elements, up to the end of the storage, in FIFO order.
GetLeadingSpan() followed by GetTrailingSpan() visits every live element in FIFO order. Both are empty for an empty ring. Invalidated by any operation that removes the viewed elements.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetLeadingSpan

std::span< const ElementType > ludus::foundation::core::RingBuffer< ElementType, Count >::GetLeadingSpan() const noexcept
Read-only form of GetLeadingSpan().

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetTrailingSpan

std::span< ElementType > ludus::foundation::core::RingBuffer< ElementType, Count >::GetTrailingSpan() noexcept
The newer elements that wrapped to the start of the storage; empty when nothing wrapped.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

GetTrailingSpan

std::span< const ElementType > ludus::foundation::core::RingBuffer< ElementType, Count >::GetTrailingSpan() const noexcept
Read-only form of GetTrailingSpan().

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

TryAdd

bool ludus::foundation::core::RingBuffer< ElementType, Count >::TryAdd(const ElementType &value)
Append a copy of value at the back unless the ring is full.
No live element moves, so value may refer to an element of this ring.

Returns

False when full; the ring and value are unchanged.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

TryAdd

bool ludus::foundation::core::RingBuffer< ElementType, Count >::TryAdd(ElementType &&value)
Append by moving value at the back unless the ring is full.

Returns

False when full; the ring is unchanged and value is not moved from.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

TryAddInPlace

template <typename... Args>
ElementType * ludus::foundation::core::RingBuffer< ElementType, Count >::TryAddInPlace(Args &&... args)
Append an element constructed from args at the back unless the ring is full.
No live element moves, so args may refer to elements of this ring.

Returns

The new element, or nullptr when full with the ring and args untouched.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

RemoveFirst

void ludus::foundation::core::RingBuffer< ElementType, Count >::RemoveFirst() noexcept
Destroy the oldest element. O(1).
Invalidates borrows of that element only.

Precondition

The ring is not empty.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

Clear

void ludus::foundation::core::RingBuffer< ElementType, Count >::Clear() noexcept
Destroy every live element and restart at the first slot.

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

Static methods

GetCapacity

static LUDUS_INLINE constexpr usize ludus::foundation::core::RingBuffer< ElementType, Count >::GetCapacity() noexcept
Fixed capacity; always Count.

Related types: usize

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp

Members

operator==

bool operator==(const RingBuffer &left, const RingBuffer &right)
Element-wise equality in FIFO order; the physical slot positions are ignored.

Related types: RingBuffer

Declared in modules/foundation/containers/include/ludus/foundation/containers/ring_buffer.hpp