CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
commrat::TimestampedRingBuffer< T, MaxSize > Class Template Reference

Thread-safe timestamped ring buffer with getData lookup. More...

#include <commrat/mailbox/timestamped_ring_buffer.hpp>

Public Types

using value_type = T
 
using size_type = std::size_t
 

Public Member Functions

 TimestampedRingBuffer (std::chrono::milliseconds default_tolerance=std::chrono::milliseconds(50))
 Constructor with optional sync tolerance.
 
size_type size () const
 Get current number of stored messages.
 
bool empty () const
 Check if buffer is empty.
 
bool full () const
 Check if buffer is full.
 
void clear ()
 Clear all messages from buffer.
 
void push (const T &message)
 Push new message with timestamp.
 
void push (T &&message)
 Push new message via move.
 
std::optional< T > getData (uint64_t timestamp, std::chrono::milliseconds tolerance=std::chrono::milliseconds(-1), InterpolationMode mode=InterpolationMode::NEAREST) const
 
std::pair< uint64_t, uint64_t > getTimestampRange () const
 Get timestamp range currently in buffer.
 

Static Public Member Functions

static constexpr size_type capacity ()
 Get maximum capacity.
 

Detailed Description

template<typename T, std::size_t MaxSize = 100>
class commrat::TimestampedRingBuffer< T, MaxSize >

Thread-safe timestamped ring buffer with getData lookup.

Wraps SeRTial::RingBuffer with:

  • Thread-safe push/get operations
  • Timestamp-based lookup (getData)
  • Multiple interpolation modes
  • Maintains temporal ordering (must push in timestamp order)
Template Parameters
TMessage type (must have .timestamp member)
MaxSizeMaximum capacity (default: 100 messages)

Requirements for T:

  • Must have uint64_t timestamp field (milliseconds since epoch)
  • Must be copyable or movable
  • Timestamps must be monotonically increasing on push

Thread Safety:

  • Multiple concurrent readers (shared lock)
  • Single writer (exclusive lock)
  • Lock-free reads possible in future optimization

Example:

struct IMUData {
uint64_t timestamp; // Required!
float ax, ay, az;
};
// Producer thread
history.push(IMUData{.timestamp = 1000, .ax = 1.0f, ...});
// Consumer thread (secondary input)
auto data = history.getData(1050, std::chrono::milliseconds(50),
if (data) {
// Use data synchronized to timestamp 1050
}
Thread-safe timestamped ring buffer with getData lookup.
void push(const T &message)
Push new message with timestamp.
std::optional< T > getData(uint64_t timestamp, std::chrono::milliseconds tolerance=std::chrono::milliseconds(-1), InterpolationMode mode=InterpolationMode::NEAREST) const
@ NEAREST
Return closest message by timestamp.

Definition at line 102 of file timestamped_ring_buffer.hpp.

Member Typedef Documentation

◆ size_type

template<typename T , std::size_t MaxSize = 100>
using commrat::TimestampedRingBuffer< T, MaxSize >::size_type = std::size_t

◆ value_type

template<typename T , std::size_t MaxSize = 100>
using commrat::TimestampedRingBuffer< T, MaxSize >::value_type = T

Constructor & Destructor Documentation

◆ TimestampedRingBuffer()

template<typename T , std::size_t MaxSize = 100>
commrat::TimestampedRingBuffer< T, MaxSize >::TimestampedRingBuffer ( std::chrono::milliseconds  default_tolerance = std::chrono::milliseconds(50))
inlineexplicit

Constructor with optional sync tolerance.

Parameters
default_toleranceDefault tolerance for timestamp matching (ms)

Definition at line 118 of file timestamped_ring_buffer.hpp.

Member Function Documentation

◆ capacity()

template<typename T , std::size_t MaxSize = 100>
static constexpr size_type commrat::TimestampedRingBuffer< T, MaxSize >::capacity ( )
inlinestaticconstexpr

Get maximum capacity.

Returns
Maximum number of messages buffer can hold
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 140 of file timestamped_ring_buffer.hpp.

◆ clear()

template<typename T , std::size_t MaxSize = 100>
void commrat::TimestampedRingBuffer< T, MaxSize >::clear ( )
inline

Clear all messages from buffer.

Note
Thread-safe (write lock)
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 168 of file timestamped_ring_buffer.hpp.

◆ empty()

template<typename T , std::size_t MaxSize = 100>
bool commrat::TimestampedRingBuffer< T, MaxSize >::empty ( ) const
inline

Check if buffer is empty.

Returns
true if no messages stored
Note
Thread-safe (read lock)
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 149 of file timestamped_ring_buffer.hpp.

◆ full()

template<typename T , std::size_t MaxSize = 100>
bool commrat::TimestampedRingBuffer< T, MaxSize >::full ( ) const
inline

Check if buffer is full.

Returns
true if buffer is at maximum capacity
Note
Thread-safe (read lock)
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 159 of file timestamped_ring_buffer.hpp.

◆ getData()

template<typename T , std::size_t MaxSize = 100>
std::optional< T > commrat::TimestampedRingBuffer< T, MaxSize >::getData ( uint64_t  timestamp,
std::chrono::milliseconds  tolerance = std::chrono::milliseconds(-1),
InterpolationMode  mode = InterpolationMode::NEAREST 
) const
inline

◆ getTimestampRange()

template<typename T , std::size_t MaxSize = 100>
std::pair< uint64_t, uint64_t > commrat::TimestampedRingBuffer< T, MaxSize >::getTimestampRange ( ) const
inline

Get timestamp range currently in buffer.

Returns
{oldest_timestamp, newest_timestamp} or {0, 0} if empty
Note
Thread-safe (read lock)
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 329 of file timestamped_ring_buffer.hpp.

◆ push() [1/2]

template<typename T , std::size_t MaxSize = 100>
void commrat::TimestampedRingBuffer< T, MaxSize >::push ( const T &  message)
inline

Push new message with timestamp.

Messages must be pushed in timestamp order (monotonically increasing). If buffer is full, overwrites oldest message.

Parameters
messageMessage to store (must have .timestamp field)
Note
Thread-safe (write lock)
O(1) time complexity
Warning
Violating timestamp order leads to undefined getData behavior!
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 191 of file timestamped_ring_buffer.hpp.

References commrat::TimestampAccessor< T >::get().

◆ push() [2/2]

template<typename T , std::size_t MaxSize = 100>
void commrat::TimestampedRingBuffer< T, MaxSize >::push ( T &&  message)
inline

Push new message via move.

Parameters
messageMessage to move into buffer
Note
Thread-safe (write lock)

Definition at line 219 of file timestamped_ring_buffer.hpp.

References commrat::TimestampAccessor< T >::get().

◆ size()

template<typename T , std::size_t MaxSize = 100>
size_type commrat::TimestampedRingBuffer< T, MaxSize >::size ( ) const
inline

Get current number of stored messages.

Returns
Number of messages in buffer
Note
Thread-safe (read lock)
Examples
/home/runner/work/CommRaT/CommRaT/include/commrat/mailbox/timestamped_ring_buffer.hpp.

Definition at line 131 of file timestamped_ring_buffer.hpp.


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