Thunderbots Project
Loading...
Searching...
No Matches
ThreadSafeBuffer< T > Class Template Reference

#include <thread_safe_buffer.hpp>

Public Member Functions

 ThreadSafeBuffer (std::size_t buffer_size, bool log_buffer_full=true)
 
 ThreadSafeBuffer (const ThreadSafeBuffer &)=delete
 
std::optional< T > popLeastRecentlyAddedValue (Duration max_wait_time=Duration::fromSeconds(0))
 
std::optional< T > popMostRecentlyAddedValue (Duration max_wait_time=Duration::fromSeconds(0))
 
void push (const T &value)
 
bool empty () const
 

Detailed Description

template<typename T>
class ThreadSafeBuffer< T >

This class represents a buffer of objects

It's public API is fully thread-safe (meaning that it can be accessed by multiple threads at the same time without issue)

Template Parameters
TThe type of whatever is being buffered

Constructor & Destructor Documentation

◆ ThreadSafeBuffer()

template<typename T >
ThreadSafeBuffer ( std::size_t  buffer_size,
bool  log_buffer_full = true 
)
explicit

Creates a new ThreadSafeBuffer

Parameters
buffer_sizesize of the buffer
log_buffer_fullwhether or not to log when the buffer is full

Member Function Documentation

◆ empty()

template<typename T >
bool empty ( ) const

Returns whether or not the buffer is empty

Returns
True if the buffer is empty, false otherwise

◆ popLeastRecentlyAddedValue()

template<typename T >
std::optional< T > popLeastRecentlyAddedValue ( Duration  max_wait_time = Duration::fromSeconds(0))

Removes the value least recently added to the buffer and returns it

ex. if A,B,C were added to the buffer (in that order), this would return A

If the buffer is empty, this function will block until:

  • a value becomes available
  • the given amount of time is exceeded
  • the destructor of this class is called
Parameters
max_wait_timeThe maximum duration to wait for a new value before returning
Returns
The most recently added value to the buffer, or std::nullopt if none is available

◆ popMostRecentlyAddedValue()

template<typename T >
std::optional< T > popMostRecentlyAddedValue ( Duration  max_wait_time = Duration::fromSeconds(0))

Removes the value most recently added to the buffer and returns it

ex. if A,B,C were added to the buffer (in that order), this would return C

If the buffer is empty, this function will block until:

  • a value becomes available
  • the given amount of time is exceeded
  • the destructor of this class is called
Parameters
max_wait_timeThe maximum duration to wait for a new value before returning
Returns
The most recently added value to the buffer, or std::nullopt if none is available

◆ push()

template<typename T >
void push ( const T &  value)

Push the given value onto the buffer

If the buffer is already full, this will overwrite the least recently added value

Parameters
valueThe value to push onto the buffer

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