FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
streams::ostream Class Reference

Output stream wrapper to provide formatted output API, a la C++. More...

#include <fastarduino/streams.h>

Inheritance diagram for streams::ostream:
Collaboration diagram for streams::ostream:

Public Types

using MANIPULATOR = void(*)(ostream &)
 General type of a manipulator function applicable to this output stream.
 
- Public Types inherited from streams::ios_base
using iostate = uint8_t
 Bitmask type to represent stream state flags. More...
 
using fmtflags = uint16_t
 Bitmask type to represent stream format flags. More...
 

Public Member Functions

 ostream (ostreambuf &streambuf)
 Construct a formatted output wrapper of streambuf. More...
 
ostreamoperator= (const ostream &)=delete
 
ostreambufrdbuf () const
 Return the stream buffer associated with this stream.
 
void flush ()
 Flush this ostream and blocks until all its buffer has been written to the underlying device. More...
 
void put (char c)
 Insert character c into this stream. More...
 
void write (const char *content, size_t size)
 Write a block of data to this stream. More...
 
void write (const char *str)
 Write a string (null-terminated) to this stream. More...
 
void write (const flash::FlashStorage *str)
 Write a flash-stored string (null-terminated) to this stream. More...
 
ostreamoperator<< (const void *ptr)
 Output the address of a pointer. More...
 
ostreamoperator<< (bool value)
 Output a boolean value. More...
 
ostreamoperator<< (char ch)
 Output a single character. More...
 
ostreamoperator<< (const char *str)
 Output a C-string (\0 terminated). More...
 
ostreamoperator<< (const flash::FlashStorage *str)
 Output a C-string (\0 terminated) that is stored in flash memory. More...
 
ostreamoperator<< (int value)
 Output a signed integral number, represented within the current base(), using the current minimum width(). More...
 
ostreamoperator<< (unsigned int value)
 Output an unsigned integral number, represented within the current base(), using the current minimum width(). More...
 
ostreamoperator<< (long value)
 Output a signed long integral number, represented within the current base(), using the current minimum width(). More...
 
ostreamoperator<< (unsigned long value)
 Output an unsigned long integral number, represented within the current base(), using the current minimum width(). More...
 
ostreamoperator<< (double value)
 Output a floating point number, using the current minimum width() and precision(). More...
 
ostreamoperator<< (MANIPULATOR func)
 Apply a MANIPULATOR to this output stream. More...
 
- Public Member Functions inherited from streams::ios_base
iostate rdstate () const
 Return the current stream error state.
 
void setstate (iostate state)
 Set the stream error flags state in addition to currently set flags. More...
 
void clear (iostate state=goodbit)
 Set the stream error state flags by assigning them the value of state. More...
 
bool good () const
 
bool eof () const
 Return true if the associated stream has reached end-of-file. More...
 
bool fail () const
 Return true if an error has occurred on the associated stream, since last time state was reset (clear() was called). More...
 
bool bad () const
 Return true if a non-recoverable error has occurred on the associated stream. More...
 
bool operator! () const
 Return true if an error has occurred on the associated stream, since last time state was reset (clear() was called). More...
 
 operator bool () const
 Check that the current stream has no errors. More...
 
void flags (fmtflags flags)
 Set new format flags for this stream. More...
 
fmtflags flags () const
 Return the format flags currently selected in this stream. More...
 
void setf (fmtflags flags)
 Set this stream's format flags whose bits are set in flags, leaving unchanged the rest. More...
 
void setf (fmtflags flags, fmtflags mask)
 Set this stream's format flags whose bits are set in both flags and mask, and clears the format flags whose bits are set in mask but not in flags. More...
 
void unsetf (fmtflags flags)
 Clear this stream's format flags whose bits are set in flags. More...
 
char fill () const
 Return the fill character. More...
 
void fill (char fill)
 Set fill as new fill character for this stream. More...
 
void width (uint8_t width)
 Set minimum width used for displaying values. More...
 
uint8_t width () const
 Get the current minimum width value (default = 0) used for formatted output. More...
 
void precision (uint8_t precision)
 Set precision (number of digits after decimal point) used for displaying floating values. More...
 
uint8_t precision () const
 Get the current precision (default = 6) used for formatted floating values output. More...
 
ios_basecopyfmt (const ios_base &rhs)
 Copy formatting information from rhs to this stream. More...
 

Additional Inherited Members

- Static Public Attributes inherited from streams::ios_base
static constexpr iostate eofbit = 0x01
 This bit is set if the stream has unexpectedly reached its end during an extraction. More...
 
