VLink  2.0.0
A high-performance communication middleware
vlink::FastStream Class Reference

Logger-friendly std::ostream with a custom growable string buffer. More...

#include <fast_stream.h>

Inheritance diagram for vlink::FastStream:
Collaboration diagram for vlink::FastStream:

Public Member Functions

 FastStream () noexcept
 Constructs the stream with the default initial backing capacity. More...
 
 ~FastStream () noexcept override
 Destructor; releases the underlying string buffer. More...
 
void reset () noexcept
 Empties the buffer and clears the stream's error state. More...
 
void append_to (std::string &target) const noexcept
 Appends the current buffer contents to target without resetting the stream. More...
 
std::string_view take_view ()
 Returns a non-owning view of the current buffer contents. More...
 
size_t size () const noexcept
 Returns the number of bytes currently held in the buffer. More...
 
size_t capacity () const noexcept
 Returns the current allocated capacity of the backing buffer. More...
 
void shrink_to_fit () noexcept
 Trims the underlying buffer to its current backing size. More...
 
FastStreamwrite_raw (const char *data, size_t len)
 Writes raw bytes into the buffer without going through std::ostream formatting. More...
 

Detailed Description

Logger-friendly std::ostream with a custom growable string buffer.

Routes every write through an embedded StringBuf that owns a std::string. The buffer grows by doubling up to 8 KiB and by linear 8 KiB increments thereafter. take_view yields a non-owning view into the buffer, so a single log line incurs at most one allocation when the buffer must grow and zero allocations on the steady-state path.

Constructor & Destructor Documentation

◆ FastStream()

vlink::FastStream::FastStream ( )
noexcept

Constructs the stream with the default initial backing capacity.

Initial capacity is kDefaultCapacity (256 bytes); the buffer grows on demand.

◆ ~FastStream()

vlink::FastStream::~FastStream ( )
overridenoexcept

Destructor; releases the underlying string buffer.

Member Function Documentation

◆ append_to()

void vlink::FastStream::append_to ( std::string &  target) const
noexcept

Appends the current buffer contents to target without resetting the stream.

Parameters
targetDestination string; contents are appended in place.

◆ capacity()

size_t vlink::FastStream::capacity ( ) const
noexcept

Returns the current allocated capacity of the backing buffer.

Returns
Capacity in bytes.

◆ reset()

void vlink::FastStream::reset ( )
noexcept

Empties the buffer and clears the stream's error state.

Retains the allocated capacity so subsequent messages avoid reallocation.

◆ shrink_to_fit()

void vlink::FastStream::shrink_to_fit ( )
noexcept

Trims the underlying buffer to its current backing size.

Does not shrink to the formatted message length; the put pointer is reset to the start.

◆ size()

size_t vlink::FastStream::size ( ) const
noexcept

Returns the number of bytes currently held in the buffer.

Returns
Buffer length in bytes.

◆ take_view()

std::string_view vlink::FastStream::take_view ( )

Returns a non-owning view of the current buffer contents.

Primary zero-copy hand-off used by the logger. The view remains valid until the next write to this stream or an explicit reset.

Warning
Do not retain the view beyond the next stream operation; doing so dereferences freed or moved storage.
Returns
View covering the current buffer.

◆ write_raw()

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

Writes raw bytes into the buffer without going through std::ostream formatting.

Faster than std::ostream::write for pre-formatted C strings because it skips the locale and format-flag plumbing.

Parameters
dataSource pointer; non-null when len is non-zero.
lenNumber of bytes to write.
Returns
Reference to *this for chaining.

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