VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::TerminalStream Class Referencefinal

Singleton buffered stdout writer with thread-safety and TTY detection. More...

#include <terminal_stream.h>

Collaboration diagram for vlink::TerminalStream:

Public Types

using ManipType = TerminalStream& (*)(TerminalStream&)
 Type alias for stream manipulator functions.

Public Member Functions

 TerminalStream () noexcept
 Constructs the stream with a kDefaultBufferSize internal buffer.
 ~TerminalStream () noexcept
 Destructor – flushes any remaining buffered data to stdout.
 TerminalStream (const TerminalStream &) noexcept=delete
TerminalStreamoperator= (const TerminalStream &) noexcept=delete
 TerminalStream (TerminalStream &&) noexcept=delete
TerminalStreamoperator= (TerminalStream &&) noexcept=delete
void init () noexcept
 Initialises the stream: detects the file descriptor and TTY status.
bool is_tty () const noexcept
 Returns true if stdout is connected to a terminal (TTY).
bool is_initialized () const noexcept
 Returns true if init() has been called.
void flush ()
 Flushes all buffered data to stdout immediately.
TerminalStreamwrite_raw (const char *data, size_t len) noexcept
 Writes a raw byte array of len bytes to the buffer.
TerminalStreamoperator<< (char c) noexcept
 Writes a single character to the buffer.
TerminalStreamoperator<< (const char *str) noexcept
 Writes a NUL-terminated C string to the buffer.
TerminalStreamoperator<< (const std::string &str) noexcept
 Writes a std::string to the buffer.
TerminalStreamoperator<< (std::string_view str) noexcept
 Writes a std::string_view to the buffer.
TerminalStreamoperator<< (bool value) noexcept
 Writes "true" or "false".
TerminalStreamoperator<< (short value) noexcept
 Writes a short integer.
TerminalStreamoperator<< (unsigned short value) noexcept
 Writes an unsigned short integer.
TerminalStreamoperator<< (int value) noexcept
 Writes an int.
TerminalStreamoperator<< (unsigned int value) noexcept
 Writes an unsigned int.
TerminalStreamoperator<< (long value) noexcept
 Writes a long.
TerminalStreamoperator<< (unsigned long value) noexcept
 Writes an unsigned long.
TerminalStreamoperator<< (long long value) noexcept
 Writes a long long.
TerminalStreamoperator<< (unsigned long long value) noexcept
 Writes an unsigned long long.
TerminalStreamoperator<< (float value) noexcept
 Writes a float using "%g" format.
TerminalStreamoperator<< (double value) noexcept
 Writes a double using "%g" format.
TerminalStreamoperator<< (long double value) noexcept
 Writes a long double using "%Lg" format.
TerminalStreamoperator<< (const void *ptr) noexcept
 Writes a pointer address as a hex string.
TerminalStreamoperator<< (ManipType manip) noexcept
 Applies a ManipType manipulator function to the stream.
TerminalStreamoperator<< (std::ostream &(*)(std::ostream &)) noexcept
 Accepts std::endl and similar std::ostream manipulators.

Static Public Member Functions

static TerminalStreamget () noexcept
 Returns the process-global TerminalStream singleton.
static TerminalStreamendl (TerminalStream &stream) noexcept
 Stream manipulator that appends a newline and flushes the buffer.
static TerminalStreamflush_manip (TerminalStream &stream) noexcept
 Stream manipulator that flushes the buffer without appending a newline.

Static Public Attributes

static constexpr size_t kDefaultBufferSize {1024 * 1024 * 1}
 Default internal buffer size (1 MiB).

Detailed Description

Singleton buffered stdout writer with thread-safety and TTY detection.

Obtain the singleton with TerminalStream::get(). Copy and move are disabled.

Member Typedef Documentation

◆ ManipType

Type alias for stream manipulator functions.

Constructor & Destructor Documentation

◆ TerminalStream() [1/3]

vlink::TerminalStream::TerminalStream ( )
inlinenoexcept

Constructs the stream with a kDefaultBufferSize internal buffer.

Here is the caller graph for this function:

◆ ~TerminalStream()

vlink::TerminalStream::~TerminalStream ( )
inlinenoexcept

Destructor – flushes any remaining buffered data to stdout.

◆ TerminalStream() [2/3]

vlink::TerminalStream::TerminalStream ( const TerminalStream & )
deletenoexcept
Here is the call graph for this function:

◆ TerminalStream() [3/3]

vlink::TerminalStream::TerminalStream ( TerminalStream && )
deletenoexcept
Here is the call graph for this function:

Member Function Documentation

◆ endl()

TerminalStream & vlink::TerminalStream::endl ( TerminalStream & stream)
inlinestaticnoexcept

Stream manipulator that appends a newline and flushes the buffer.

Usage: stream << TerminalStream::endl;

Parameters
streamThe TerminalStream to write to.
Returns
Reference to stream.
Here is the call graph for this function:

◆ flush()

void vlink::TerminalStream::flush ( )
inline

Flushes all buffered data to stdout immediately.

On non-Windows TTY outputs, also calls tcdrain() to wait for the terminal.

Note
This method acquires the internal mutex; safe to call concurrently.
Here is the caller graph for this function:

◆ flush_manip()

TerminalStream & vlink::TerminalStream::flush_manip ( TerminalStream & stream)
inlinestaticnoexcept

Stream manipulator that flushes the buffer without appending a newline.

Usage: stream << TerminalStream::flush_manip;

Parameters
streamThe TerminalStream to flush.
Returns
Reference to stream.
Here is the call graph for this function:

◆ get()

TerminalStream & vlink::TerminalStream::get ( )
inlinestaticnoexcept

Returns the process-global TerminalStream singleton.

Implementation Details.

Returns
Reference to the singleton instance.
Here is the call graph for this function:

◆ init()

void vlink::TerminalStream::init ( )
inlinenoexcept

Initialises the stream: detects the file descriptor and TTY status.

Must be called once before using is_tty(). On Windows, enables virtual terminal processing (ANSI escape codes). Subsequent calls are no-ops.

Here is the caller graph for this function:

◆ is_initialized()

bool vlink::TerminalStream::is_initialized ( ) const
inlinenodiscardnoexcept

Returns true if init() has been called.

Returns
true after the first successful init() call.
Here is the caller graph for this function:

◆ is_tty()

bool vlink::TerminalStream::is_tty ( ) const
inlinenodiscardnoexcept

Returns true if stdout is connected to a terminal (TTY).

Only meaningful after init() has been called.

Returns
true if stdout is a TTY.
Here is the caller graph for this function:

◆ operator<<() [1/19]

TerminalStream & vlink::TerminalStream::operator<< ( bool value)
inlinenoexcept

Writes "true" or "false".

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [2/19]

TerminalStream & vlink::TerminalStream::operator<< ( char c)
inlinenoexcept

Writes a single character to the buffer.

Parameters
cCharacter to write.
Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [3/19]

TerminalStream & vlink::TerminalStream::operator<< ( const char * str)
inlinenoexcept

Writes a NUL-terminated C string to the buffer.

Null-pointer check is performed; passing nullptr is a no-op.

Parameters
strC string to write.
Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [4/19]

TerminalStream & vlink::TerminalStream::operator<< ( const std::string & str)
inlinenoexcept

Writes a std::string to the buffer.

Parameters
strString to write.
Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [5/19]

TerminalStream & vlink::TerminalStream::operator<< ( const void * ptr)
inlinenoexcept

Writes a pointer address as a hex string.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [6/19]

TerminalStream & vlink::TerminalStream::operator<< ( double value)
inlinenoexcept

Writes a double using "%g" format.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [7/19]

TerminalStream & vlink::TerminalStream::operator<< ( float value)
inlinenoexcept

Writes a float using "%g" format.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [8/19]

TerminalStream & vlink::TerminalStream::operator<< ( int value)
inlinenoexcept

Writes an int.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [9/19]

TerminalStream & vlink::TerminalStream::operator<< ( long double value)
inlinenoexcept

Writes a long double using "%Lg" format.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [10/19]

TerminalStream & vlink::TerminalStream::operator<< ( long long value)
inlinenoexcept

Writes a long long.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [11/19]

TerminalStream & vlink::TerminalStream::operator<< ( long value)
inlinenoexcept

Writes a long.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [12/19]

TerminalStream & vlink::TerminalStream::operator<< ( ManipType manip)
inlinenoexcept

Applies a ManipType manipulator function to the stream.

Compatible with TerminalStream::endl and TerminalStream::flush_manip.

Parameters
manipManipulator function pointer.
Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [13/19]

TerminalStream & vlink::TerminalStream::operator<< ( short value)
inlinenoexcept

Writes a short integer.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [14/19]

TerminalStream & vlink::TerminalStream::operator<< ( std::ostream & *)(std::ostream &)
inlinenoexcept

Accepts std::endl and similar std::ostream manipulators.

When std::endl is passed, writes a newline and flushes the buffer.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [15/19]

TerminalStream & vlink::TerminalStream::operator<< ( std::string_view str)
inlinenoexcept

Writes a std::string_view to the buffer.

Parameters
strString view to write.
Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [16/19]

TerminalStream & vlink::TerminalStream::operator<< ( unsigned int value)
inlinenoexcept

Writes an unsigned int.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [17/19]

TerminalStream & vlink::TerminalStream::operator<< ( unsigned long long value)
inlinenoexcept

Writes an unsigned long long.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [18/19]

TerminalStream & vlink::TerminalStream::operator<< ( unsigned long value)
inlinenoexcept

Writes an unsigned long.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator<<() [19/19]

TerminalStream & vlink::TerminalStream::operator<< ( unsigned short value)
inlinenoexcept

Writes an unsigned short integer.

Returns
Reference to *this.
Here is the call graph for this function:

◆ operator=() [1/2]

TerminalStream & vlink::TerminalStream::operator= ( const TerminalStream & )
deletenoexcept
Here is the call graph for this function:

◆ operator=() [2/2]

TerminalStream & vlink::TerminalStream::operator= ( TerminalStream && )
deletenoexcept
Here is the call graph for this function:

◆ write_raw()

TerminalStream & vlink::TerminalStream::write_raw ( const char * data,
size_t len )
inlinenoexcept

Writes a raw byte array of len bytes to the buffer.

Parameters
dataPointer to data to write.
lenNumber of bytes to write.
Returns
Reference to *this for chaining.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ kDefaultBufferSize

size_t vlink::TerminalStream::kDefaultBufferSize {1024 * 1024 * 1}
staticconstexpr

Default internal buffer size (1 MiB).


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