fiber
Loading...
Searching...
No Matches
Output Streams and Formating

Overview

fiber::OStream provides a lightweight and flexible output streaming interface, optimized for embedded systems with limited resources. It offers:

  • Minimal FLASH memory usage compared to alternatives like std::ostream or {fmt}.
  • Formatted output for various types: strings, integers, floating points, booleans, pointers, and chrono durations.
  • Extendable and customizable formatting.

Getting Started

Basic example:

fiber::cout << "Hello World!" << fiber::endl;
void endl(OStream &stream)
Writes a new line character to the stream followed by a call to OStream::flush()
Definition OStream.hpp:198
OStreamRef cout
Definition OStream.cpp:20

Predefined default output streams:

These must be initialized by the user to point to a concrete OStream implementation. Creating a Custom Output Stream.

class MyCout : public OStream {/*...*/};
class MyCerr : public OStream {/*...*/};
MyCout my_cout;
MyCerr my_cerr;
int main(){
fiber::cout = my_cout;
fiber::cerr = my_cerr;
}
OStreamRef cerr
Definition OStream.cpp:21
int main()
Definition test_main.cpp:23

To create your own output stream (e.g., UART, USB), derive from fiber::OStream:

class MyStream : public fiber::OStream {
public:
void put(char c) final {
// Output one character
}
void flush() final {
// Flush buffer if necessary
}
void write(const char* str, size_t len) final {
// (Optional) Optimized multi-character output
}
};
Abstract class for an output character stream that offers string and number formating.
Definition OStream.hpp:125
virtual void write(const char *str, size_t len)
Writes a string to the stream.
Definition OStream.cpp:32
virtual void flush()=0
Overload this method to force flush the buffer.
virtual void put(char c)=0
Overload this method to write a character to the stream.

Mandatory methods to override:

  • void put(char c)
  • void flush()

Recommended for performance:

  • void write(const char* str, size_t len)

Optional:

  • void newl() define how to end a line (default: \n)
  • void endl() default: newline + flush

Formating and Streaming Capabilities

Stream the following types naturally with operator<<:

  • Characters and strings (char, const char*)
  • Booleans (bool) — configurable textual/numeric output
  • Integers and unsigned integers (all standard sizes + units)
  • Floating point numbers (float, double, long double)
  • Pointers and nullptr

Example:

fiber::cout << "Answer: " << 42 << fiber::newl;
fiber::cout << "Result: " << true << fiber::endl;
fiber::cout << "Address: " << myPtr << fiber::endl;
void newl(OStream &stream)
Writes a new line character to the stream.
Definition OStream.hpp:183

Special Stream Functions

You can use the following functions inside streaming expressions:

  • fiber::newl(stream): Write newline (\n) - may be overridden by the user by overrideing OStream::newl()
  • fiber::flush(stream): Flush the stream
  • fiber::endl(stream): Write newline and flush

String Formatting (FormatStr)

Format strings with padding, minimum width, alignment:

fiber::cout << "[" << fiber::FormatStr("OK").mwidth(6).left() << "]" << fiber::endl;
// Output: [OK ]
Formats a string and allows to pass an additional size parameter.
Definition OStream.hpp:261
constexpr FormatStr & left()
Formats the string to the left area set by mwidth().
Definition OStream.hpp:399
constexpr FormatStr & mwidth(int mw)
Sets the minimum number of character that will be put into the stream.
Definition OStream.hpp:387

All following formating types use and provide the functionalities of fiber::FormatStr.

See also
fiber::FormatStr

Boolean Formatting (FormatBool)

Configure booleans to print as true/false or 1/0:

fiber::cout << FormatBool(true).text() << fiber::endl; // "true"
fiber::cout << FormatBool(false).num() << fiber::endl; // "0"
See also
fiber::FormatBool

Integer Formatting (FormatInt)

Enhanced integer output, with thousands, alignment, and sign padding

fiber::cout << FormatInt(123456).use_thousands().thousands('.') << fiber::endl; // "123.456"
fiber::cout << FormatInt(42).fsign().mwidth(6) << fiber::endl; // " +42"
See also
fiber::FormatInt

Integer with Suffix (FormatIntSuffix)

Allows to add a suffix to integers that will be aligned with the integer.

fiber::cout << FormatIntSuffix(64, "kB").left().mwidth(6) << fiber::endl; // "64kB "
See also
FormatIntSuffix

Floating-Point Formatting (FormatFloat)

Flexible floating-point output in scientific, engineering and full formating:

fiber::cout << FormatFloat(3.14f).decimals(2).full() << fiber::endl; // "3.14"
fiber::cout << FormatFloat(12345.6).sci() << fiber::endl; // "1.2356e4"
fiber::cout << FormatFloat(12345.6).eng() << fiber::endl; // "12.356e3"
See also
fiber::FormatFloat

Hexadecimal Formatting (FormatHex)

Custom FormatHex options allows including or excluding the hex header 0x as well as upper/lower case and optional leading zeros or compact notation.

int a;
fiber::cout << FormatHex(&a).upper << fiber::endl; // possible output: 0x21F56D
See also
fiber::FormatHex

Note: Pointers are automatically formatted as hexadecimal.

Chrono Durations

Using format_chrono(duration) you can create an fiber::FormatIntSuffix from std::chrono::duration types with automatically appropriate units (determined at compile time).

See also
fiber::format_chrono
fiber::FormatIntSuffix

Customization with Compiler Flags

You can customize the formatting behavior at compile time by defining multiple compiler macros or CMake options.

For a complete overview of available compile options and their effect on formatting, refer to the page: Compile Options & Flags, specifically section Formatting