MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Memory::RingBuffer< T, StoragePolicy, ConcurrencyPolicy, AccessPattern > Class Template Reference

Policy-driven unified circular buffer implementation. More...

#include <RingBuffer.hpp>

+ Inheritance diagram for MayaFlux::Memory::RingBuffer< T, StoragePolicy, ConcurrencyPolicy, AccessPattern >:
+ Collaboration diagram for MayaFlux::Memory::RingBuffer< T, StoragePolicy, ConcurrencyPolicy, AccessPattern >:

Public Types

using value_type = T
 
using storage_type = StoragePolicy
 
using reference = T &
 
using const_reference = const T &
 

Public Member Functions

 RingBuffer (size_t initial_capacity=64)
 Construct ring buffer with runtime capacity (DynamicStorage only)
 
 RingBuffer ()=default
 Default construct ring buffer (FixedStorage only) Capacity determined by template parameter.
 
bool push (const T &value) noexcept(is_lock_free)
 Push element into buffer.
 
std::optional< T > pop () noexcept(is_lock_free)
 Pop element from buffer.
 
const_reference peek_newest () const
 Peek at newest element without removing.
 
const_reference peek_oldest () const
 Peek at oldest element without removing.
 
const_reference operator[] (size_t index) const
 Access element by index (delay line style)
 
void overwrite_newest (const T &value)
 Overwrite newest element without advancing write position.
 
std::span< T > linearized_view () const
 Get linearized view of buffer contents.
 
std::span< T > linearized_view_mut ()
 Get mutable linearized view for modification.
 
std::vector< T > snapshot () const
 Thread-safe snapshot of buffer contents.
 
bool empty () const noexcept
 Check if buffer is empty.
 
size_t size () const noexcept
 Get approximate element count.
 
size_t capacity () const noexcept
 Get buffer capacity.
 
void resize (size_t new_capacity)
 Resize buffer capacity (DynamicStorage only)
 
void reset () noexcept
 Clear buffer contents and reset indices.
 

Static Public Attributes

static constexpr bool is_lock_free = ConcurrencyPolicy::is_thread_safe
 
static constexpr bool is_resizable = StoragePolicy::is_resizable
 
static constexpr bool is_delay_line = AccessPattern::push_front
 

Private Types

using State = typename ConcurrencyPolicy::template State< T, StoragePolicy >
 

Private Member Functions

bool push_lockfree (const T &value) noexcept
 
std::optional< T > pop_lockfree () noexcept
 
bool push_singlethread (const T &value) noexcept
 
std::optional< T > pop_singlethread () noexcept
 

Private Attributes

StoragePolicy m_storage
 
State m_state
 
std::vector< T > m_linearized
 

Detailed Description

template<typename T, typename StoragePolicy, typename ConcurrencyPolicy = SingleThreadedPolicy, typename AccessPattern = QueueAccess>
class MayaFlux::Memory::RingBuffer< T, StoragePolicy, ConcurrencyPolicy, AccessPattern >

Policy-driven unified circular buffer implementation.

Provides a single, flexible ring buffer that adapts to different use cases through compile-time policy selection. Policies control storage allocation, thread safety, and access patterns independently.

Design Philosophy:

  • Zero runtime cost: All policy dispatch via templates (no virtual calls)
  • Type-safe: Policy constraints enforced at compile time
  • Composable: Policies combine orthogonally
  • Minimal: No features you don't need based on policy selection

Policy Dimensions:

  1. Storage: FixedStorage (compile-time) vs DynamicStorage (runtime)
  2. Concurrency: LockFreePolicy (SPSC) vs SingleThreadedPolicy (no sync)
  3. Access: QueueAccess (FIFO) vs HistoryBufferAccess (newest-first)

Common Configurations:

// Lock-free input event queue (realtime → worker thread)
using InputQueue = RingBuffer<InputValue,
// Single-threaded audio delay line (DSP processing)
using AudioDelay = RingBuffer<double,
// Lock-free logging buffer (audio thread → disk writer)
using LogBuffer = RingBuffer<RealtimeEntry,
// Polynomial node history buffer (single-threaded DSP)
using NodeHistory = RingBuffer<double,
Policy-driven unified circular buffer implementation.
Runtime-resizable storage using std::vector.
Compile-time fixed-capacity storage using std::array.
History buffer semantics (newest sample first)
Lock-free SPSC (Single Producer Single Consumer) concurrency.
FIFO queue semantics (oldest data first)
Non-atomic single-threaded operation.

Migration from Legacy Implementations:

Template Parameters
TElement type
StoragePolicyFixedStorage<T,N> or DynamicStorage<T>
ConcurrencyPolicyLockFreePolicy or SingleThreadedPolicy
AccessPatternQueueAccess or HistoryBufferAccess

Definition at line 666 of file RingBuffer.hpp.


The documentation for this class was generated from the following file: