Ookii.CommandLine for C++  2.0.0
Public Types | Public Member Functions | Static Public Member Functions | List of all members
ookii::basic_line_wrapping_ostream< CharType, Traits > Class Template Reference

Output stream that wraps lines on white-space characters at the specified line length, and with support for indentation. More...

#include <ookii/line_wrapping_stream.h>

Inheritance diagram for ookii::basic_line_wrapping_ostream< CharType, Traits >:
Inheritance graph
[legend]
Collaboration diagram for ookii::basic_line_wrapping_ostream< CharType, Traits >:
Collaboration graph
[legend]

Public Types

using base_type = std::basic_ostream< CharType, Traits >
 The concrete type that this class derives from.
 
using streambuf_type = std::basic_streambuf< CharType, Traits >
 The concrete base stream buffer type used by this stream.
 

Public Member Functions

 basic_line_wrapping_ostream ()
 Initializes a new instance of the basic_line_wrapping_ostream class. More...
 
 basic_line_wrapping_ostream (base_type &base_stream, size_t max_line_length, bool count_formatting=false)
 Initializes a new instance of the basic_line_wrapping_ostream class with the specified underlying stream and maximum line length. More...
 
 basic_line_wrapping_ostream (basic_line_wrapping_ostream &&other) noexcept
 Move constructor. More...
 
void flush (bool flush_last_line)
 Flushes the buffer to the underlying stream buffer, optionally including the the final, partial line. More...
 
void init (base_type &base_stream, size_t max_line_length, bool count_formatting=false)
 Initializes a new instance of the basic_line_wrapping_ostream class with the specified underlying stream and maximum line length. More...
 
basic_line_wrapping_ostreamoperator= (basic_line_wrapping_ostream &&other) noexcept
 Move assignment operator. More...
 
void swap (basic_line_wrapping_ostream &other) noexcept
 Swaps this basic_line_wrapping_ostream instance with another. More...
 

Static Public Member Functions

static basic_line_wrapping_ostream for_cerr (short default_width=80)
 Creates a basic_line_wrapping_ostream that writes to the standard error stream, using the console width as the line width. More...
 
static basic_line_wrapping_ostream for_cout (short default_width=80)
 Creates a basic_line_wrapping_ostream that writes to the standard output stream, using the console width as the line width. More...
 

Detailed Description

template<typename CharType, typename Traits = std::char_traits<CharType>>
class ookii::basic_line_wrapping_ostream< CharType, Traits >

Output stream that wraps lines on white-space characters at the specified line length, and with support for indentation.

This stream writes its output to the stream buffer of another stream, which could be any stream (like a file stream, or string stream).

Warning
This class assumes that the target stream is at the start of a line when writing starts, and that no other users are writing to the same stream. Otherwise, output will not be correctly wrapped or indented.
Flushing this stream will not flush the contents of the last unfinished line. Use ookii::flush() with the flush_last_line parameter set to true to make sure all content is flushed (this may insert an additional new line character).

Several typedefs for common character types are provided:

Type Definition
ookii::line_wrapping_ostream ookii::basic_line_wrapping_ostream<char>
ookii::wline_wrapping_ostream ookii::basic_line_wrapping_ostream<wchar_t>
Template Parameters
CharTypeThe type of characters used by the target stream.
TraitsThe character traits used by the target stream.

Constructor & Destructor Documentation

◆ basic_line_wrapping_ostream() [1/3]

template<typename CharType , typename Traits = std::char_traits<CharType>>
ookii::basic_line_wrapping_ostream< CharType, Traits >::basic_line_wrapping_ostream ( )
inline

Initializes a new instance of the basic_line_wrapping_ostream class.

You must call the init() method before the stream is usable.

◆ basic_line_wrapping_ostream() [2/3]

template<typename CharType , typename Traits = std::char_traits<CharType>>
ookii::basic_line_wrapping_ostream< CharType, Traits >::basic_line_wrapping_ostream ( base_type base_stream,
size_t  max_line_length,
bool  count_formatting = false 
)
inline

