ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
stdiostream.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_strings of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace strings { namespace compatibility { namespace std {
9
10//==================================================================================================
11/// This template type may be specialized to suppress ambiguities for types \p{T} which
12/// - have <c>std::operator<<(ostream, const T&)</c> defined, \b and
13/// - are #"alib_strings_assembly_ttostring;appendable" to \alib strings.
14///
15/// \note
16/// The ambiguity occurs due to the definition of <c>std::operator<<</c> for all appendable
17/// types.
18///
19/// If a specialization of this template struct exists that inherits <c>std::true_type</c>,
20/// the compiler will not choose the \alib implementation of the operator, which resolves the
21/// ambiguity.
22///
23/// \see
24/// Specialization might be done with macro
25/// #"ALIB_STRINGS_SUPPRESS_STD_OSTREAM_OPERATOR".
26///
27/// @tparam T The appendable type to suppress
28//==================================================================================================
29template<typename T> struct SuppressStdOStreamOpTraits : ::std::false_type {};
30
31//==================================================================================================
32/// Parameter class used to append to objects of type #"^AString", which invokes the method of the
33/// according specialization of template struct
34/// <b>AppendableTraits<TIStreamLine,TChar,HeapAllocator></b>.<br>
35/// This then reads a line of text from the encapsulated \c std::istream and appends that line to
36/// the target #"%AString".
37///
38/// This class can be created 'inline', similar to, for example,
39/// #"alib_strings_assembly_ttostring_builtin;special types appendable to" class #"%AString".
40/// But in the common cases, where a series of lines are to be read from a \c std::istream, a local
41/// object of this type should be created.
42/// In the case of a reading-loop, it is efficient to place it outside such a loop.
43///
44/// Field #"IsEOF" can be used to detect the end of the input stream.
45///
46/// \see
47/// - #"compatibility::std::operator>>(std::istream&, alib::NAString&)" and
48/// #"std::operator<<(std::ostream&, const alib::NString&)".
49/// - For a sample, refer to the source code of \alib class #"%IniFile", method
50/// #"IniFile::Read;*".
51///
52///
53/// @tparam TChar The character type of the input stream as well as the receiving string.<br>
54/// Specializations for character types #"characters::nchar",
55/// #"characters::wchar" exist. Those have corresponding alias type definition
56/// shortcuts #"IStreamLineN" and #"IStreamLineW" in namespace #"alib".
57//==================================================================================================
58template<typename TChar>
60 /// The input stream to read from.
61 ::std::basic_istream<TChar>* IStream;
62
63 /// If \c CurrentData::KEEP, the target #"%AString" is not cleared before the read operation.
65
66 /// The number of characters that the buffer is increased while reading parts of the line.
68
69 /// The maximum length of a single line to be read. Longer lines get truncated.
71
72 /// Indicates if the end of the input stream was detected with the last read operation.
73 /// If so, a next read operation will not change the string (or clear it, if #"TargetData" is
74 /// \c false
75 bool IsEOF = false;
76
77 /// Constructor.
78 ///
79 /// @param istream The input stream to read from.
80 /// @param targetData If \c CurrentData::Keep, the target #"%AString" is not cleared
81 /// before the read operation is performed.
82 /// Defaults to \c CurrentData::Clear.
83 /// @param bufferSize The number of characters that the buffer is increased while reading
84 /// parts of the line. Defaults to 4096 characters.
85 /// @param maxLineWidth The maximum length of a single line to be read. Longer lines
86 /// get truncated. Defaults to 4096 characters.
87 TIStreamLine( ::std::basic_istream<TChar>* istream,
89 integer bufferSize = 4096,
90 integer maxLineWidth = 4096 )
91 : IStream (istream),
92 TargetData (targetData),
93 BufferSize (bufferSize),
94 MaxLineWidth(maxLineWidth) {}
95};
96
97
98}} // namespace alib::strings[::compatibility::std]
99
100#if DOXYGEN
101 namespace APPENDABLES {
102#endif
103
104/// Specialization of the type trait #"AppendableTraits" for type
105/// #"std::TIStreamLine".
106/// @tparam TChar The <b>AString</b>'s #"alib_characters_chars;character type".
107template<typename TChar>
108struct AppendableTraits<compatibility::std::TIStreamLine<TChar>, TChar, lang::HeapAllocator>
109{
110 /// Reads a line from a text file and appends the contents to \p{target}.
111 /// If the end of the input stream was reached, field
112 /// #"TIStreamLine;IsEOF" of parameter \p{reader} will be set
113 /// to \c true, which indicates that the next read operation would fail if it was performed.
114 ///
115 /// \note
116 /// For setting field <em>IsEOF</em> the object will be cast to a non-constant reference.
117 /// See functor #"AppendableTraits" for an explanation why it is OK to do so.
118 ///
119 /// @param target The AString object to read into.
120 /// @param reader The object holding the \c std::istream and some parameters.
123};
124
125#if DOXYGEN
126}
127#endif
128
129
130namespace compatibility { namespace std {
131
132//==================================================================================================
133/// This class writes \alib_strings into instances of
134/// <c>std::basic_ostream<TChar></c>. The features of this type are:
135/// - Supports writing into narrow and wide output streams (<c>std::ostream</c> and
136/// <c>std::wostream</c>). The character width is controlled with the template parameter \p{TChar}.
137/// - Accepts \alib strings of all three character widths (narrow, wide, and
138/// #"ch xchar;strange" strings).
139/// - The template parameter \p{TSynced} allows syncing the output in multithreaded applications
140/// to avoid interleaving of output.
141/// For this, the C++20 mechanism provided with type
142/// \https{std::osyncstream,https://en.cppreference.com/w/cpp/io/basic_osyncstream} is used.
143/// This allows syncing across C++ libraries that rely on the same standard.<br>
144/// If syncing is enabled, output is emitted to the stream only at the moment that the
145/// corresponding instance of this class is destructed.
146/// - Optionally, this type converts non-conform newline sequences to the desired coding.
147/// For example, if the given strings contain WindowsOS newline codes <c>"\r\n"</c>, on GNU/Linux
148/// platforms, just <c>"\n"</c> may be written. This feature is controlled by the template
149/// parameter \p{TTargetLF}.
150/// - The type counts the number of printed characters with its overloaded method
151/// #".Write(const NString&)".
152/// "Printed" here means that it is how many characters in a <em>UTF8-enconding</em>
153/// would effectively be shown, if printed.
154/// - The type uses \alib allocators for memory management.
155///
156/// It is recommended that only short-living, local instances of this class are created.
157/// This is especially important in the case that parameter \p{TSynced} is \c true.
158/// In this mode, this type follows the
159/// \https{RAII idiom,https://en.cppreference.com/w/cpp/language/raii} just as its then
160/// available member <c>std::osyncstream</c> does.
161///
162/// \note As of 10/2025, some toolchains might not support the C++20 output stream synchronization,
163/// yet. In this case, the code compiles without using it, and the output is not synchronized
164/// and may interleave.<br>
165/// Clang's standard library \e libc++ (which can be activated with
166/// \alib with CMake variable #"alib_manual_build_cmake_3;ALIB_CLANG_USE_LIBCPP"),
167/// in its version \b 200100 supports class <c>std::osyncstream</c> only if the compiler
168/// flag <c>-fexperimental-library</c> is provided.
169///
170/// @tparam TChar The character type of the target <c>std::basic_ostream</c>.
171/// Defaults to #"characters::nchar" (which effectively is C++ \c char).
172/// @tparam TAllocator The #"lang::Allocator;allocator type" to use with
173/// <c>std::osyncstream</c> (if \p{TSynced} is \c true), and with temporary
174/// character conversion buffers in case those need to exceed 4kB.<br>
175/// Defaults to #"HeapAllocator".
176/// @tparam TSynced Determines if the encapsulated <c>std::ostream</c> should be synchronized
177/// on writing. If so, the C++20 mechanism provided with
178/// \https{std::osyncstream,https://en.cppreference.com/w/cpp/io/basic_osyncstream}
179/// is used, as well as \alib's built-in mechanism provided with the global mutex
180/// #"STD_IOSTREAMS_LOCK".
181/// Both mechanics togoether allows syncing across C++ libraries and in
182/// respect to ouput generated with \alib, synchronization between
183/// <c>std::cout</c> and <c>std::cerr</c>.
184/// @tparam TTargetLF Determines whether non-conform line-feeds are adjusted.
185/// The default value is #"LineFeeds;Platform", which converts all
186/// line-feeds to the platforms' standard. Values #"LineFeeds;Unix"
187/// and #"LineFeeds;WindowsOS" can be used to force conversion
188/// to a different standard. Finally, #"LineFeeds;Ignore" disables
189/// the detection and conversion of line-feed codes.
190//==================================================================================================
191template< typename TChar = nchar,
192 typename TAllocator= lang::HeapAllocator,
193 bool TSynced = false,
195requires (::std::is_same_v<TChar, char> || ::std::is_same_v<TChar, wchar_t>)
196class OStreamWriter : public lang::AllocatorMember<TAllocator> {
197 protected:
198 /// The type of the base class that stores the allocator.
200
201 public:
202 static constexpr bool Synced = TSynced; ///< Exposes template parameter \p{TSynced}.
203 using CharType = TChar; ///< Exposes template parameter \p{TChar}.
204 using AllocatorType= TAllocator; ///< Exposes template parameter \p{TAllocator}.
205 static constexpr
206 lang::LineFeeds TargetLF = TTargetLF; ///< Exposes template parameter \p{TAdjustLF}.
207
208 /// This flag may be set after construction to disable the syncing mechanics, even if
209 /// the template parameter \p{TSynced} is set. This is useful, for example, if a using code
210 /// detects recursion, which is not allowed with \c std::osyncstream and
211 /// #"STD_IOSTREAMS_LOCK".
212 bool DisableSync =false;
213
214 protected:
215 #if defined(__cpp_lib_syncbuf) || DOXYGEN
216 /// If unsynced, this member holds the output stream as provided with construction.
217 /// In the synced case, this stores a <c>std::basic_osyncstream</c> which wraps the given
218 /// output stream.
219 ::std::conditional_t<TSynced && !ALIB_SINGLE_THREADED,
220 ::std::basic_osyncstream< TChar,
221 ::std::char_traits<TChar>,
223 ::std::basic_ostream<TChar>& > ostream;
224 #else
225 ::std::basic_ostream<TChar>& ostream;
226 #endif
227
228 /// Implementation of overloaded methods #".Write(const NString&)".
229 /// @param src The string to write.
230 /// @param[out] printedWidth If given, the output width of \p{src} when it was printed
231 /// is returned. (See the class description for further information.)
232 /// @tparam TSrc The character type of the source string.
233 template<typename TSrc>
234 void doWrite( const TString<TSrc>& src, integer* printedWidth ) {
235 integer lineStart= 0;
236 integer lineEnd = 0;
237 if ( printedWidth )
238 *printedWidth= 0;
239 bool done= TTargetLF != lang::LineFeeds::Ignore ? false : true;
240
241 #if !ALIB_SINGLE_THREADED && (defined(__cpp_lib_syncbuf) || DOXYGEN )
242 // create an empty owner and fill it in case that a) this object is synced, and b) the
243 // encapsulated ostream is either std::cout or std::cerr.
245 if constexpr (TSynced)
246 if ( !DisableSync
247 && ( ostream.get_wrapped() == ::std::cout.rdbuf()
248 || ostream.get_wrapped() == ::std::cerr.rdbuf() ) )
249 ioLocker.Set( &threads::STD_IOSTREAMS_LOCK );
250 #endif
251
252 for (;;) {
253 if constexpr ( TTargetLF != lang::LineFeeds::Ignore ) {
254 lineEnd= src.IndexOf('\n', lineStart);
255 if (lineEnd == -1) {
256 lineEnd= src.Length();
257 done= true;
258 } }
259 else
260 lineEnd= src.Length();
261
262 integer writeEnd= lineEnd;
263 if (src.CharAt(writeEnd - 1) == '\r' )
264 --writeEnd;
265 if (writeEnd > lineStart) {
266 if constexpr (::std::is_same_v<TChar, TSrc>) {
267 if ( printedWidth )
268 *printedWidth= TString<TChar>( src.Buffer() + lineStart, writeEnd - lineStart ).WStringLength();
269 ostream.write( src.Buffer() + lineStart, writeEnd - lineStart );
270 } else {
271
274 TString<TSrc> line(src.Buffer() + lineStart, writeEnd - lineStart);
275 converter << line;
276 ostream.write( converter.Buffer(), converter.Length() );
277 if ( printedWidth ) {
278 if constexpr (::std::is_same_v<TChar, wchar>)
279 *printedWidth= converter.Length();
280 else
281 *printedWidth= line.Length();
282 } } }
283
284 if ( done )
285 break;
286
287 // write the line feed as specified by TTargetLF
288 if constexpr ( TTargetLF == lang::LineFeeds::Unix ) {
289 if constexpr ( ::std::is_same_v<TChar, nchar> ) ostream.write( "\n", 1 );
290 else ostream.write( L"\n", 1 );
291 } else if constexpr ( TTargetLF == lang::LineFeeds::WindowsOS ) {
292 if constexpr ( ::std::is_same_v<TChar, nchar> ) ostream.write( "\r\n", 2 );
293 else ostream.write( L"\r\n", 2 );
294 }
295 lineStart= lineEnd + 1;
296 } }
297
298 public:
299 /// Constructor accepting the destination stream.
300 /// @param os The output-stream to use.
301 OStreamWriter(::std::basic_ostream<TChar> &os)
302 : ostream( os ) {}
303
304 #if !defined(__cpp_lib_syncbuf) || ALIB_SINGLE_THREADED || DOXYGEN
305 /// Constructor accepting the destination stream, as well as an allocator to use.
306 /// @param os The output-stream to use.
307 /// @param alloc The allocator to use for the internal buffers.
308 OStreamWriter(::std::basic_ostream<TChar>& os, TAllocator& alloc)
309 : allocBase(alloc)
310 , ostream( os ) {}
311 #else
312 OStreamWriter(::std::basic_ostream<TChar>& os, TAllocator& alloc)
313 requires (!TSynced)
314 : allocBase(alloc)
315 , ostream( os ) {}
316
317 OStreamWriter(::std::basic_ostream<TChar>& os, TAllocator& alloc)
318 requires (TSynced)
319 : allocBase{alloc}
320 , ostream { os.rdbuf() , lang::StdAllocator<TChar, TAllocator>(alloc) } {}
321 #endif
322
323 /// Returns the output stream given with construction, or, if template parameter \p{TSynced}
324 /// is \c true, that output stream wrapped in a <c>std::basic_osyncstream</c>.
325 /// In the latter case, the sync-stream is destructed - and data is written - only with the
326 /// destruction of this instance.
327 /// @return The output stream of this writer.
328 ::std::ostream& GetStream() { return ostream; }
329
330 /// Writes the given narrow string to the stream.
331 ///
332 /// \note
333 /// The output parameter \p{printedWidth} returns the length of the given string, when
334 /// it was converted to wide string. This is done, even if template parameter \p{TChar}
335 /// is narrow and consequently no conversion was performed.<br>
336 /// The value is useful to callers when the "real" output width is needed, i.e., when
337 /// the output text is formatted in tabulator columns or tables.
338 /// Of course, in some locales, this might still not be the correct output width because even
339 /// uni-code characters are not guaranteed to represent exactly one printable character.
340 /// But still, this value is already a much better approximation than the length of the
341 /// given narrow string.
342 ///
343 /// @param src The string to write.
344 /// @param[out] printedWidth If given, the width if \p{src} was printed is returned.
345 /// See the note above. If not given, the method might execute
346 /// slightly faster.
347 void Write( const NString& src, integer* printedWidth= nullptr ) { doWrite(src, printedWidth); }
348
349 /// See sibling method #"Write(const NString& src, integer* printedWidth)".
350 /// @param src The wide string to write.
351 /// @param[out] printedWidth If given, the width if \p{src} was printed is returned.
352 /// See the note above. If not given, the method might execute
353 /// slightly faster.
354 void Write( const WString& src, integer* printedWidth= nullptr ) { doWrite(src, printedWidth); }
355
356 /// See sibling method #"Write(const NString& src, integer* printedWidth)".
357 /// @param src The strange string to write.
358 /// @param[out] printedWidth If given, the width if \p{src} was printed is returned.
359 /// See the note above. If not given, the method might execute
360 /// slightly faster.
361 void Write( const XString& src, integer* printedWidth= nullptr ) { doWrite(src, printedWidth); }
362
363 /// Write the given character repeatedly to the stream.
364 /// @param fillChar The character to write.
365 /// @param count The number of characters to write.
366 void Fill( const TChar fillChar, integer count ) {
367 characters::AlignedCharArray<TChar, 8*sizeof(void*)> alc( fillChar );
368 while (count >= alc.Length() ) {
369 ostream.write(alc.Buffer(), alc.Length());
370 count-= alc.Length();
371 }
372
373 if (count > 0)
374 ostream.write(alc.Buffer(), count);
375 }
376
377
378};
379
380/// @param os The output-stream to use.
381template< typename TChar = nchar,
382 typename TAllocator= lang::HeapAllocator,
383 bool TSynced = false,
385OStreamWriter(::std::basic_ostream<TChar>& os)
387
388/// C++17 Deduction Guide to construct the type #"std::OStreamWriter".
389/// @param os The output-stream to use.
390/// @param allocator The allocator to use for the internal buffers.
391template< typename TChar = nchar,
392 typename TAllocator= lang::HeapAllocator,
393 bool TSynced = false,
395OStreamWriter(::std::basic_ostream<TChar>& os, TAllocator allocator)
397
398//==================================================================================================
399/// This class is a helper-class that converts narrow string data read from an object of
400/// type \c std::istream to the #"characters::character;default character type".
401///
402/// \see Class #"std::OStreamWriter".
403//==================================================================================================
405 protected:
406 /// The string buffer used for conversion.
408
409 /// The input stream as provided with #"SetStream". Will be set to the \c std::cin,
410 /// respectively \c std::win in the constructor.
412
413
414 public:
415 /// Constructor.Invokes #".SetStream" passing \c std::cin.
416 /// @param bufferSize The number of characters that the buffer is increased while reading
417 /// parts of the line. Defaults to 4096 characters.
418 /// @param maxLineWidth The maximum length of a single line to be read. Longer lines
419 /// get truncated. Defaults to 4096 characters.
420 IStreamReader( integer bufferSize= 4096, integer maxLineWidth = 4096 )
421 : readOp( &::std::cin, lang::CurrentData::Clear, bufferSize, maxLineWidth ) {}
422
423 /// Sets the input stream.
424 /// @param is Pointer to the input stream to read from to.
425 void SetStream( ::std::istream* is ) { readOp.IStream= is; }
426
427 /// Returns the input stream previously set with #".SetStream".
428 /// @return The input stream set with #".SetStream".
429 ::std::istream* GetStream() { return readOp.IStream; }
430
431 /// Returns \c true if the input stream signaled its end, \c false otherwise.
432 /// @return \c true if the input stream is known to be at its end, \c false otherwise.
433 bool IsEOF() { return readOp.IsEOF; }
434
435
436 /// Reads one line of text from the input stream into a narrow string.
437 /// @param target The storage buffer for the string to read. This string will be cleared
438 /// independent of the availability of input data.
439 void Read( NAString& target ) { target.Reset( readOp ); }
440
441 /// Reads one line of text from the internal input stream into a wide string.
442 /// @param target The storage buffer for the string to read. This string will be cleared
443 /// independent of the availability of input data.
444 void Read( WAString& target ) {
445 target.Reset();
446 converter.Reset( readOp );
447 target << converter;
448 }
449};
450
451}}} // namespace alib[::strings::compatibility::std]
452
453/// Type alias in namespace #"%alib".
455
456/// Type alias in namespace #"%alib".
458
459/// Type alias in namespace #"%alib".
461
462/// Type alias in namespace #"%alib".
463template< typename TChar = nchar,
464 typename TAllocator= lang::HeapAllocator,
465 bool TSynced = false,
468 TTargetLF>;
469
470/// Type alias in namespace #"%alib".
472
473//##################################################################################################
474//##################################### std::ostream& operator<< ###################################
475//##################################################################################################
476#if DOXYGEN
477 namespace strings { namespace compatibility { namespace std {
478#else
479} // namespace [alib]
480#endif
481
482//==================================================================================================
483/// Copies the contents of the given #"%NString" to into the \c std::ostream given as reference.
484///
485/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
486/// @param stream The ostream object to write the given String into.
487/// @param string The String to write into the given ostream.
488/// @returns The ostream to allow concatenated operations.
489//==================================================================================================
491inline std::ostream& operator<<( std::ostream& stream, const alib::NString& string ) {
492 if ( string.IsNotEmpty() )
493 stream.write( string.Buffer(), string.Length() );
494 return stream;
495}
496
497//==================================================================================================
498/// Copies the contents of the given #"%NString" to into the \c std::ostream given as pointer.
499///
500/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
501/// @param stream The ostream object to write the given String into.
502/// @param string The String to write into the given ostream.
503/// @returns The ostream to allow concatenated operations.
504//==================================================================================================
506inline std::ostream* operator<<( std::ostream* stream, const alib::NString& string )
507{
508 stream->write( string.Buffer(), string.Length() );
509 return stream;
510}
511
512//==================================================================================================
513/// Copies the contents of the given #"%WString" to into the \c std::ostream given as reference.
514///
515/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
516/// @param stream The ostream object to write the given String into.
517/// @param string The String to write into the given ostream.
518/// @returns The ostream to allow concatenated operations.
519//==================================================================================================
521ALIB_DLL std::ostream& operator<<( std::ostream& stream, const alib::WString& string );
522
523//==================================================================================================
524/// Copies the contents of the given #"%WString" to into the \c std::ostream given as pointer.
525///
526/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
527/// @param stream The ostream object to write the given String into.
528/// @param string The String to write into the given ostream.
529/// @returns The ostream to allow concatenated operations.
530//==================================================================================================
532inline std::ostream* operator<<( std::ostream* stream, const alib::WString& string )
533{
534 (*stream) << string;
535 return stream;
536}
537
538//==================================================================================================
539/// Copies the contents of the given #"%NString" to into the \c std::wostream given as reference.
540///
541/// \note
542/// This operator uses a local string buffer of 256 bytes size to convert the given narrow string
543/// to an string of \c wchar_t characters that the output stream accepts. In case that
544/// the given \p{string} is larger, a dynamic memory allocation has to be made.<br>
545/// In performance-critical code that writes larger string data, a custom conversion method,
546/// that, for example, reuses a buffer, may be appropriate.
547///
548/// <p>
549/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
550/// @param stream The ostream object to write the given String into.
551/// @param string The String to write into the given ostream.
552/// @returns The ostream to allow concatenated operations.
553//==================================================================================================
555ALIB_DLL std::wostream& operator<<( std::wostream& stream, const alib::NString& string );
556
557//==================================================================================================
558/// Copies the contents of the given #"%NString" to into the \c std::wostream given as pointer.
559///
560/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
561/// \see The notes on memory efficiency, documented with operator
562/// #"operator<<(std::wostream&,const alib::NString&)"
563/// which this operator uses inline.
564/// @param stream The ostream object to write the given String into.
565/// @param string The String to write into the given ostream.
566/// @returns The ostream to allow concatenated operations.
567//==================================================================================================
569inline std::wostream* operator<<( std::wostream* stream, const alib::NString& string )
570{
571 (*stream) << string;
572 return stream;
573}
574
575//==================================================================================================
576/// Copies the contents of the given #"%WString" to into the \c std::wostream given as reference.
577///
578/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
579/// @param stream The ostream object to write the given String into.
580/// @param string The String to write into the given ostream.
581/// @returns The ostream to allow concatenated operations.
582//==================================================================================================
584inline std::wostream& operator<<( std::wostream& stream, const alib::WString& string ) {
585 if ( string.IsNotEmpty() ) {
586 #if ALIB_CHARACTERS_NATIVE_WCHAR
587 stream.write( string.Buffer(), string.Length() );
588 #else
589 alib::XLocalString<1024> converter( string );
591 stream.write( converter.Buffer(), converter.Length() );
592 #endif
593 }
594 return stream;
595}
596
597//==================================================================================================
598/// Copies the contents of the given #"%WString" to into the \c std::wostream given as pointer.
599///
600/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
601/// @param stream The ostream object to write the given String into.
602/// @param string The String to write into the given ostream.
603/// @returns The ostream to allow concatenated operations.
604//==================================================================================================
606inline std::wostream* operator<<( std::wostream* stream, const alib::WString& string )
607{
608 (*stream) << string;
609 return stream;
610}
611
612//==================================================================================================
613/// Clears the given #"%NAString" and extracts data from the std::istream into it. The extraction
614/// ends with either the end of the std::istream or when reading a newline character.
615///
616/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
617/// @param stream The istream object to extract data from.
618/// @param string The AString to receive data.
619/// @returns The ostream to allow concatenated operations.
620//==================================================================================================
622inline std::istream& operator>>( std::istream& stream, alib::NAString& string ) {
623 string << alib::strings::compatibility::std::TIStreamLine<alib::nchar>( &stream,
625 return stream;
626}
627
629//==================================================================================================
630/// Clears the given #"%NAString" and extracts data from the std::istream into it. The extractions
631/// ends with either the end of the std::istream or when reading a newline character.
632///
633/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
634/// @param stream The istream object to extract data from.
635/// @param string The AString to receive data.
636/// @returns The ostream to allow concatenated operations.
637//==================================================================================================
639inline std::istream* operator>>( std::istream* stream, alib::NAString& string ) {
640 ALIB_ASSERT_WARNING( stream != nullptr, "STRINGS", "Given std::IStream is nullptr" )
641
642 if (stream != nullptr)
643 string << alib::strings::compatibility::std::TIStreamLine<alib::nchar>( stream,
645 return stream;
646}
647
648//==================================================================================================
649/// Clears the given #"%WAString" and extracts data from the std::istream into it. The extractions
650/// ends with either the end of the std::istream or when reading a newline character.
651///
652/// \note
653/// If code selection macro #"ALIB_CHARACTERS_NATIVE_WCHAR" evaluates to false, a local buffer
654/// is used to convert the string of \c wchar_t characters that the input stream provides.
655/// In case that the string read from the stream is larger, a dynamic memory allocation has to
656/// be made.<br>
657/// In performance-critical code that receives larger string data, a custom conversion method
658/// that, for example, reuses a buffer may be appropriate.
659///
660/// <p>
661/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
662/// @param stream The istream object to extract data from.
663/// @param string The AString to receive data.
664/// @returns The ostream to allow concatenated operations.
665//==================================================================================================
667inline std::basic_istream<wchar_t>& operator>>( std::basic_istream<wchar_t>& stream,
668 alib::WAString& string ) {
669 #if ALIB_CHARACTERS_NATIVE_WCHAR
670 string << alib::strings::compatibility::std::TIStreamLine<wchar_t>( &stream,
672 #else
673 alib::XLocalString<1024> converter;
675 converter << alib::strings::compatibility::std::TIStreamLine<wchar_t>( &stream,
677 string.Reset( converter );
678 #endif
679 return stream;
680}
681
682//==================================================================================================
683/// Clears the given #"%WAString" and extracts data from the std::istream into it. The extractions
684/// ends with either the end of the std::istream or when reading a newline character.
685///
686/// \note Unlike this documentation indicates, the operator is defined in the global namespace.
687///
688/// \see The notes on memory efficiency, documented with operator
689/// #"operator>>(std::basic_istream<wchar_t>&, alib::WAString& )"
690/// which this operator uses inline.
691/// @param stream The istream object to extract data from.
692/// @param string The AString to receive data.
693/// @returns The ostream to allow concatenated operations.
694//==================================================================================================
696inline std::basic_istream<wchar_t>* operator>>( std::basic_istream<wchar_t>* stream,
697 alib::WAString& string ) {
698 ALIB_ASSERT_WARNING ( stream != nullptr, "STRINGS", "Given std::istream is nullptr" )
699
700 if (stream != nullptr)
701 (*stream) >> string;
702 return stream;
703}
704
705#include "ALib.Lang.CIMethods.H"
706
707
708//==================================================================================================
709/// Copies the contents of the given #"alib_strings_assembly_ttostring;appendable type"
710/// the \c std::ostream given as reference.
711///
712/// \note Unlike this documentation indicates, this operator is defined in the global namespace.
713///
714/// @tparam TAppendable The appendable type.
715/// @param stream The \c std::ostream object to write the given String into.
716/// @param appendable The object whose contents is to be written into the given \p{stream}.
717/// @returns The ostream to allow concatenated operations.
718//==================================================================================================
720template<typename TAppendable>
723std::ostream& operator<<( std::ostream& stream, const TAppendable& appendable ) {
726
727 if ( buf._(appendable).IsNotEmpty() )
728 stream.write( buf.Buffer(), buf.Length() );
729 return stream;
730}
731
732//==================================================================================================
733/// Copies the contents of the given #"AppendableTraits;appendable type" the \c std::ostream
734/// given as pointer.
735///
736/// \note Unlike this documentation indicates, this operator is defined in the global namespace.
737///
738/// @tparam TAppendable The appendable type.
739/// @param stream The \c std::ostream object to write the given String into.
740/// @param appendable The object whose contents is to be written into the given \p{stream}.
741/// @returns The ostream to allow concatenated operations.
742//==================================================================================================
744template<typename TAppendable>
747std::ostream* operator<<( std::ostream* stream, const TAppendable& appendable ) {
748 if (stream != nullptr)
749 operator<<( * stream, appendable );
750 return stream;
751}
752
753//==================================================================================================
754/// Copies the contents of the given #"AppendableTraits;appendable type" the \c std::ostream
755/// given as reference.
756///
757/// \note Unlike this documentation indicates, this operator is defined in the global namespace.
758///
759/// @tparam TAppendable The appendable type.
760/// @param stream The \c std::ostream object to write the given String into.
761/// @param appendable The object whose contents is to be written into the given \p{stream}.
762/// @returns The ostream to allow concatenated operations.
763//==================================================================================================
765template<typename TAppendable>
768std::wostream& operator<<( std::wostream& stream, const TAppendable& appendable ) {
769 #if ALIB_CHARACTERS_NATIVE_WCHAR
771 #else
773 #endif
775
776 if ( buf._(appendable).IsNotEmpty() )
777 stream.write( buf.Buffer(), buf.Length() );
778 return stream;
779}
780
781//==================================================================================================
782/// Copies the contents of the given #"AppendableTraits;appendable type" the \c std::ostream
783/// given as a pointer.
784///
785/// \note Unlike this documentation indicates, this operator is defined in the global namespace.
786///
787/// @tparam TAppendable The appendable type.
788/// @tparam TAllocator The allocator type, as prototyped with class #"lang::Allocator".
789/// @param stream The \c std::ostream object to write the given String into.
790/// @param appendable The object whose contents is to be written into the given \p{stream}.
791/// @returns The ostream to allow concatenated operations.
792//==================================================================================================
794template<typename TAppendable, typename TAllocator>
797std::wostream* operator<<( std::wostream* stream, const TAppendable& appendable ) {
798 if (stream != nullptr)
799 operator<<( * stream, appendable );
800 return stream;
801}
802
803
804
805#if DOXYGEN
806 }} namespace APPENDABLES {
807#else
809namespace alib { namespace strings { // the real namespace
810#endif
811
812
814 ::operator()( TAString<char , lang::HeapAllocator>& target, const strings::compatibility::std::TIStreamLine<char >& reader );
815
817 ::operator()( TAString<wchar_t, lang::HeapAllocator>& target, const strings::compatibility::std::TIStreamLine<wchar_t>& reader );
818
819#if DOXYGEN
820 }} // namespace alib[::strings::APPENDABLES]
821#else
822 } // namespace alib[::strings]
823#endif
824
825} // namespace [alib]
826#include "ALib.Lang.CIMethods.H"
827
833
#define ALIB_DLL
#define ALIB_COMMA_CALLER_PRUNED
#define ALIB_SINGLE_THREADED
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_EXPORT
void Set(OwnablePointer ownable)
Definition owner.hpp:102
TAString & _(const TAppendable &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr integer Length() const
Definition string.hpp:300
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:799
TChar CharAt(integer idx) const
Definition string.hpp:399
constexpr bool IsNotEmpty() const
Definition string.hpp:353
constexpr const TChar * Buffer() const
Definition string.hpp:295
integer WStringLength() const
IStreamReader(integer bufferSize=4096, integer maxLineWidth=4096)
compatibility::std::TIStreamLine< nchar > readOp
NAString converter
The string buffer used for conversion.
::std::conditional_t< TSynced &&!0, ::std::basic_osyncstream< TChar, ::std::char_traits< TChar >, lang::StdAllocator< TChar, TAllocator > >, ::std::basic_ostream< TChar > & > ostream
void Write(const NString &src, integer *printedWidth=nullptr)
void Write(const XString &src, integer *printedWidth=nullptr)
OStreamWriter(::std::basic_ostream< TChar > &os)
lang::AllocatorMember< TAllocator > allocBase
The type of the base class that stores the allocator.
void Write(const WString &src, integer *printedWidth=nullptr)
OStreamWriter(::std::basic_ostream< TChar > &os, TAllocator &alloc)
void doWrite(const TString< TSrc > &src, integer *printedWidth)
TChar CharType
Exposes template parameter TChar.
void Fill(const TChar fillChar, integer count)
TAllocator AllocatorType
Exposes template parameter TAllocator.
LineFeeds
Denotes line-feed encoding sequences "\n" and "\r\n".
@ WindowsOS
WindowsOS style line-feeds "\r\n".
@ Platform
Platform specific. Equals to either 'Unix' or 'WindowsOS'.
@ Unix
Unix-style line-feeds "\n".
@ Ignore
Same as 'None'.
@ Keep
Chooses not no clear existing data.
@ Clear
Chooses to clear existing data.
ALIB_EXPORT std::istream & operator>>(std::istream &stream, alib::NAString &string)
ALIB_EXPORT std::ostream & operator<<(std::ostream &stream, const alib::NString &string)
Lock STD_IOSTREAMS_LOCK
Definition locks.cpp:17
Definition alox.cpp:14
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
strings::compatibility::std::IStreamReader IStreamReader
Type alias in namespace #"%alib".
strings::TString< wchar > WString
Type alias in namespace #"%alib".
Definition string.hpp:2177
strings::compatibility::std::TIStreamLine< alib::wchar > IStreamLineW
Type alias in namespace #"%alib".
strings::TLocalString< wchar, TCapacity, lang::HeapAllocator > WLocalString
Type alias in namespace #"%alib".
strings::TString< xchar > XString
Type alias in namespace #"%alib".
Definition string.hpp:2180
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
strings::compatibility::std::TIStreamLine< alib::character > IStreamLine
Type alias in namespace #"%alib".
strings::TLocalString< xchar, TCapacity, lang::HeapAllocator > XLocalString
Type alias in namespace #"%alib".
characters::nchar nchar
Type alias in namespace #"%alib".
strings::compatibility::std::TIStreamLine< alib::nchar > IStreamLineN
Type alias in namespace #"%alib".
NLocalString< 256 > NString256
Type alias name for #"TLocalString;TLocalString<nchar,256>".
strings::TAString< wchar, lang::HeapAllocator > WAString
Type alias in namespace #"%alib".
strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF > OStreamWriter
Type alias in namespace #"%alib".
#define ALIB_STRINGS_SUPPRESS_STD_OSTREAM_OPERATOR(TYPE)
TAllocator & GetAllocator() const noexcept
void operator()(TAString< TChar, lang::HeapAllocator > &target, const compatibility::std::TIStreamLine< TChar > &reader)
TIStreamLine(::std::basic_istream< TChar > *istream, lang::CurrentData targetData=lang::CurrentData::Clear, integer bufferSize=4096, integer maxLineWidth=4096)