OSpanStream Class Reference

Similar to OStringStream but using an externally managed buffer for its output. More...

Inheritance diagram for OSpanStream:
Collaboration diagram for OSpanStream:

Public Member Functions

 OSpanStream (IOstreamOption streamOpt=IOstreamOption())
 Default construct (empty output) More...
 
 OSpanStream (std::string &buffer, IOstreamOption streamOpt=IOstreamOption())
 Use data area from string content. More...
 
 OSpanStream (char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
 Construct using specified buffer and number of bytes. More...
 
 OSpanStream (::Foam::UList< char > &buffer, IOstreamOption streamOpt=IOstreamOption())
 Construct using data area from a List and its inherent storage size. More...
 
template<int SizeMin>
 OSpanStream (::Foam::DynamicList< char, SizeMin > &buffer, IOstreamOption streamOpt=IOstreamOption())
 Construct using full data area from DynamicList. More...
 
std::streampos tellp () const
 Position of the put buffer. More...
 
std::streampos output_pos () const
 The current output position within the buffer (tellp) More...
 
label size () const
 The current output size. Same as tellp(), output_pos() More...
 
std::streamsize capacity () const
 The put buffer capacity. More...
 
UList< char > list () const
 Span of the current output characters (is modifiable!) More...
 
auto view () const -> decltype(stream_.view())
 A string_view (c++17) or span view (older c++) of buffer contents. More...
 
auto str () const -> decltype(stream_.str())
 For OStringStream compatibility, return buffer as string copy. More...
 
void reset (char *buffer, size_t nbytes)
 Reset the put area. More...
 
void reset (std::string &s)
 Reset the put buffer area to use the data area from a string. More...
 
virtual void rewind ()
 Rewind the stream, clearing any old errors. More...
 
virtual void print (Ostream &os) const override
 Print stream description to Ostream. More...
 
- Public Member Functions inherited from OSstream
 OSstream (const OSstream &)=default
 Copy construct. More...
 
void operator= (const OSstream &)=delete
 No copy assignment. More...
 
 OSstream (std::ostream &os, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
 Construct wrapper around std::ostream, set stream status. More...
 
 OSstream (std::ostream &os, const string &streamName, IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
 Construct wrapper around std::ostream, set stream status. More...
 
 OSstream (std::ostream &os, const string &streamName, IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
 Construct wrapper around std::ostream, set stream status. More...
 
virtual const fileNamename () const override
 Get the name of the output serial stream. (eg, the name of the Fstream file name) More...
 
virtual const std::ostream & stdStream () const
 Const access to underlying std::ostream. More...
 
virtual std::ostream & stdStream ()
 Access to underlying std::ostream. More...
 
virtual ios_base::fmtflags flags () const override
 Get stream flags. More...
 
virtual ios_base::fmtflags flags (const ios_base::fmtflags f) override
 Set stream flags. More...
 
void syncState ()
 Set stream state to match that of the std::ostream. More...
 
virtual bool write (const token &tok) override
 Write token to stream or otherwise handle it. More...
 
virtual Ostreamwrite (const char c) override
 Write character. More...
 
virtual OstreamwriteQuoted (const char *str, std::streamsize len, const bool quoted=true) override
 Write character/string content, with/without surrounding quotes. More...
 
virtual Ostreamwrite (const char *str) override
 Write character string. More...
 
virtual Ostreamwrite (const word &str) override
 Write word. More...
 
virtual Ostreamwrite (const std::string &str) override
 Write string (quoted) More...
 
virtual Ostreamwrite (const int32_t val) override
 Write int32_t. More...
 
virtual Ostreamwrite (const int64_t val) override
 Write int64_t. More...
 
virtual Ostreamwrite (const float val) override
 Write float. More...
 
virtual Ostreamwrite (const double val) override
 Write double. More...
 
virtual Ostreamwrite (const char *data, std::streamsize count) override
 Write binary block. More...
 
virtual OstreamwriteRaw (const char *data, std::streamsize count) override
 Low-level raw binary output. More...
 
virtual bool beginRawWrite (std::streamsize count) override
 Begin marker for low-level raw binary output. More...
 
virtual bool endRawWrite () override
 End marker for low-level raw binary output. More...
 
virtual void indent () override
 Add indentation characters. More...
 
virtual void flush () override
 Flush stream. More...
 
virtual void endl () override
 Add newline and flush stream. More...
 
virtual char fill () const override
 Get the current padding character. More...
 
virtual char fill (const char fillch) override
 Set padding character for formatted field up to field width. More...
 
virtual int width () const override
 Get width of output field. More...
 
virtual int width (const int w) override
 Set width of output field. More...
 
virtual int precision () const override
 Get precision of output field. More...
 
virtual int precision (const int p) override
 Set precision of output field. More...
 
virtual OstreamwriteQuoted (const char *str, std::streamsize len, const bool quoted=true)=0
 Inherit write methods from Ostream. More...
 
virtual OstreamwriteQuoted (const std::string &str, const bool quoted=true)
 Inherit write methods from Ostream. More...
 
- Public Member Functions inherited from Ostream
 Ostream (const Ostream &)=default
 Copy construct. More...
 
virtual ~Ostream ()=default
 Destructor. More...
 
 Ostream (IOstreamOption streamOpt=IOstreamOption())
 Default construct (ASCII, uncompressed), construct with specified stream option. More...
 
 Ostream (IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
 Construct with format (uncompressed) More...
 
virtual OstreamwriteQuoted (const std::string &str, const bool quoted=true)
 Write string content, with/without surrounding quotes. More...
 
virtual Ostreamwrite (const keyType &kw)
 Write keyType. More...
 
unsigned short indentSize () const noexcept
 Return indent size (spaces per level) More...
 
unsigned short indentSize (unsigned short val) noexcept
 Change indent size (spaces per level), return old value. More...
 
unsigned short indentLevel () const noexcept
 Return the indent level. More...
 
unsigned short indentLevel (unsigned short val) noexcept
 Change the indent level, return old value. More...
 
void incrIndent () noexcept
 Increment the indent level. More...
 
void decrIndent ()
 Decrement the indent level. More...
 
virtual OstreamwriteKeyword (const keyType &kw)
 Write the keyword followed by an appropriate indentation. More...
 
virtual OstreambeginBlock (const keyType &kw)
 Write begin block group with the given name. More...
 
virtual OstreambeginBlock ()
 Write begin block group without a name. More...
 
virtual OstreamendBlock ()
 Write end block group. More...
 
virtual OstreamendEntry ()
 Write end entry (';') followed by newline. More...
 
template<class T >
OstreamwriteEntry (const keyType &key, const T &value)
 Write a keyword/value entry. More...
 
template<class T >
OstreamwriteEntryIfDifferent (const word &key, const T &value1, const T &value2)
 Write a keyword/value entry only when the two values differ. More...
 
Ostreamoperator() () const
 Return a non-const reference to const Ostream. More...
 
unsigned short & indentLevel () noexcept
 Access to indent level. More...
 
unsigned short & indentSize () noexcept
 Access to indent size. More...
 
- Public Member Functions inherited from IOstream
 IOstream (const IOstream &)=default
 Copy construct. More...
 
virtual ~IOstream ()=default
 Destructor. More...
 
 IOstream (IOstreamOption streamOpt=IOstreamOption())
 Default construct (ASCII, uncompressed), construct with specified stream option. More...
 
 IOstream (IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
 Construct with format, version (compression) More...
 
fileName relativeName () const
 Return the name of the stream relative to the current case. More...
 
virtual bool check (const char *operation) const
 Check IOstream status for given operation. More...
 
bool fatalCheck (const char *operation) const
 Check IOstream status for given operation. More...
 
bool opened () const noexcept
 True if stream has been opened. More...
 
bool closed () const noexcept
 True if stream is closed. More...
 
bool good () const noexcept
 True if next operation might succeed. More...
 
bool eof () const noexcept
 True if end of input seen. More...
 
bool fail () const noexcept
 True if next operation will fail. More...
 
bool bad () const noexcept
 True if stream is corrupted. More...
 
 operator bool () const noexcept
 Return true if the stream has not failed. More...
 
bool operator! () const noexcept
 Return true if the stream has failed. More...
 
unsigned labelByteSize () const noexcept
 The sizeof (label) in bytes associated with the stream. More...
 
unsigned scalarByteSize () const noexcept
 The sizeof (scalar) in bytes associated with the stream. More...
 
void setLabelByteSize (unsigned nbytes) noexcept
 Set the sizeof (label) in bytes associated with the stream. More...
 
void setScalarByteSize (unsigned nbytes) noexcept
 Set the sizeof (scalar) in bytes associated with the stream. More...
 
template<class T = label>
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize () const noexcept
 Check if the label byte-size associated with the stream is the same as the given type. More...
 
template<class T = scalar>
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize () const noexcept
 Check if the scalar byte-size associated with the stream is the same as the given type. More...
 
label lineNumber () const noexcept
 Const access to the current stream line number. More...
 
label & lineNumber () noexcept
 Non-const access to the current stream line number. More...
 
label lineNumber (const label num) noexcept
 Set the stream line number. More...
 
void setEof () noexcept
 Set stream state as reached 'eof'. More...
 
void setFail () noexcept
 Set stream state as 'failed'. More...
 
void setBad ()
 Set stream state to be 'bad'. More...
 
ios_base::fmtflags setf (const ios_base::fmtflags f)
 Set flags of stream. More...
 
ios_base::fmtflags setf (const ios_base::fmtflags f, const ios_base::fmtflags mask)
 Set flags of given field of stream. More...
 
void unsetf (const ios_base::fmtflags f)
 Unset flags of stream. More...
 
void print (Ostream &os, const int streamState) const
 Print information about the stream state bits. More...
 
InfoProxy< IOstreaminfo () const noexcept
 Return info proxy, used to print IOstream information to a stream. More...
 
- Public Member Functions inherited from IOstreamOption
constexpr IOstreamOption (streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
 Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression. More...
 
constexpr IOstreamOption (streamFormat fmt, compressionType comp, versionNumber ver) noexcept
 Construct from components (format, compression, version) More...
 
constexpr IOstreamOption (streamFormat fmt, versionNumber ver, compressionType comp=compressionType::UNCOMPRESSED) noexcept
 Construct from components (format, version, compression) More...
 
 IOstreamOption (const IOstreamOption &opt, streamFormat fmt) noexcept
 Copy construct with change of format. More...
 
streamFormat format () const noexcept
 Get the current stream format. More...
 
streamFormat format (const streamFormat fmt) noexcept
 Set the stream format. More...
 
streamFormat format (const word &formatName)
 Set the stream format from string value. More...
 
compressionType compression () const noexcept
 Get the stream compression. More...
 
compressionType compression (const compressionType comp) noexcept
 Set the stream compression. More...
 
compressionType compression (const word &compName)
 Set the stream compression from string value. More...
 
versionNumber version () const noexcept
 Get the stream version. More...
 
versionNumber version (const versionNumber ver) noexcept
 Set the stream version. More...
 
versionNumber version (const token &tok)
 Set the stream version from token. More...
 

Additional Inherited Members

- Public Types inherited from IOstream
enum  streamAccess : char { CLOSED = 0, OPENED }
 Enumeration for stream open/closed state. More...
 
- Public Types inherited from IOstreamOption
enum  streamFormat : char { ASCII = 0, BINARY }
 Data format (ascii | binary) More...
 
enum  compressionType : char { UNCOMPRESSED = 0, COMPRESSED }
 Compression treatment (UNCOMPRESSED | COMPRESSED) More...
 
enum  appendType : char { NON_APPEND = 0, APPEND }
 File appending (NON_APPEND | APPEND) More...
 
enum  atomicType : char { NON_ATOMIC = 0, ATOMIC }
 Atomic operations (output) More...
 
enum  floatFormat : unsigned { general = unsigned(0), fixed = unsigned(std::ios_base::fixed), scientific = unsigned(std::ios_base::scientific) }
 Float formats (eg, time directory name formats) More...
 
- Static Public Member Functions inherited from IOstream
static unsigned int defaultPrecision () noexcept
 Return the default precision. More...
 
static unsigned int defaultPrecision (unsigned int prec) noexcept
 Reset the default precision. More...
 
- Static Public Member Functions inherited from IOstreamOption
static floatFormat floatFormatEnum (const word &fmtName, const floatFormat deflt=floatFormat::general)
 Lookup floatFormat enum corresponding to the string (general | fixed | scientific). More...
 
static floatFormat floatFormatEnum (const word &key, const dictionary &dict, const floatFormat deflt=floatFormat::general)
 getOrDefault floatFormat from dictionary, warn only on bad enumeration. More...
 
static streamFormat formatEnum (const word &fmtName, const streamFormat deflt=streamFormat::ASCII)
 Lookup streamFormat enum corresponding to the string (ascii | binary). More...
 
static streamFormat formatEnum (const word &key, const dictionary &dict, const streamFormat deflt=streamFormat::ASCII)
 getOrDefault streamFormat from dictionary, warn only on bad enumeration. More...
 
static compressionType compressionEnum (const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
 The compression enum corresponding to the string. More...
 
static compressionType compressionEnum (const word &key, const dictionary &dict, const compressionType deflt=compressionType::UNCOMPRESSED)
 getOrDefault compressionType from dictionary, warn only on bad enumeration. More...
 
- Static Public Attributes inherited from IOstream
static unsigned int precision_
 Default precision. More...
 
- Static Public Attributes inherited from IOstreamOption
static const Enum< floatFormatfloatFormatNames
 Names for float formats (general, fixed, scientific) More...
 
static const Enum< streamFormatformatNames
 Stream format names (ascii, binary) More...
 
static const versionNumber currentVersion
 The current version number (2.0) More...
 
- Protected Member Functions inherited from OSpanStreamAllocator
 OSpanStreamAllocator ()=default
 Default construct (empty) More...
 
- Protected Member Functions inherited from IOstream
void setOpened () noexcept
 Set stream opened. More...
 
void setClosed () noexcept
 Set stream closed. More...
 
void setState (std::ios_base::iostate state) noexcept
 Set stream state. More...
 
void setGood () noexcept
 Set stream state to be good. More...
 
- Protected Attributes inherited from OSpanStreamAllocator
Foam::ospanstream stream_
 The stream. More...
 
- Protected Attributes inherited from Ostream
unsigned short indentSize_ = 4
 Number of spaces per indent level. More...
 
unsigned short indentLevel_ = 0
 Current indent level. More...
 
- Protected Attributes inherited from IOstream
std::ios_base::iostate ioState_
 Mirror of internal stream io state. More...
 
streamAccess openClosed_
 The stream open/closed state. More...
 
unsigned char sizeofLabel_
 The sizeof (label), possibly read from the header. More...
 
unsigned char sizeofScalar_
 The sizeof (scalar), possibly read from the header. More...
 
label lineNumber_
 The file line. More...
 
- Static Protected Attributes inherited from Ostream
static constexpr const unsigned short entryIndentation_ = 16
 Indentation of the entry from the start of the keyword. More...
 
- Static Protected Attributes inherited from IOstream
static fileName staticName_
 Name for any generic stream - normally treat as readonly. More...
 

Detailed Description

Similar to OStringStream but using an externally managed buffer for its output.

This allows the output buffer to be reused and can make it easier when writing out data. It is the user's responsibility to ensure proper synchronization in the sizes. Provided that the external buffer is large enough that overflow does not occur, the following usage pattern works.

DynamicList<char> buffer(4096); // allocate some large buffer
{
OSpanStream os(buffer);
os << "content1" << " and more content";
buffer.resize(os.size()); // synchronize sizes
}
something.write(buffer, buffer.size());

Although the OSpanStream is quite lightweight, there may be cases where it is preferable to reuse the stream as well.

DynamicList<char> buffer(4096); // allocate some large buffer
OSpanStream os(buffer);
os << "content1" << " and more content";
buffer.resize(os.size()); // synchronize sizes
something.write(buffer, buffer.size());
os.rewind();
os << "content2";
buffer.resize(os.size()); // synchronize sizes
something.write(buffer, buffer.size());
// or simply using the output size directly (without sync)
os.rewind();
os << "content3";
something.write(buffer, os.size());
See also
Foam::ICharStream Foam::ISpanStream Foam::OCharStream

Definition at line 263 of file OSpanStream.H.

Constructor & Destructor Documentation

◆ OSpanStream() [1/5]

OSpanStream ( IOstreamOption  streamOpt = IOstreamOption())
inlineexplicit

Default construct (empty output)

Definition at line 278 of file OSpanStream.H.

◆ OSpanStream() [2/5]

OSpanStream ( std::string &  buffer,
IOstreamOption  streamOpt = IOstreamOption() 
)
inlineexplicit

Use data area from string content.

Definition at line 290 of file OSpanStream.H.

References ospanstream::reset(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ OSpanStream() [3/5]

OSpanStream ( char *  buffer,
size_t  nbytes,
IOstreamOption  streamOpt = IOstreamOption() 
)
inline

Construct using specified buffer and number of bytes.

Definition at line 304 of file OSpanStream.H.

References ospanstream::reset(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ OSpanStream() [4/5]

OSpanStream ( ::Foam::UList< char > &  buffer,
IOstreamOption  streamOpt = IOstreamOption() 
)
inlineexplicit

Construct using data area from a List and its inherent storage size.

Definition at line 319 of file OSpanStream.H.

◆ OSpanStream() [5/5]

OSpanStream ( ::Foam::DynamicList< char, SizeMin > &  buffer,
IOstreamOption  streamOpt = IOstreamOption() 
)
inlineexplicit

Construct using full data area from DynamicList.

Definition at line 332 of file OSpanStream.H.

References DynamicList< T, SizeMin >::capacity(), and DynamicList< T, SizeMin >::resize().

Here is the call graph for this function:

Member Function Documentation

◆ tellp()

std::streampos tellp ( ) const
inline

Position of the put buffer.

Definition at line 348 of file OSpanStream.H.

References ospanstream::output_pos(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ output_pos()

std::streampos output_pos ( ) const
inline

The current output position within the buffer (tellp)

Definition at line 353 of file OSpanStream.H.

References ospanstream::output_pos(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ size()

label size ( ) const
inline

The current output size. Same as tellp(), output_pos()

Definition at line 358 of file OSpanStream.H.

References ospanstream::output_pos(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ capacity()

std::streamsize capacity ( ) const
inline

The put buffer capacity.

Definition at line 363 of file OSpanStream.H.

References ospanstream::capacity(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ list()

UList<char> list ( ) const
inline

Span of the current output characters (is modifiable!)

Definition at line 368 of file OSpanStream.H.

References ospanstream::list(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ view()

auto view ( ) const -> decltype(stream_.view())
inline

A string_view (c++17) or span view (older c++) of buffer contents.

Definition at line 373 of file OSpanStream.H.

References OSpanStreamAllocator::stream_, and ospanstream::view().

Here is the call graph for this function:

◆ str()

auto str ( ) const -> decltype(stream_.str())
inline

For OStringStream compatibility, return buffer as string copy.

Use sparingly - it creates a full copy!!

Definition at line 383 of file OSpanStream.H.

References ospanstream::str(), and OSpanStreamAllocator::stream_.

Here is the call graph for this function:

◆ reset() [1/2]

void reset ( char *  buffer,
size_t  nbytes 
)
inline

Reset the put area.

Definition at line 391 of file OSpanStream.H.

References ospanstream::reset(), OSpanStreamAllocator::stream_, and OSstream::syncState().

Here is the call graph for this function:

◆ reset() [2/2]

void reset ( std::string &  s)
inline

Reset the put buffer area to use the data area from a string.

Definition at line 400 of file OSpanStream.H.

References ospanstream::reset(), s, OSpanStreamAllocator::stream_, and OSstream::syncState().

Here is the call graph for this function:

◆ rewind()

virtual void rewind ( )
inlinevirtual

Rewind the stream, clearing any old errors.

Definition at line 409 of file OSpanStream.H.

References ospanstream::rewind(), OSpanStreamAllocator::stream_, and OSstream::syncState().

Here is the call graph for this function:

◆ print()

void print ( Ostream os) const
overridevirtual

Print stream description to Ostream.

Reimplemented from OSstream.

Definition at line 34 of file SpanStreams.C.

References Foam::endl(), and os().

Here is the call graph for this function:

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