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

Stream buffer 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_streambuf< CharType, Traits >:
Inheritance graph
[legend]
Collaboration diagram for ookii::basic_line_wrapping_streambuf< CharType, Traits >:
Collaboration graph
[legend]

Public Types

using base_type = std::basic_streambuf< CharType, Traits >
 The concrete type that this class derives from.
 
using char_type = typename base_type::char_type
 Character type used by the base type.
 
using int_type = typename base_type::int_type
 Integer type used by the base type.
 
using traits_type = typename base_type::traits_type
 Traits type used by the base type.
 

Public Member Functions

 basic_line_wrapping_streambuf () noexcept=default
 Initializes a new instance of the basic_line_wrapping_streambuf class. More...
 
 basic_line_wrapping_streambuf (base_type *streambuf, size_t max_line_length)
 Initializes a new instance of the basic_line_wrapping_streambuf class with the specified underlying stream buffer and maximum line length. More...
 
 basic_line_wrapping_streambuf (basic_line_wrapping_streambuf &&other) noexcept
 Move constructor.
 
 basic_line_wrapping_streambuf (basic_line_wrapping_streambuf &)=delete
 
virtual ~basic_line_wrapping_streambuf ()
 Destructor for the basic_line_wrapping_streambuf class. More...
 
size_t indent () const
 Gets the current number of spaces that each line is indented with.
 
void indent (size_t indent) noexcept
 Sets the number of spaces that each line is indented with. More...
 
void init (base_type *streambuf, size_t max_line_length, bool count_formatting=false) noexcept
 Initializes this basic_line_wrapping_streambuf instance with the specified underlying stream buffer and maximum line length. More...
 
basic_line_wrapping_streambufoperator= (basic_line_wrapping_streambuf &&other) noexcept
 Move assignment operator.
 
basic_line_wrapping_streambufoperator= (basic_line_wrapping_streambuf &)=delete
 
bool reset_indent ()
 Disables indentation for the next line. More...
 
void swap (basic_line_wrapping_streambuf &other) noexcept
 Swaps the contents basic_line_wrapping_streambuf instance with another. More...
 
virtual int sync (bool flush_last_line)
 Flushes the buffer to the underlying stream buffer, optionally including the the final, partial line. More...
 

Protected Member Functions

virtual void imbue (const std::locale &loc) override
 Change the locale of the stream buffer. More...
 
virtual int_type overflow (int_type ch=traits_type::eof()) override
 Ensure there is space to write at least one character to the buffer. More...
 
virtual int sync () override
 Flushes the buffer to the underlying stream buffer. More...
 

Detailed Description

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

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

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

Warning
This class assumes that the target stream buffer is at the start of a line when writing starts, and that no other users are writing to the same stream buffer. Otherwise, output will not be correctly wrapped or indented.
Syncing this buffer will not flush the contents of the last unfinished line. Use sync() 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_streambuf ookii::basic_line_wrapping_streambuf<char>
ookii::wline_wrapping_streambuf ookii::basic_line_wrapping_streambuf<wchar_t>
Template Parameters
CharTypeThe type of characters used by the target stream buffer.
TraitsThe character traits used by the target stream buffer.

Constructor & Destructor Documentation

◆ basic_line_wrapping_streambuf() [1/2]

template<typename CharType , typename Traits = std::char_traits<CharType>>
ookii::basic_line_wrapping_streambuf< CharType, Traits >::basic_line_wrapping_streambuf ( )
defaultnoexcept

Initializes a new instance of the basic_line_wrapping_streambuf class.

This instance cannot be used until init() is called.

◆ basic_line_wrapping_streambuf() [2/2]

template<typename CharType , typename Traits = std::char_traits<CharType>>
ookii::basic_line_wrapping_streambuf< CharType, Traits >::basic_line_wrapping_streambuf ( base_type streambuf,
size_t  max_line_length 
)
inline

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

Parameters
streambufThe 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.

◆ ~basic_line_wrapping_streambuf()

template<typename CharType , typename Traits = std::char_traits<CharType>>
virtual ookii::basic_line_wrapping_streambuf< CharType, Traits >::~basic_line_wrapping_streambuf ( )
inlinevirtual

Destructor for the basic_line_wrapping_streambuf class.

This destructor will flush all contents, including the last line.

Member Function Documentation

◆ imbue()

template<typename CharType , typename Traits = std::char_traits<CharType>>
virtual void ookii::basic_line_wrapping_streambuf< CharType, Traits >::imbue ( const std::locale &  loc)
inlineoverrideprotectedvirtual

Change the locale of the stream buffer.

Parameters
locThe new locale.

◆ indent()

template<typename CharType , typename Traits = std::char_traits<CharType>>
void ookii::basic_line_wrapping_streambuf< CharType, Traits >::indent ( size_t  indent)
inlinenoexcept

Sets the number of spaces that each line is indented with.

Parameters
indentThe new indentation size.

◆ init()

template<typename CharType , typename Traits = std::char_traits<CharType>>
void ookii::basic_line_wrapping_streambuf< CharType, Traits >::init ( base_type streambuf,
size_t  max_line_length,
bool  count_formatting = false 
)
inlinenoexcept

Initializes this basic_line_wrapping_streambuf instance with the specified underlying stream buffer and maximum line length.

Parameters
streambufThe 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.

◆ overflow()

template<typename CharType , typename Traits = std::char_traits<CharType>>
virtual int_type ookii::basic_line_wrapping_streambuf< CharType, Traits >::overflow ( int_type  ch = traits_type::eof())
inlineoverrideprotectedvirtual

Ensure there is space to write at least one character to the buffer.

Called when there is no more space in the buffer, or when sync() is called.

Attention
This function flushes the buffer to the underlying stream buffer, wrapping lines and adding indent as necessary. The passed character, if not eof, will be added to the buffer afterwards.
Parameters
chThe character to put in the buffer.
Returns
A value not equal to Traits::eof() on success, and Traits::eof() on failure.

◆ reset_indent()

template<typename CharType , typename Traits = std::char_traits<CharType>>
bool ookii::basic_line_wrapping_streambuf< CharType, Traits >::reset_indent ( )
inline

Disables indentation for the next line.

Warning
If the last character written to the buffer was not a new line, one is inserted.
Returns
false if a new line character was needed but could not be written; otherwise, true.

◆ swap()

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

Swaps the contents basic_line_wrapping_streambuf instance with another.

Parameters
otherThe basic_line_wrapping_streambuf to swap with.

◆ sync() [1/2]

template<typename CharType , typename Traits = std::char_traits<CharType>>
virtual int ookii::basic_line_wrapping_streambuf< CharType, Traits >::sync ( )
inlineoverrideprotectedvirtual

Flushes the buffer to the underlying stream buffer.

If the buffer contains a line that's shorter than the maximum and was not terminated by a line break, this 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.

Returns
The result of calling pubsync() on the underlying stream buffer.

◆ sync() [2/2]

template<typename CharType , typename Traits = std::char_traits<CharType>>
virtual int ookii::basic_line_wrapping_streambuf< CharType, Traits >::sync ( bool  flush_last_line)
inlinevirtual

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 pubsync() function will not flush that final line, because doing so would make it impossible to find the correct place to break 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.

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