static constexpr iostate failbit = 0x02
 This bit is set when an input or operation failed due to a formatting error during extraction.
 
static constexpr iostate badbit = 0x04
 This bit is set when an irrecoverable stream error has occurred, e.g. More...
 
static constexpr iostate goodbit = 0
 No error; always 0.
 
static constexpr fmtflags dec = 0x0001
 Read or write integral values using decimal (0..9) base format.
 
static constexpr fmtflags bin = 0x0002
 Read or write integral values using binary (0,1) base format.
 
static constexpr fmtflags oct = 0x0004
 Read or write integral values using octal (0..7) base format.
 
static constexpr fmtflags hex = 0x0008
 Read or write integral values using hexadecimal (0..9,A..F) base format.
 
static constexpr fmtflags basefield = dec | bin | oct | hex
 Bitmask constant used with setf(fmtflags, fmtflags) when changing the output base format. More...
 
static constexpr fmtflags left = 0x0010
 Pad all output to width() characters, with fill() character appended at the end so that the output appears left-adjusted.
 
static constexpr fmtflags right = 0x0020
 Pad all output to width() characters, with fill() character added at the beginning so that the output appears right-adjusted.
 
static constexpr fmtflags adjustfield = left | right
 Bitmask constant used with setf(fmtflags, fmtflags) when changing the output adjustment. More...
 
static constexpr fmtflags scientific = 0x0040
 Write floating point values in fixed-point notation.
 
static constexpr fmtflags fixed = 0x0080
 Write floating point values in scientific notation.
 
static constexpr fmtflags floatfield = scientific | fixed
 Bitmask constant used with setf(fmtflags, fmtflags) when changing the floating point output representation. More...
 
static constexpr fmtflags boolalpha = 0x0200
 Read or write bool values as alphabetic string (true or false).
 
static constexpr fmtflags showbase = 0x0400
 Write integral values prefixed by their base: More...
 
static constexpr fmtflags showpos = 0x1000
 Write non-negative numerical values preceded by +.
 
static constexpr fmtflags skipws = 0x2000
 Skip leading spaces on certain extraction (read) operations.
 
static constexpr fmtflags unitbuf = 0x4000
 Flush output after each insertion operation.
 
static constexpr fmtflags uppercase = 0x8000U
 Write uppercase letters instead of lowercase in certain insertion operations. More...
 
static constexpr uint8_t MAX_PRECISION = 16
 The maximum allowed precision. More...
 

Detailed Description

Output stream wrapper to provide formatted output API, a la C++.

Definition at line 60 of file streams.h.

Constructor & Destructor Documentation

◆ ostream()

streams::ostream::ostream ( ostreambuf streambuf)
inlineexplicit

Construct a formatted output wrapper of streambuf.

Parameters
streambufthe output streambuf to be wrapped

Definition at line 67 of file streams.h.

Member Function Documentation

◆ flush()

void streams::ostream::flush ( )
inline

Flush this ostream and blocks until all its buffer has been written to the underlying device.

A manipulator exists with the same name and behavior.

See also
ostreambuf::pubsync()
streams::flush()

Definition at line 86 of file streams.h.

◆ put()

void streams::ostream::put ( char  c)
inline

Insert character c into this stream.

The character is buffered by the underlying ostreambuf and transmitted to the connected device when possible. If the underlying ostreambuf overflows, then badbit is set for this stream.

See also
ostreambuf::sputc(char)

Definition at line 98 of file streams.h.

◆ write() [1/3]

void streams::ostream::write ( const char *  content,
size_t  size 
)
inline

Write a block of data to this stream.

content gets buffered by the underlying ostreambuf and transmitted to the connected device when possible. If the underlying ostreambuf overflows, then badbit is set for this stream.

Parameters
contentdata to be transmitted
sizethe number of bytes from content to be transmitted
See also
ostreambuf::sputn(const char*, size_t)

Definition at line 113 of file streams.h.

◆ write() [2/3]

void streams::ostream::write ( const char *  str)
inline

Write a string (null-terminated) to this stream.

str gets buffered by the underlying ostreambuf and transmitted to the connected device when possible. The terminating \0 is not transmitted. If the underlying ostreambuf overflows, then badbit is set for this stream.

Parameters
strthe string to be transmitted
See also
ostreambuf::sputn(const char*)

Definition at line 127 of file streams.h.

◆ write() [3/3]

void streams::ostream::write ( const flash::FlashStorage *  str)
inline

Write a flash-stored string (null-terminated) to this stream.

