BasicTextOStream< CharT, ByteT > Class Template Reference

#include <Pt/TextStream.h>

Converts character sequences using a codec. More...

Inherits BasicOStream< CharT, TraitsT >.

Public Types

typedef ByteT extern_type
 External character type.
typedef CharT intern_type
 Internal character type.
typedef CharT char_type
 Internal character type.
typedef std::char_traits< CharT > traits_type
 Internal character traits.
typedef traits_type::int_type int_type
 Integer type.
typedef traits_type::pos_type pos_type
 Stream position type.
typedef traits_type::off_type off_type
 Stream offset type.
typedef std::basic_ostream< extern_typeStreamType
 External stream type.
typedef TextCodec< char_type, extern_typeCodecType
 Codec type.

Public Member Functions

 BasicTextOStream (StreamType &os, CodecType *codec)
 Construct with output stream and codec.
 BasicTextOStream (CodecType *codec)
 Construct with codec.
 ~BasicTextOStream ()
 Destructor.
CodecTypecodec ()
 Returns the used code or a nullptr.
void setCodec (CodecType *codec)
 Sets the text codec.
void attach (StreamType &os)
 Attach to external target.
void detach ()
 Detach from external target.
void discard ()
 Discards the buffer.
void reset ()
 Resets the buffer and target.
void reset (StreamType &os)
 Resets the buffer and target.
BasicTextBuffer< intern_type, extern_type > & textBuffer ()
 Returns the stream buffer.
std::streamsize writesome (CharT *buffer, std::streamsize n)
 Write as much data as fits in buffer.
BasicStreamBuffer< CharT > * buffer ()
 Returns the buffer.
void setBuffer (BasicStreamBuffer< CharT > *sb)
 Sets the buffer.

Detailed Description

template<typename CharT, typename ByteT>
class Pt::BasicTextOStream< CharT, ByteT >

This stream encodes an external character sequence using a codec. Writing to the stream will convert the written characters to the external character types in the external encoding.

The following example shows how to encode text to an UTF-8 byte sequence:

std::ostringstream oss;
Pt::String s = L"Hello World!";
Pt::TextOStream tos(oss, new Pt::Utf8Codec());
tos << s;
tos.flush();
Unicode capable basic_string.
Definition Api-String.h:67
Convert between unicode and UTF-8.
Definition Utf8Codec.h:44

The string stream serves as the output of the text stream, which uses a Pt::Utf8Codec to encode text to UTF-8. The insertion operator can be used for strings or to format numbers. When all data has been written to the text stream, flush() needs to be called to finish off the output byte sequence. This is especially important for encodings with shift states.

Constructor & Destructor Documentation

◆ BasicTextOStream() [1/2]

template<typename CharT, typename ByteT>
BasicTextOStream ( StreamType & os,
CodecType * codec )

The character sequence converted by the codec is written to the output stream os. The codec object which is passed as a pointer will be managed by this class and deleted if its reference count reaches 0.

◆ BasicTextOStream() [2/2]

template<typename CharT, typename ByteT>
BasicTextOStream ( CodecType * codec)
explicit

The codec object which is passed as pointer will be managed by this class and deleted if its reference count reaches 0.

Member Function Documentation

◆ setCodec()

template<typename CharT, typename ByteT>
void setCodec ( CodecType * codec)

The codec object which is passed as pointer will be managed by this class and deleted if its reference count reaches 0.

◆ reset() [1/2]

template<typename CharT, typename ByteT>
void reset ( )

The target is detached and the buffer content is discarded. The codec is kept, if one was set previously.

◆ reset() [2/2]

template<typename CharT, typename ByteT>
void reset ( StreamType & os)

Attaches to the new target and discards the buffer. The codec is kept, if one was set previously.