ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF > Class Template Reference

Description:

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
requires (::std::is_same_v<TChar, char> || ::std::is_same_v<TChar, wchar_t>)
class alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >

This class writes ALib Strings into instances of std::basic_ostream<TChar>. The features of this type are:

  • Supports writing into narrow and wide output streams (std::ostream and std::wostream). The character width is controlled with the template parameter TChar.
  • Accepts ALib strings of all three character widths (narrow, wide, and strange strings).
  • The template parameter TSynced allows syncing the output in multithreaded applications to avoid interleaving of output. For this, the C++ 20 mechanism provided with type std::osyncstream is used. This allows syncing across C++ libraries that rely on the same standard.
    If syncing is enabled, output is emitted to the stream only at the moment that the corresponding instance of this class is destructed.
  • Optionally, this type converts non-conform newline sequences to the desired coding. For example, if the given strings contain WindowsOS newline codes "\r\n", on GNU/Linux platforms, just "\n" may be written. This feature is controlled by the template parameter TTargetLF.
  • The type counts the number of printed characters with its overloaded method Write. "Printed" here means that it is how many characters in a UTF8-enconding would effectively be shown, if printed.
  • The type uses ALib allocators for memory management.

It is recommended that only short-living, local instances of this class are created. This is especially important in the case that parameter TSynced is true. In this mode, this type follows the RAII idiom just as its then available member std::osyncstream does.

Note
As of 10/2025, some toolchains might not support the C++20 output stream synchronization, yet. In this case, the code compiles without using it, and the output is not synchronized and may interleave.
Clang's standard library libc++ (which can be activated with ALib with CMake variable ALIB_CLANG_USE_LIBCPP), in its version 200100 supports class std::osyncstream only if the compiler flag -fexperimental-library is provided.
Template Parameters
TCharThe character type of the target std::basic_ostream. Defaults to nchar (which effectively is C++ char).
TAllocatorThe allocator type to use with std::osyncstream (if TSynced is true), and with temporary character conversion buffers in case those need to exceed 4kB.
Defaults to lang::HeapAllocator.
TSyncedDetermines if the encapsulated std::ostream should be synchronized on writing. If so, the C++ 20 mechanism provided with std::osyncstream is used, as well as ALib's built-in mechanism provided with the global mutex STD_IOSTREAMS_LOCK. Both mechanics togoether allows syncing across C++ libraries and in respect to ouput generated with ALib, synchronization between std::cout and std::cerr.
TTargetLFDetermines whether non-conform line-feeds are adjusted. The default value is Platform, which converts all line-feeds to the platforms' standard. Values Unix and WindowsOS can be used to force conversion to a different standard. Finally, Ignore disables the detection and conversion of line-feed codes.

Definition at line 235 of file stdiostream.mpp.

Inheritance diagram for alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >:
[legend]
Collaboration diagram for alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >:
[legend]

Public Type Index:

using AllocatorType = TAllocator
 Exposes template parameter TAllocator.
 
using CharType = TChar
 Exposes template parameter TChar.
 
- Public Type Index: inherited from alib::lang::AllocatorMember< lang::HeapAllocator >
using AllocatorType
 Exposes the allocator type.
 

Public Static Field Index:

static constexpr bool Synced = TSynced
 Exposes template parameter TSynced.
 
static constexpr lang::LineFeeds TargetLF = TTargetLF
 Exposes template parameter TAdjustLF.
 

Public Field Index:

bool DisableSync =false
 

Public Method Index:

 OStreamWriter (::std::basic_ostream< TChar > &os)
 
 OStreamWriter (::std::basic_ostream< TChar > &os, TAllocator &alloc)
 
void Fill (const TChar fillChar, integer count)
 
::std::ostream & GetStream ()
 
void Write (const NString &src, integer *printedWidth=nullptr)
 
void Write (const WString &src, integer *printedWidth=nullptr)
 
void Write (const XString &src, integer *printedWidth=nullptr)
 
- Public Method Index: inherited from alib::lang::AllocatorMember< lang::HeapAllocator >
 AllocatorMember ()
 
 AllocatorMember (lang::HeapAllocator &pAllocator) noexcept
 
AllocatorInterface< lang::HeapAllocatorAI () const noexcept
 
lang::HeapAllocatorGetAllocator () const noexcept
 

Protected Type Index:

using allocBase = lang::AllocatorMember<TAllocator>
 The type of the base class that stores the allocator.
 

Protected Field Index:

::std::conditional_t< TSynced &&!ALIB_SINGLE_THREADED, ::std::basic_osyncstream< TChar, ::std::char_traits< TChar >, lang::StdAllocator< TChar, TAllocator > >, ::std::basic_ostream< TChar > & > ostream
 
- Protected Field Index: inherited from alib::lang::AllocatorMember< lang::HeapAllocator >
lang::HeapAllocatorallocator
 The allocator stored.
 

Protected Method Index:

template<typename TSrc>
void doWrite (const TString< TSrc > &src, integer *printedWidth)
 

Type Definition Details:

◆ AllocatorType

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
using alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::AllocatorType = TAllocator

