POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
shm_ring_buffer< T, TableType > Class Template Reference

Lock-free ring buffer for high-throughput streaming data. More...

#include <shm_ring_buffer.h>

Public Types

using value_type = T
 
using size_type = size_t
 

Public Member Functions

template<typename ShmType >
 shm_ring_buffer (ShmType &shm, std::string_view name, size_t capacity=0)
 
bool push (const T &value) noexcept
 Push a single element.
 
size_t push_bulk (std::span< const T > values) noexcept
 Push multiple elements efficiently.
 
std::optional< T > pop () noexcept
 Pop a single element.
 
size_t pop_bulk (std::span< T > values) noexcept
 Pop multiple elements efficiently.
 
size_t peek_bulk (size_t offset, std::span< T > values) const noexcept
 Peek at elements without consuming them.
 
size_t get_last_n (size_t n, std::span< T > values) const noexcept
 Get the last N elements (most recent) Useful for getting trailing sensor data.
 
void skip (size_t count) noexcept
 Skip elements without reading them.
 
void clear () noexcept
 Clear the buffer (reset read/write positions)
 
size_t capacity () const noexcept
 
size_t size () const noexcept
 
bool empty () const noexcept
 
bool full () const noexcept
 
size_t available_space () const noexcept
 
uint64_t total_written () const noexcept
 Get total elements written (useful for monitoring data rate)
 
uint64_t total_read () const noexcept
 
std::string_view name () const noexcept
 
void push_overwrite (const T &value) noexcept
 Force overwrite when full (converts to circular overwrite mode) Useful for continuous sensor streams where latest data is most important.
 

Detailed Description

template<typename T, typename TableType = shm_table>
requires std::is_trivially_copyable_v<T>
class shm_ring_buffer< T, TableType >

Lock-free ring buffer for high-throughput streaming data.

Optimized for bulk operations and sensor data streams. Supports multiple readers that can read without consuming.

Better than queue when you need:

  • Bulk read/write operations
  • Non-consuming reads (peeking at data)
  • Historical data access (last N samples)
Template Parameters
TElement type
TableTypeMetadata table type

Definition at line 24 of file shm_ring_buffer.h.

Member Typedef Documentation

◆ size_type

template<typename T , typename TableType = shm_table>
using shm_ring_buffer< T, TableType >::size_type = size_t

Definition at line 45 of file shm_ring_buffer.h.

◆ value_type

template<typename T , typename TableType = shm_table>
using shm_ring_buffer< T, TableType >::value_type = T

Definition at line 44 of file shm_ring_buffer.h.

Constructor & Destructor Documentation

◆ shm_ring_buffer()

template<typename T , typename TableType = shm_table>
template<typename ShmType >
shm_ring_buffer< T, TableType >::shm_ring_buffer ( ShmType &  shm,
std::string_view  name,
size_t  capacity = 0 
)
inline

Member Function Documentation

◆ available_space()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::available_space ( ) const
inlinenoexcept

◆ capacity()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::capacity ( ) const
inlinenoexcept

◆ clear()

template<typename T , typename TableType = shm_table>
void shm_ring_buffer< T, TableType >::clear ( )
inlinenoexcept

Clear the buffer (reset read/write positions)

Definition at line 226 of file shm_ring_buffer.h.

◆ empty()

template<typename T , typename TableType = shm_table>
bool shm_ring_buffer< T, TableType >::empty ( ) const
inlinenoexcept

Definition at line 242 of file shm_ring_buffer.h.

References shm_ring_buffer< T, TableType >::size().

◆ full()

template<typename T , typename TableType = shm_table>
bool shm_ring_buffer< T, TableType >::full ( ) const
inlinenoexcept

◆ get_last_n()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::get_last_n ( size_t  n,
std::span< T >  values 
) const
inlinenoexcept

Get the last N elements (most recent) Useful for getting trailing sensor data.

Definition at line 195 of file shm_ring_buffer.h.

Referenced by sensor_streaming_example().

◆ name()

template<typename T , typename TableType = shm_table>
std::string_view shm_ring_buffer< T, TableType >::name ( ) const
inlinenoexcept

Definition at line 265 of file shm_ring_buffer.h.

Referenced by shm_ring_buffer< T, TableType >::shm_ring_buffer().

◆ peek_bulk()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::peek_bulk ( size_t  offset,
std::span< T >  values 
) const
inlinenoexcept

Peek at elements without consuming them.

Parameters
offsetHow many elements to skip from the front
[out]valuesBuffer to store peeked elements
Returns
Number of elements actually peeked

Definition at line 173 of file shm_ring_buffer.h.

◆ pop()

template<typename T , typename TableType = shm_table>
std::optional< T > shm_ring_buffer< T, TableType >::pop ( )
inlinenoexcept

Pop a single element.

Returns
Optional containing value if available

Definition at line 134 of file shm_ring_buffer.h.

◆ pop_bulk()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::pop_bulk ( std::span< T >  values)
inlinenoexcept

Pop multiple elements efficiently.

Parameters
[out]valuesBuffer to store popped elements
Returns
Number of elements actually popped

Definition at line 152 of file shm_ring_buffer.h.

Referenced by sensor_streaming_example().

◆ push()

template<typename T , typename TableType = shm_table>
bool shm_ring_buffer< T, TableType >::push ( const T &  value)
inlinenoexcept

Push a single element.

Returns
true if successful, false if buffer is full

Definition at line 98 of file shm_ring_buffer.h.

Referenced by sensor_streaming_example().

◆ push_bulk()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::push_bulk ( std::span< const T >  values)
inlinenoexcept

Push multiple elements efficiently.

Returns
Number of elements actually pushed

Definition at line 115 of file shm_ring_buffer.h.

◆ push_overwrite()

template<typename T , typename TableType = shm_table>
void shm_ring_buffer< T, TableType >::push_overwrite ( const T &  value)
inlinenoexcept

Force overwrite when full (converts to circular overwrite mode) Useful for continuous sensor streams where latest data is most important.

Definition at line 273 of file shm_ring_buffer.h.

◆ size()

template<typename T , typename TableType = shm_table>
size_t shm_ring_buffer< T, TableType >::size ( ) const
inlinenoexcept

◆ skip()

template<typename T , typename TableType = shm_table>
void shm_ring_buffer< T, TableType >::skip ( size_t  count)
inlinenoexcept

Skip elements without reading them.

Definition at line 213 of file shm_ring_buffer.h.

◆ total_read()

template<typename T , typename TableType = shm_table>
uint64_t shm_ring_buffer< T, TableType >::total_read ( ) const
inlinenoexcept

Definition at line 261 of file shm_ring_buffer.h.

◆ total_written()

template<typename T , typename TableType = shm_table>
uint64_t shm_ring_buffer< T, TableType >::total_written ( ) const
inlinenoexcept

Get total elements written (useful for monitoring data rate)

Definition at line 257 of file shm_ring_buffer.h.

Referenced by sensor_streaming_example().


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