Monado OpenXR Runtime
xrt::auxiliary::util::detail::RingBufferHelper Class Reference

All the bookkeeping for adapting a fixed-size array to a ring buffer. More...

#include <util/u_template_historybuf_impl_helpers.hpp>

Public Member Functions

constexpr RingBufferHelper (size_t capacity)
 Construct for a given size. More...
 
 RingBufferHelper (RingBufferHelper const &)=default
 
 RingBufferHelper (RingBufferHelper &&)=default
 
RingBufferHelperoperator= (RingBufferHelper const &)=default
 
RingBufferHelperoperator= (RingBufferHelper &&)=default
 
bool age_to_inner_index (size_t age, size_t &out_inner_idx) const noexcept
 Get the inner index for a given age (if possible) More...
 
bool clamped_age_to_inner_index (size_t age, size_t &out_inner_idx) const noexcept
 Get the inner index for a given age, clamping it if out of bounds. More...
 
bool index_to_inner_index (size_t index, size_t &out_inner_idx) const noexcept
 Get the inner index for a given index (if possible) More...
 
bool empty () const noexcept
 Is the buffer empty? More...
 
size_t size () const noexcept
 How many elements are in the buffer? More...
 
size_t push_back_location () noexcept
 Update internal state for pushing an element to the back, and return the inner index to store the element at. More...
 
void pop_front () noexcept
 Record the logical removal of the front element, if any. More...
 
bool pop_back () noexcept
 Record the logical removal of the back element, if any. More...
 
size_t front_inner_index () const noexcept
 Get the inner index of the front (oldest) value, or capacity_ if empty. More...
 
size_t back_inner_index () const noexcept
 Get the inner index of the back (newest) value, or capacity_ if empty. More...
 
void clear ()
 

Detailed Description

All the bookkeeping for adapting a fixed-size array to a ring buffer.

This is all the guts of the ring buffer except for the actual buffer. We split it out to

  • reduce code size (this can be shared among multiple types)
  • separate concerns (keeping track of the indices separate from owning the buffer)
  • allow easier implementation of both const iterators and non-const iterators

There are a few types of "index":

  • just "index": an index where the least-recently-added element still remaining is numbered 0, the next oldest is 1, etc. (Chronological)
  • "age": Reverse chronological order: 0 means most-recently-added, 1 means the one before it, etc.
  • "inner" index: the index in the underlying array/buffer. It's called "inner" because the consumer of the ring buffer should not ever deal with this index, it's an implementation detail.

Constructor & Destructor Documentation

◆ RingBufferHelper()

constexpr xrt::auxiliary::util::detail::RingBufferHelper::RingBufferHelper ( size_t  capacity)
inlineexplicitconstexpr

Construct for a given size.

Member Function Documentation

◆ age_to_inner_index()

bool xrt::auxiliary::util::detail::RingBufferHelper::age_to_inner_index ( size_t  age,
size_t out_inner_idx 
) const
inlinenoexcept

Get the inner index for a given age (if possible)

References empty().

Referenced by clamped_age_to_inner_index(), and u_id_ringbuffer::u_id_ringbuffer_get_at_age().

◆ back_inner_index()

size_t xrt::auxiliary::util::detail::RingBufferHelper::back_inner_index ( ) const
inlinenoexcept

Get the inner index of the back (newest) value, or capacity_ if empty.

References empty().

Referenced by u_id_ringbuffer::u_id_ringbuffer_get_back().

◆ clamped_age_to_inner_index()

bool xrt::auxiliary::util::detail::RingBufferHelper::clamped_age_to_inner_index ( size_t  age,
size_t out_inner_idx 
) const
inlinenoexcept

Get the inner index for a given age, clamping it if out of bounds.

References age_to_inner_index(), and empty().

Referenced by xrt::auxiliary::util::HistoryBuffer< T, MaxSize >::get_at_clamped_age(), and u_id_ringbuffer::u_id_ringbuffer_get_at_clamped_age().

◆ empty()

bool xrt::auxiliary::util::detail::RingBufferHelper::empty ( ) const
inlinenoexcept

◆ front_inner_index()

size_t xrt::auxiliary::util::detail::RingBufferHelper::front_inner_index ( ) const
inlinenoexcept

Get the inner index of the front (oldest) value, or capacity_ if empty.

References empty().

Referenced by u_id_ringbuffer::u_id_ringbuffer_get_front().

◆ index_to_inner_index()

bool xrt::auxiliary::util::detail::RingBufferHelper::index_to_inner_index ( size_t  index,
size_t out_inner_idx 
) const
inlinenoexcept

Get the inner index for a given index (if possible)

References empty().

Referenced by u_id_ringbuffer::u_id_ringbuffer_get_at_index().

◆ pop_back()

bool xrt::auxiliary::util::detail::RingBufferHelper::pop_back ( )
inlinenoexcept

Record the logical removal of the back element, if any.

Returns false if the buffer is empty. Does not actually modify the value stored in the backing array.

References empty().

Referenced by xrt::auxiliary::util::HistoryBuffer< T, MaxSize >::pop_back(), and u_id_ringbuffer::u_id_ringbuffer_pop_back().

◆ pop_front()

void xrt::auxiliary::util::detail::RingBufferHelper::pop_front ( )
inlinenoexcept

Record the logical removal of the front element, if any.

Does nothing if the buffer is empty. Does not actually modify the value stored in the backing array.

References empty().

Referenced by xrt::auxiliary::util::HistoryBuffer< T, MaxSize >::pop_front(), and u_id_ringbuffer::u_id_ringbuffer_pop_front().

◆ push_back_location()

size_t xrt::auxiliary::util::detail::RingBufferHelper::push_back_location ( )
inlinenoexcept

Update internal state for pushing an element to the back, and return the inner index to store the element at.

This is the implementation of "push_back" excluding all the messy "actually dealing with the data" part ;-)

Referenced by u_id_ringbuffer::u_id_ringbuffer_push_back().

◆ size()

size_t xrt::auxiliary::util::detail::RingBufferHelper::size ( ) const
inlinenoexcept

How many elements are in the buffer?

Referenced by u_id_ringbuffer::u_id_ringbuffer_get_size().


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