xentara-utils v2.1
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::lockFree::SingleElementBuffer< Element > Class Template Referencefinal

A lock-free single element buffer. More...

#include <xentara/utils/lockFree/SingleElementBuffer.hpp>

+ Inheritance diagram for xentara::utils::lockFree::SingleElementBuffer< Element >:

Public Member Functions

auto push (const Element &value) -> void
 Buffers a value.
 
auto push (Element &&value) -> void
 Buffers a value.
 
template<typename... Arguments>
requires std::is_constructible_v<Element, Arguments &&...>
auto emplace (Arguments &&...arguments) -> void
 Constructs a value within the buffer.
 
template<typename ListElement , typename... Arguments>
requires std::is_constructible_v<Element, std::initializer_list<ListElement>, Arguments &&...>
auto emplace (std::initializer_list< ListElement > list, Arguments &&... arguments) -> void
 Constructs a value within the buffer using an initializer list.
 
auto pop () -> std::optional< Element >
 Gets the buffered value and clears the buffer.
 

Detailed Description

template<typename Element>
class xentara::utils::lockFree::SingleElementBuffer< Element >

A lock-free single element buffer.

This type of buffer can hold exactly one element. It is typically used in a producer-consumer system where the consumer consumes values asynchronously.

Template Parameters
Element

Member Function Documentation

◆ emplace() [1/2]

template<typename Element >
template<typename... Arguments>
requires std::is_constructible_v<Element, Arguments &&...>
auto xentara::utils::lockFree::SingleElementBuffer< Element >::emplace ( Arguments &&...  arguments) -> void

Constructs a value within the buffer.

Any value already in the buffer will be replaced.

Attention
This function should generally only be called if the constructor of the element will not throw an exception. While this function is thread safe in principle, an exception thrown by the constructor may cause a value pushed by another thread to be lost if a collision occurs. If there is a possibility that the constructor will throw an exception during normal operation, then the element should be constructed beforehand, and moved into the buffer using push().
Parameters
argumentsThe arguments for the constructor of the element

◆ emplace() [2/2]

template<typename Element >
template<typename ListElement , typename... Arguments>
requires std::is_constructible_v<Element, std::initializer_list<ListElement>, Arguments &&...>
auto xentara::utils::lockFree::SingleElementBuffer< Element >::emplace ( std::initializer_list< ListElement >  list,
Arguments &&...  arguments 
) -> void

Constructs a value within the buffer using an initializer list.

Any value already in the buffer will be replaced.

Attention
This function should generally only be called if the constructor of the element will not throw an exception. While this function is thread safe in principle, an exception thrown by the constructor may cause a value pushed by another thread to be lost if a collision occurs. If there is a possibility that the constructor will throw an exception during normal operation, then the element should be constructed beforehand, and moved into the buffer using push().
Parameters
listAn initializer list to pass as the first argument to the constructor of the element
argumentsThe remaining arguments for the constructor of the element

◆ pop()

template<typename Element >
auto xentara::utils::lockFree::SingleElementBuffer< Element >::pop ( ) -> std::optional<Element>

Gets the buffered value and clears the buffer.

Returns
The buffered value, or std::nullopt if the buffer was empty.

◆ push() [1/2]

template<typename Element >
auto xentara::utils::lockFree::SingleElementBuffer< Element >::push ( const Element &  value) -> void

Buffers a value.

Any value already in the buffer will be replaced.

Parameters
valueThe value to place in the buffer

◆ push() [2/2]

template<typename Element >
auto xentara::utils::lockFree::SingleElementBuffer< Element >::push ( Element &&  value) -> void

Buffers a value.

Any value already in the buffer will be replaced.

Parameters
valueThe value to place in the buffer