Centipede 0.0.1
Centipede program
Loading...
Searching...
No Matches
centipede::writer::Binary Class Reference

Class for writing binary files. More...

#include <binary.hpp>

Classes

class  Config
 Class for configuring the binary writer class (Binary). More...

Public Types

using BufferType = std::pair<std::vector<uint32_t>, std::vector<float>>
 Type of the data_buffer_.
using BufferPoint = std::pair<uint32_t, float>
 Type of the buffer point stored in the data_buffer_.

Public Member Functions

 Binary ()=default
 Default constructor.
constexpr Binary (Config config)
 Constructor takes an argument for the configuration.
auto init () -> EnumError<>
 Initialization.
template<std::size_t NLocals, std::size_t NGlobals>
auto add_entrypoint (const EntryPoint< NLocals, NGlobals > &entry_point) -> EnumError<>
 Add an entrypoint to the internal data buffer.
auto write_current_entry () -> EnumError< std::size_t >
 Streaming an entry data to the output file.
void close ()
 Manually close the output file handler.
constexpr auto get_config () const -> const Config &
 Getter of the configuration.
constexpr auto get_buffer () const -> const BufferType &
 Getter of the buffer.

Private Attributes

Config config_
 Member variable for the configuration.
BufferType data_buffer_
 Data buffer to store entry_point.
std::ofstream output_file_
 Output file handler.

Detailed Description

Data is written to the binary file via each entry, which contains different entrypoints (of the type centipede::EntryPoint). Before any operation, the Binary::init() function must be called, where the file handler is opened and internal buffer resetted, ready for the next data input. When adding each entrypoint, the writer doesn't write the corresponding data to the binary file, but rather pushes the data to its internal buffer (see Binary::data_buffer_). Data is only written to the binary file after calling Binary::write_current_entry(). All entrypoints added before this call are grouped into the same entry.

Configuration of the class is done via the Binary::Config struct.

Example usage

#include <centipede/centipede.hpp>
#include <print>
auto init_err = writer.init()
if(not init_err.has_value())
{
// Prints the error message here.
std::println(stderr, "Error: {}", init_err.error());
}
// Starts to write entrypoint
auto entry_point = get_new_entry_point();
auto add_err = writer.add_entrypoint(entry_point);
// Deal with add_err here.
if(not add_err.has_value()) { ... }
// Write current entry to the file.
auto write_err = writer.write_current_entry();
// Deal with write_err here.
if(not write_err.has_value()) { ... }
Class for writing binary files.
Definition binary.hpp:63

For a complete example, please check out test_writer.cpp.

Definition at line 62 of file binary.hpp.

Constructor & Destructor Documentation

◆ Binary()

centipede::writer::Binary::Binary ( Config config)
inlineexplicitconstexpr

The config argument will be moved (std::move) to its member variable config_

Parameters
configConfiguration struct.
See also
Config

Definition at line 100 of file binary.hpp.

Member Function Documentation

◆ add_entrypoint()

template<std::size_t NLocals, std::size_t NGlobals>
auto centipede::writer::Binary::add_entrypoint ( const EntryPoint< NLocals, NGlobals > & entry_point) -> EnumError<>
nodiscard

The adding of an entrypoint to the data buffer follows the sequence:

  1. If the sigma value is negative, returns immediately.
  2. Check if the current internal buffer has enough of space to store the latest data point.
  3. Add an index-value pair [0, measurement] as one buffer point to the data buffer.
  4. Add all local derivatives with indices as buffer points to the data buffer.
  5. Add an index-value pair [0, sigma] as one buffer point to the data buffer.
  6. Add all global derivatives with indices as buffer points to the data buffer.
Parameters
entry_pointInput data structure which stores the local/global derivatives, measurement and error.
Returns

Definition at line 183 of file binary.hpp.

◆ close()

void centipede::writer::Binary::close ( )
inline

This function will be called automatically when the destructor is called.

Definition at line 151 of file binary.hpp.

◆ get_buffer()

auto centipede::writer::Binary::get_buffer ( ) const -> const BufferType &
inlineconstexpr
Returns
Returns a const reference to the member variable data_buffer_.
See also
ref

Definition at line 167 of file binary.hpp.

◆ get_config()

auto centipede::writer::Binary::get_config ( ) const -> const Config &
inlineconstexpr
Returns
Returns a const reference to the member variable config_.
See also
ref

Definition at line 159 of file binary.hpp.

◆ init()

auto centipede::writer::Binary::init ( ) -> EnumError<>
nodiscard

The initialization function must be called before calling the add_entrypoint() method. When calling this function, data_buffer_ is resetted and a file is opened with the specified name in Config.

Returns
Returns ErrorCode::writer_file_fail_to_open if the file cannot be opened with the name specified by Config::out_filename.
See also
Config

Definition at line 50 of file binary.cpp.

◆ write_current_entry()

auto centipede::writer::Binary::write_current_entry ( ) -> EnumError< std::size_t >

Streaming the entry data to the output file and call the reset() function to clear the internal buffer. After the function is called, the writer is waiting for a new entry to be added.

Returns
Number of bytes written to the binary file.

Definition at line 63 of file binary.cpp.


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