microlibrary

I/O Facilities

I/O facilities are defined in the microlibrary static library’s microlibrary/stream.h/microlibrary/stream.cc header/source file pair.

Table of Contents

  1. Device Error Handling
  2. Stream I/O Driver
  3. Stream Core
  4. Output Stream
    1. Output Formatters

Device Error Handling

Two different types of streams (normal and fault reporting) are supported that are differentiated by how device errors are handled. Normal streams (::microlibrary::Stream_IO_Driver, ::microlibrary::Stream, ::microlibrary::Output_Stream, and derived classes) cannot experience device errors, handle device errors internally, or call ::microlibrary::handle_assertion_failure() (via a precondition or postcondition check) if a device error occurs. Fault reporting streams (::microlibrary::Fault_Reporting_Stream_IO_Driver, ::microlibrary::Fault_Reporting_Stream, ::microlibrary::Fault_Reporting_Output_Stream, and derived classes) enter an error state when a device error occurs and report the device error to the caller.

Stream I/O Driver

The ::microlibrary::Stream_IO_Driver and ::microlibrary::Fault_Reporting_Stream_IO_Driver stream I/O driver classes define the standard interfaces that streams use to interact with devices. ::microlibrary::Stream_IO_Driver automated tests are defined in the test-automated-microlibrary-stream_io_driver automated test executable’s main.cc source file. ::microlibrary::Fault_Reporting_Stream_IO_Driver automated tests are defined in the test-automated-microlibrary-fault_reporting_stream_io_driver automated test executable’s main.cc source file.

The ::microlibrary::Testing::Automated::Mock_Stream_IO_Driver and ::microlibrary::Testing::Automated::Mock_Fault_Reporting_Stream_IO_Driver mock stream I/O driver classes are available if MICROLIBRARY_TARGET is DEVELOPMENT_ENVIRONMENT. The mocks are defined in the microlibrary/testing/automated/stream.h/microlibrary/testing/automated/stream.cc header/source file pair.

Stream Core

The ::microlibrary::Stream and ::microlibrary::Fault_Reporting_Stream stream core classes store a stream’s state information and associate a stream with a stream I/O driver. Stream state includes the following:

::microlibrary::Stream and ::microlibrary::Fault_Reporting_Stream support the following operations:

::microlibrary::Stream automated tests are defined in the test-automated-microlibrary-stream automated test executable’s main.cc source file. ::microlibrary::Fault_Reporting_Stream automated tests are defined in the test-automated-microlibrary-fault_reporting_stream automated test executable’s main.cc source file.

Output Stream

The ::microlibrary::Output_Stream and ::microlibrary::Fault_Reporting_Output_Stream output stream classes define stream unformatted and formatted output operations. The stream being nominal is a precondition for all output operations. ::microlibrary::Stream and ::microlibrary::Fault_Reporting_Stream support the following operations:

void foo( ::microlibrary::Output_Stream & stream, char const * string ) noexcept { // default constructed ::microlibrary::Output_Formatter<char const *> used stream.print( string );

// provided ::microlibrary::Output_Formatter<char const *> used
stream.print( string, ::microlibrary::Output_Formatter<char const *>{} ); } ```

::microlibrary::Output_Stream automated tests are defined in the test-automated-microlibrary-output_stream automated test executable’s main.cc source file. ::microlibrary::Fault_Reporting_Output_Stream automated tests are defined in the test-automated-microlibrary-fault_reporting_output_stream automated test executable’s main.cc source file.

The ::microlibrary::Testing::Automated::Mock_Output_Stream and ::microlibrary::Testing::Automated::Mock_Fault_Reporting_Output_Stream mock output stream classes are available if MICROLIBRARY_TARGET is DEVELOPMENT_ENVIRONMENT. The mocks are defined in the microlibrary/testing/automated/stream.h/microlibrary/testing/automated/stream.cc header/source file pair.

The ::microlibrary::Testing::Automated::Output_String_Stream and ::microlibrary::Testing::Automated::Fault_Reporting_Output_String_Stream output string stream classes are available if MICROLIBRARY_TARGET is DEVELOPMENT_ENVIRONMENT. The string streams are defined in the microlibrary/testing/automated/stream.h/microlibrary/testing/automated/stream.cc header/source file pair.

The ::microlibrary::Testing::Automated::Output_Vector_Stream and ::microlibrary::Testing::Automated::Fault_Reporting_Output_Vector_Stream output vector stream classes are available if MICROLIBRARY_TARGET is DEVELOPMENT_ENVIRONMENT. The vector streams are defined in the microlibrary/testing/automated/stream.h/microlibrary/testing/automated/stream.cc header/source file pair.

Output Formatters

The ::microlibrary::Output_Formatter template class must be fully or partially specialized for each type that will support formatted output. Specialization constructor parameters can be used to let the user configure formatting.

The following ::microlibrary::Output_Formatter specializations are defined in the microlibrary/stream.h/microlibrary/stream.cc header/source file pair:

The ::microlibrary::Output_Formatter<char> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<char> automated tests are defined in the test-automated-microlibrary-output_stream automated test executable’s main.cc source file and the test-automated-microlibrary-fault_reporting_output_stream automated test executable’s main.cc source file.

The ::microlibrary::Output_Formatter<char const *> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<char const *> automated tests are defined in the test-automated-microlibrary-output_stream automated test executable’s main.cc source file and the test-automated-microlibrary-fault_reporting_output_stream automated test executable’s main.cc source file.

The ::microlibrary::Output_Formatter<::microlibrary::ROM::String> specialization does not support user formatting configuration.

The ::microlibrary::Output_Formatter<::microlibrary::Error_Code> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<::microlibrary::Error_Code> automated tests are defined in the test-automated-microlibrary-output_stream automated test executable’s main.cc source file and the test-automated-microlibrary-fault_reporting_output_stream automated test executable’s main.cc source file.

#include "microlibrary/error.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    // output will be "::microlibrary::Generic_Error::LOGIC_ERROR"
    stream.print( ::microlibrary::Error_Code{ ::microlibrary::Generic_Error::LOGIC_ERROR } );
}

The ::microlibrary::Output_Formatter<Enum, std::enable_if_t<::microlibrary::is_error_code_enum_v<Enum>>> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<Enum, std::enable_if_t<::microlibrary::is_error_code_enum_v<Enum>>> automated tests are defined in the test-automated-microlibrary-output_stream automated test executable’s main.cc source file and the test-automated-microlibrary-fault_reporting_output_stream automated test executable’s main.cc source file.

#include "microlibrary/error.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    // output will be "::microlibrary::Generic_Error::LOGIC_ERROR"
    stream.print( ::microlibrary::Generic_Error::LOGIC_ERROR );
}

The following formatting helper types and ::microlibrary::Output_Formatter specializations are defined in the microlibrary/format.h/microlibrary/format.cc header/source file pair:

The ::microlibrary::Format::Bin class is used to print an integer type in binary. The ::microlibrary::Output_Formatter<::microlibrary::Format::Bin<Integer>> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<::microlibrary::Format::Bin<Integer>> automated tests are defined in the test-automated-microlibrary-format-bin automated test executable’s main.cc source file.

#include <cstdint>

#include "microlibrary/format.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    // output will be "0b01010101"
    stream.print( ::microlibrary::Format::Bin{ std::uint8_t{ 0b01010101 } } );
}

The ::microlibrary::Format::Dec class is used to print an integer type in decimal. The ::microlibrary::Output_Formatter<::microlibrary::Format::Dec<Integer>> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<::microlibrary::Format::Dec<Integer>> automated tests are defined in the test-automated-microlibrary-format-dec automated test executable’s main.cc source file.

#include <cstdint>

#include "microlibrary/format.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    // output will be "42"
    stream.print( ::microlibrary::Format::Dec{ std::int8_t{ 42 } } );

    // output will be "-42"
    stream.print( ::microlibrary::Format::Dec{ std::int8_t{ -42 } } );

    // output will be "42"
    stream.print( ::microlibrary::Format::Dec{ std::uint8_t{ 42 } } );
}

The ::microlibrary::Format::Hex class is used to print an integer type in hexadecimal. The ::microlibrary::Output_Formatter<::microlibrary::Format::Hex<Integer>> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<::microlibrary::Format::Hex<Integer>> automated tests are defined in the test-automated-microlibrary-format-hex automated test executable’s main.cc source file.

#include <cstdint>

#include "microlibrary/format.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    // output will be "0x0A"
    stream.print( ::microlibrary::Format::Hex{ std::uint8_t{ 0x0A } } );
}

The ::microlibrary::Format::Hex_Dump class is used to print a hex dump of data. The ::microlibrary::Output_Formatter<::microlibrary::Format::Hex_Dump<Address, Iterator>> specialization does not support user formatting configuration. ::microlibrary::Output_Formatter<::microlibrary::Format::Hex_Dump<Address, Iterator>> automated tests are defined in the test-automated-microlibrary-format-hex_dump automated test executable’s main.cc source file.

#include <cstdint>
#include <string_view>

#include "microlibrary/format.h"
#include "microlibrary/stream.h"

void foo( ::microlibrary::Output_Stream & stream ) noexcept
{
    auto const data = std::string_view{ "{yZZk7V!/{>fm[lxV!$e|:" };

    // output will be
    // "0400  7B 79 5A 5A 6B 37 56 21 2F 7B 3E 66 6D 5B 6C 78  |{yZZk7V!/{>fm[lx|\n"
    // "0410  56 21 24 65 7C 3A                                |V!$e|:|          \n"
    stream.print( ::picolibrary::Format::Hex_Dump{ std::uint16_t{ 0x0400 }, data.begin(), data.end() } );
}