Initializes a new instance of the basic_line_wrapping_ostream class with the specified underlying stream and maximum line length.

Parameters
base_streamThe stream whose stream buffer to write output to.
max_line_lengthThe maximum line length, or a value of 0 or larger than 65536 to specify no limit. Use the use_console_width constant to use the console width as the maximum.
count_formattingInclude virtual terminal sequences when calculating the length of a line.

◆ basic_line_wrapping_ostream() [3/3]

template<typename CharType , typename Traits = std::char_traits<CharType>>
ookii::basic_line_wrapping_ostream< CharType, Traits >::basic_line_wrapping_ostream ( basic_line_wrapping_ostream< CharType, Traits > &&  other)
inlinenoexcept

Move constructor.

Parameters
otherThe basic_line_wrapping_ostream to move from.

Member Function Documentation

◆ flush()

template<typename CharType , typename Traits = std::char_traits<CharType>>
void ookii::basic_line_wrapping_ostream< CharType, Traits >::flush ( bool  flush_last_line)
inline

Flushes the buffer to the underlying stream buffer, optionally including the the final, partial line.

If the buffer contains a line that's shorter than the maximum and was not terminated by a line break, the normal basic_ostream::flush() function will not flush that final line, because doing so would make it impossible to find the correct place to wrap the line later if that place was already flushed.

This function offers the ability to flush the buffer including the last line. If there is a non-empty, non-terminated line in the buffer, that line will be flushed, followed by a line break.

Parameters
flush_last_linetrue to flush the final non-terminated line; false to leave that line in the buffer.
Returns
The result of calling pubsync() on the underlying stream buffer.

◆ for_cerr()

template<typename CharType , typename Traits = std::char_traits<CharType>>
static basic_line_wrapping_ostream ookii::basic_line_wrapping_ostream< CharType, Traits >::for_cerr ( short  default_width = 80)
inlinestatic

Creates a basic_line_wrapping_ostream that writes to the standard error stream, using the console width as the line width.

Parameters
default_widthThe maximum line length to use if the console width cannot be determined.

◆ for_cout()

template<typename CharType , typename Traits = std::char_traits<CharType>>
static basic_line_wrapping_ostream ookii::basic_line_wrapping_ostream< CharType, Traits >::for_cout ( short  default_width = 80)
inlinestatic

Creates a basic_line_wrapping_ostream that writes to the standard output stream, using the console width as the line width.

Parameters
default_widthThe maximum line length to use if the console width cannot be determined.

◆ init()

template<typename CharType , typename Traits = std::char_traits<CharType>>
void ookii::basic_line_wrapping_ostream< CharType, Traits >::init ( base_type base_stream,
size_t  max_line_length,
bool  count_formatting = false 
)
inline

Initializes a new instance of the basic_line_wrapping_ostream class with the specified underlying stream and maximum line length.

Parameters
base_streamThe stream whose stream buffer to write output to.
max_line_lengthThe maximum line length, or a value of 0 or larger than 65536 to specify no limit. Use the use_console_width constant to use the console width as the maximum.
count_formattingInclude virtual terminal sequences when calculating the length of a line.

◆ operator=()

template<typename CharType , typename Traits = std::char_traits<CharType>>
basic_line_wrapping_ostream& ookii::basic_line_wrapping_ostream< CharType, Traits >::operator= ( basic_line_wrapping_ostream< CharType, Traits > &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe basic_line_wrapping_ostream to move from.

◆ swap()

template<typename CharType , typename Traits = std::char_traits<CharType>>
void ookii::basic_line_wrapping_ostream< CharType, Traits >::swap ( basic_line_wrapping_ostream< CharType, Traits > &  other)
inlinenoexcept

Swaps this basic_line_wrapping_ostream instance with another.

Parameters
otherThe instance to swap with.

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