Exposes template parameter TAllocator.

Definition at line 243 of file stdiostream.mpp.

◆ allocBase

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
using alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::allocBase = lang::AllocatorMember<TAllocator>
protected

The type of the base class that stores the allocator.

Definition at line 238 of file stdiostream.mpp.

◆ CharType

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
using alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::CharType = TChar

Exposes template parameter TChar.

Definition at line 242 of file stdiostream.mpp.

Field Details:

◆ DisableSync

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
bool alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::DisableSync =false

This flag may be set after construction to disable the syncing mechanics, even if the template parameter TSynced is set. This is useful, for example, if a using code detects recursion, which is not allowed with std::osyncstream and STD_IOSTREAMS_LOCK.

Definition at line 251 of file stdiostream.mpp.

◆ ostream

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
::std::conditional_t<TSynced && !ALIB_SINGLE_THREADED, ::std::basic_osyncstream< TChar, ::std::char_traits<TChar>, lang::StdAllocator<TChar,TAllocator> >, ::std::basic_ostream<TChar>& > alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::ostream
protected

If unsynced, this member holds the output stream as provided with construction. In the synced case, this stores a std::basic_osyncstream which wraps the given output stream.

Definition at line 262 of file stdiostream.mpp.

◆ Synced

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
bool alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::Synced = TSynced
staticconstexpr

Exposes template parameter TSynced.

Definition at line 241 of file stdiostream.mpp.

◆ TargetLF

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
lang::LineFeeds alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::TargetLF = TTargetLF
staticconstexpr

Exposes template parameter TAdjustLF.

Definition at line 245 of file stdiostream.mpp.

Constructor(s) / Destructor Details:

◆ OStreamWriter() [1/2]

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::OStreamWriter ( ::std::basic_ostream< TChar > & os)
inline

Constructor accepting the destination stream.

Parameters
osThe output-stream to use.

Definition at line 340 of file stdiostream.mpp.

◆ OStreamWriter() [2/2]

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::OStreamWriter ( ::std::basic_ostream< TChar > & os,
TAllocator & alloc )
inline

Constructor accepting the destination stream, as well as an allocator to use.

Parameters
osThe output-stream to use.
allocThe allocator to use for the internal buffers.

Definition at line 347 of file stdiostream.mpp.

Method Details:

◆ doWrite()

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
template<typename TSrc>
void alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::doWrite ( const TString< TSrc > & src,
integer * printedWidth )
inlineprotected

Implementation of overloaded methods Write.

Parameters
srcThe string to write.
[out]printedWidthIf given, the output width of src when it was printed is returned. (See the class description for further information.)
Template Parameters
TSrcThe character type of the source string.

Definition at line 273 of file stdiostream.mpp.

◆ Fill()

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
void alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::Fill ( const TChar fillChar,
integer count )
inline

Write the given character repeatedly to the stream.

Parameters
fillCharThe character to write.
countThe number of characters to write.

Definition at line 405 of file stdiostream.mpp.

◆ GetStream()

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
::std::ostream & alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::GetStream ( )
inline

Returns the output stream given with construction, or, if template parameter TSynced is true, that output stream wrapped in a std::basic_osyncstream. In the latter case, the sync-stream is destructed - and data is written - only with the destruction of this instance.

Returns
The output stream of this writer.

Definition at line 367 of file stdiostream.mpp.

◆ Write() [1/3]

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
void alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::Write ( const NString & src,
integer * printedWidth = nullptr )
inline

Writes the given narrow string to the stream.

Note
The output parameter printedWidth returns the length of the given string, when it was converted to wide string. This is done, even if template parameter TChar is narrow and consequently no conversion was performed.
The value is useful to callers when the "real" output width is needed, i.e., when the output text is formatted in tabulator columns or tables. Of course, in some locales, this might still not be the correct output width because even uni-code characters are not guaranteed to represent exactly one printable character. But still, this value is already a much better approximation than the length of the given narrow string.
Parameters
srcThe string to write.
[out]printedWidthIf given, the width if src was printed is returned. See the note above. If not given, the method might execute slightly faster.

Definition at line 386 of file stdiostream.mpp.

◆ Write() [2/3]

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
void alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::Write ( const WString & src,
integer * printedWidth = nullptr )
inline

See sibling method Write( const NString& src, integer* printedWidth ).

Parameters
srcThe wide string to write.
[out]printedWidthIf given, the width if src was printed is returned. See the note above. If not given, the method might execute slightly faster.

Definition at line 393 of file stdiostream.mpp.

◆ Write() [3/3]

template<typename TChar = nchar, typename TAllocator = lang::HeapAllocator, bool TSynced = false, lang::LineFeeds TTargetLF = lang::LineFeeds::Platform>
void alib::strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF >::Write ( const XString & src,
integer * printedWidth = nullptr )
inline

See sibling method Write( const NString& src, integer* printedWidth ).

Parameters
srcThe strange string to write.
[out]printedWidthIf given, the width if src was printed is returned. See the note above. If not given, the method might execute slightly faster.

Definition at line 400 of file stdiostream.mpp.


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