C++ Channel
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends | List of all members
msd::channel< T, Storage > Class Template Reference

Thread-safe container for sharing data between threads. More...

#include <channel.hpp>

Public Types

using value_type = T
 The type of elements stored in the channel.
 
using iterator = blocking_iterator< channel< T, Storage > >
 The iterator type used to traverse the channel.
 
using size_type = std::size_t
 The type used to represent sizes and counts.
 

Public Member Functions

template<typename S = Storage, typename std::enable_if< is_static_storage< S >::value, int >::type = 0>
constexpr channel ()
 Creates a buffered channel if Storage is static (has static capacity member)
 
template<typename S = Storage, typename std::enable_if<!is_static_storage< S >::value, int >::type = 0>
constexpr channel ()
 Creates an unbuffered channel if Storage is not static (does not have static capacity member).
 
template<typename S = Storage, typename std::enable_if<!is_static_storage< S >::value, int >::type = 0>
constexpr channel (const size_type capacity)
 Creates a buffered channel if Storage is not static (does not have static capacity member).
 
template<typename Type >
bool write (Type &&value)
 Pushes an element into the channel.
 
bool read (T &out)
 Pops an element from the channel.
 
NODISCARD size_type size () const noexcept
 Returns the current size of the channel.
 
NODISCARD bool empty () const noexcept
 Checks if the channel is empty.
 
void close () noexcept
 Closes the channel, no longer accepting new elements.
 
NODISCARD bool closed () const noexcept
 Checks if the channel has been closed.
 
NODISCARD bool drained () noexcept
 Checks if the channel has been closed and is empty.
 
iterator begin () noexcept
 Returns an iterator to the beginning of the channel.
 
iterator end () noexcept
 Returns an iterator representing the end of the channel.
 
 channel (const channel &)=delete
 
channeloperator= (const channel &)=delete
 
 channel (channel &&)=delete
 
channeloperator= (channel &&)=delete
 

Friends

template<typename Type , typename Store >
channel< typename std::decay< Type >::type, Store > & operator<< (channel< typename std::decay< Type >::type, Store > &chan, Type &&value)
 Pushes an element into the channel.
 
template<typename Type , typename Store >
channel< Type, Store > & operator>> (channel< Type, Store > &chan, Type &out)
 Pops an element from the channel.
 

Detailed Description

template<typename T, typename Storage = default_storage<T>>
class msd::channel< T, Storage >

Thread-safe container for sharing data between threads.

Template Parameters
TThe type of the elements.
StorageThe storage type used to hold the elements. Default: msd::queue_storage.

Constructor & Destructor Documentation

◆ channel() [1/2]

template<typename T , typename Storage = default_storage<T>>
template<typename S = Storage, typename std::enable_if< is_static_storage< S >::value, int >::type = 0>
constexpr msd::channel< T, Storage >::channel ( )
inlineconstexpr

Creates a buffered channel if Storage is static (has static capacity member)

Note
Uses Storage::capacity as number of elements the channel can store before blocking.

◆ channel() [2/2]

template<typename T , typename Storage = default_storage<T>>
template<typename S = Storage, typename std::enable_if<!is_static_storage< S >::value, int >::type = 0>
constexpr msd::channel< T, Storage >::channel ( const size_type  capacity)
inlineexplicitconstexpr

Creates a buffered channel if Storage is not static (does not have static capacity member).

Parameters
capacityNumber of elements the channel can store before blocking.

Member Function Documentation

◆ begin()

template<typename T , typename Storage = default_storage<T>>
iterator msd::channel< T, Storage >::begin ( )
inlinenoexcept

Returns an iterator to the beginning of the channel.

Returns
A blocking iterator pointing to the start of the channel.

◆ closed()

template<typename T , typename Storage = default_storage<T>>
NODISCARD bool msd::channel< T, Storage >::closed ( ) const
inlinenoexcept

Checks if the channel has been closed.

Returns
true If no more elements can be added to the channel.
false Otherwise.

◆ drained()

template<typename T , typename Storage = default_storage<T>>
NODISCARD bool msd::channel< T, Storage >::drained ( )
inlinenoexcept

Checks if the channel has been closed and is empty.

Returns
true If nothing can be read anymore from the channel.
false Otherwise.

◆ empty()

template<typename T , typename Storage = default_storage<T>>
NODISCARD bool msd::channel< T, Storage >::empty ( ) const
inlinenoexcept

Checks if the channel is empty.

Returns
true If the channel contains no elements.
false Otherwise.

◆ end()

template<typename T , typename Storage = default_storage<T>>
iterator msd::channel< T, Storage >::end ( )
inlinenoexcept

Returns an iterator representing the end of the channel.

Returns
A blocking iterator representing the end condition.

◆ read()

template<typename T , typename Storage = default_storage<T>>
bool msd::channel< T, Storage >::read ( T out)
inline

Pops an element from the channel.

Parameters
outReference to the variable where the popped element will be stored.
Returns
true If an element was successfully read from the channel.
false If the channel is closed and empty.

◆ size()

template<typename T , typename Storage = default_storage<T>>
NODISCARD size_type msd::channel< T, Storage >::size ( ) const
inlinenoexcept

Returns the current size of the channel.

Returns
The number of elements in the channel.

◆ write()

template<typename T , typename Storage = default_storage<T>>
template<typename Type >
bool msd::channel< T, Storage >::write ( Type &&  value)
inline

Pushes an element into the channel.

Template Parameters
TypeThe type of the elements.
Parameters
valueThe element to be pushed into the channel.
Returns
true If an element was successfully pushed into the channel.
false If the channel is closed.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T , typename Storage = default_storage<T>>
template<typename Type , typename Store >
channel< typename std::decay< Type >::type, Store > & operator<< ( channel< typename std::decay< Type >::type, Store > &  chan,
Type &&  value 
)
friend

Pushes an element into the channel.

Parameters
chanChannel to write to.
valueValue to write.
Returns
Instance of channel.
Exceptions
closed_channelif channel is closed.

◆ operator>>

template<typename T , typename Storage = default_storage<T>>
template<typename Type , typename Store >
channel< Type, Store > & operator>> ( channel< Type, Store > &  chan,
Type out 
)
friend

Pops an element from the channel.

Parameters
chanChannel to read from.
outWhere to write read value.
Returns
Instance of channel.

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