str gets buffered by the underlying ostreambuf and transmitted to the connected device when possible. The terminating \0 is not transmitted. If the underlying ostreambuf overflows, then badbit is set for this stream.

Parameters
strthe string to be transmitted
See also
ostreambuf::sputn(const flash::FlashStorage*)

Definition at line 141 of file streams.h.

◆ operator<<() [1/11]

ostream& streams::ostream::operator<< ( const void *  ptr)
inline

Output the address of a pointer.

int i = 0;
int* p = &i;
out << p;
Parameters
ptrthe pointer which address to output
Returns
this formatted output

Definition at line 157 of file streams.h.

◆ operator<<() [2/11]

ostream& streams::ostream::operator<< ( bool  value)
inline

Output a boolean value.

out << true;
Parameters
valuethe bool to output
Returns
this formatted output

Definition at line 172 of file streams.h.

◆ operator<<() [3/11]

ostream& streams::ostream::operator<< ( char  ch)
inline

Output a single character.

out << '\n';
Parameters
chthe character to output
Returns
this formatted output

Definition at line 187 of file streams.h.

◆ operator<<() [4/11]

ostream& streams::ostream::operator<< ( const char *  str)
inline

Output a C-string (\0 terminated).

out << "Hello, Worlds!\n";
Parameters
strthe string to output
Returns
this formatted output

Definition at line 202 of file streams.h.

◆ operator<<() [5/11]

ostream& streams::ostream::operator<< ( const flash::FlashStorage *  str)
inline

Output a C-string (\0 terminated) that is stored in flash memory.

out << F("Hello, Worlds!\n");
Parameters
strthe string to output
Returns
this formatted output

Definition at line 217 of file streams.h.

◆ operator<<() [6/11]

ostream& streams::ostream::operator<< ( int  value)
inline

Output a signed integral number, represented within the current base(), using the current minimum width().

int x = -123;
out << x;
Parameters
valuethe number to output
Returns
this formatted output

Definition at line 234 of file streams.h.

◆ operator<<() [7/11]

ostream& streams::ostream::operator<< ( unsigned int  value)
inline

Output an unsigned integral number, represented within the current base(), using the current minimum width().

unsigned int x = 64000;
out << x;
Parameters
valuethe number to output
Returns
this formatted output

Definition at line 251 of file streams.h.

◆ operator<<() [8/11]

ostream& streams::ostream::operator<< ( long  value)
inline

Output a signed long integral number, represented within the current base(), using the current minimum width().

long x = -999999L;
out << x;
Parameters
valuethe number to output
Returns
this formatted output

Definition at line 268 of file streams.h.

◆ operator<<() [9/11]

ostream& streams::ostream::operator<< ( unsigned long  value)
inline

Output an unsigned long integral number, represented within the current base(), using the current minimum width().

unsigned long x = 999999UL;
out << x;
Parameters
valuethe number to output
Returns
this formatted output

Definition at line 285 of file streams.h.

◆ operator<<() [10/11]

ostream& streams::ostream::operator<< ( double  value)
inline

Output a floating point number, using the current minimum width() and precision().

double x = 123.456;
out << x;
Parameters
valuethe number to output
Returns
this formatted output

Definition at line 302 of file streams.h.

◆ operator<<() [11/11]

ostream& streams::ostream::operator<< ( MANIPULATOR  func)
inline

Apply a MANIPULATOR to this output stream.

A manipulator may:

  • change formatting option (base, width, precision)
  • call some method of this output stream
out << hex << 16384 << endl << flush;
Parameters
functhe manipulator to apply to this output stream
Returns
this formatted output

Definition at line 330 of file streams.h.


The documentation for this class was generated from the following file:
streams::endl
void endl(FSTREAM &stream)
Manipulator for an output stream, which will insert a new-line character and flush the stream buffer.
Definition: streams.h:722
streams::hex
void hex(FSTREAM &stream)
Manipulator for an output or input stream, which will set the base, used to represent (output) or int...
Definition: ios.h:774
streams::ostream::flush
void flush()
Flush this ostream and blocks until all its buffer has been written to the underlying device.
Definition: streams.h:86
F
#define F(ptr)
Force string constant to be stored as flash storage.
Definition: flash.h:98
streams::ios_base::hex
static constexpr fmtflags hex
Read or write integral values using hexadecimal (0..9,A..F) base format.
Definition: ios.h:218
streams::flush
void flush(FSTREAM &stream)
Manipulator for an output stream, which will flush the stream buffer.
Definition: streams.h:713