RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
BS::synced_stream Class Reference

A utility class to synchronize printing to one or more output streams by different threads. More...

#include <BS_thread_pool.hpp>

Public Member Functions

 synced_stream ()
 Construct a new synced stream which prints to std::cout.
template<typename... T>
 synced_stream (T &... streams)
 Construct a new synced stream which prints to the given output stream(s).
void add_stream (std::ostream &stream)
 Add a stream to the list of output streams to print to.
std::vector< std::ostream * > & get_streams () noexcept
 Get a reference to a vector containing pointers to the output streams to print to.
template<typename... T>
void print (const T &... items)
 Print any number of items into each output stream. Ensures that no other threads print to the streams simultaneously, as long as they all exclusively use the same BS::synced_stream object to print.
template<typename... T>
void println (T &&... items)
 Print any number of items into each output stream, followed by a newline character. Ensures that no other threads print to the streams simultaneously, as long as they all exclusively use the same BS::synced_stream object to print.
void remove_stream (std::ostream &stream)
 Remove a stream from the list of output streams to print to.

Static Public Attributes

static std::ostream &(& endl )(std::ostream &) = static_cast<std::ostream& (&)(std::ostream&)>(std::endl)
 A stream manipulator to pass to a BS::synced_stream (an explicit cast of std::endl). Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead.
static std::ostream &(& flush )(std::ostream &) = static_cast<std::ostream& (&)(std::ostream&)>(std::flush)
 A stream manipulator to pass to a BS::synced_stream (an explicit cast of std::flush). Used to flush the stream.

Private Attributes

std::mutex stream_mutex
 A mutex to synchronize printing.
std::vector< std::ostream * > out_streams
 The output streams to print to.

Detailed Description

A utility class to synchronize printing to one or more output streams by different threads.

Constructor & Destructor Documentation

◆ synced_stream() [1/2]

BS::synced_stream::synced_stream ( )
inlineexplicit

Construct a new synced stream which prints to std::cout.

2416 {
2417 add_stream(std::cout);
2418 }
void add_stream(std::ostream &stream)
Add a stream to the list of output streams to print to.
Definition BS_thread_pool.hpp:2437
Here is the call graph for this function:

◆ synced_stream() [2/2]

template<typename... T>
BS::synced_stream::synced_stream ( T &... streams)
inlineexplicit

Construct a new synced stream which prints to the given output stream(s).

Template Parameters
TThe types of the output streams to print to.
Parameters
streamsThe output streams to print to.
2428 {
2429 (add_stream(streams), ...);
2430 }
Here is the call graph for this function:

Member Function Documentation

◆ add_stream()

void BS::synced_stream::add_stream ( std::ostream & stream)
inline

Add a stream to the list of output streams to print to.

Parameters
streamThe stream.
2438 {
2439 out_streams.push_back(&stream);
2440 }
std::vector< std::ostream * > out_streams
The output streams to print to.
Definition BS_thread_pool.hpp:2507
Here is the caller graph for this function:

◆ get_streams()

std::vector< std::ostream * > & BS::synced_stream::get_streams ( )
inlinenoexcept

Get a reference to a vector containing pointers to the output streams to print to.

Returns
The output streams.
2448 {
2449 return out_streams;
2450 }

◆ print()

template<typename... T>
void BS::synced_stream::print ( const T &... items)
inline

Print any number of items into each output stream. Ensures that no other threads print to the streams simultaneously, as long as they all exclusively use the same BS::synced_stream object to print.

Template Parameters
TThe types of the items.
Parameters
itemsThe items to print.
2460 {
2461 const std::scoped_lock stream_lock(stream_mutex);
2462 for (std::ostream* const stream : out_streams)
2463 (*stream << ... << items);
2464 }
std::mutex stream_mutex
A mutex to synchronize printing.
Definition BS_thread_pool.hpp:2502
Here is the caller graph for this function:

◆ println()

template<typename... T>
void BS::synced_stream::println ( T &&... items)
inline

Print any number of items into each output stream, followed by a newline character. Ensures that no other threads print to the streams simultaneously, as long as they all exclusively use the same BS::synced_stream object to print.

Template Parameters
TThe types of the items.
Parameters
itemsThe items to print.
2474 {
2475 print(std::forward<T>(items)..., '\n');
2476 }
void print(const T &... items)
Print any number of items into each output stream. Ensures that no other threads print to the streams...
Definition BS_thread_pool.hpp:2459
Here is the call graph for this function:

◆ remove_stream()

void BS::synced_stream::remove_stream ( std::ostream & stream)
inline

Remove a stream from the list of output streams to print to.

Parameters
streamThe stream.
2484 {
2485 out_streams.erase(std::remove(out_streams.begin(), out_streams.end(), &stream), out_streams.end());
2486 }
Here is the caller graph for this function:

Member Data Documentation

◆ endl

std::ostream &(& BS::synced_stream::endl) (std::ostream &) = static_cast<std::ostream& (&)(std::ostream&)>(std::endl)
inlinestatic

A stream manipulator to pass to a BS::synced_stream (an explicit cast of std::endl). Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead.

◆ flush

std::ostream &(& BS::synced_stream::flush) (std::ostream &) = static_cast<std::ostream& (&)(std::ostream&)>(std::flush)
inlinestatic

A stream manipulator to pass to a BS::synced_stream (an explicit cast of std::flush). Used to flush the stream.

◆ out_streams

std::vector<std::ostream*> BS::synced_stream::out_streams
private

The output streams to print to.

◆ stream_mutex

std::mutex BS::synced_stream::stream_mutex
mutableprivate

A mutex to synchronize printing.


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