TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches

Detailed Description

Specialized functions to read/write a std::optional from/to HDF5.

The following code writes a std::optional to an HDF5 file, reads the same value and outputs it to stdout:

#include <h5/h5.hpp>
#include <iostream>
#include <optional>
int main() {
// create file in read/write mode
h5::file file("optional.h5", 'w');
// write optional
h5::write(file, "myoptional", std::optional<float> { 3.1415f });
// read into optional
std::optional<float> val;
h5::read(file, "myoptional", val);
// output optional value
std::cout << *val << std::endl;
}
A handle to an HDF5 file.
Definition file.hpp:43
T read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:73
void write(group g, std::string const &key, T const &x, auto const &...args)
Generic implementation for writing a variable to an HDF5 dataset/subgroup.
Definition generic.hpp:105
Includes all relevant h5 headers.

Output:

3.1415

Contents of optional.h5:

HDF5 "optional.h5" {
GROUP "/" {
DATASET "myoptional" {
DATATYPE H5T_IEEE_F32LE
DATASPACE SCALAR
DATA {
(0): 3.1415
}
}
}
}

Classes

struct  h5::hdf5_format_impl< std::optional< T > >
 Specialization of h5::hdf5_format_impl for std::optional. More...
 

Functions

template<typename T >
void h5::h5_read (group g, std::string name, std::optional< T > &opt)
 Read a std::optional from an HDF5 dataset/subgroup.
 
template<typename T >
void h5::h5_write (group g, std::string const &name, std::optional< T > const &opt)
 Write a std::optional to an HDF5 dataset/subgroup (if it is set).
 

Function Documentation

◆ h5_read()

template<typename T >
void h5::h5_read ( group g,
std::string name,
std::optional< T > & opt )

#include <h5/stl/optional.hpp>

Read a std::optional from an HDF5 dataset/subgroup.

Calls the specialized h5_read function for the value type of the std::optional.

Template Parameters
TValue type of std::optional.
Parameters
gh5::group containing the dataset/subgroup.
nameName of the dataset/subgroup from which the std::optional value is read.
optstd::optional to read into.

Definition at line 71 of file optional.hpp.

◆ h5_write()

template<typename T >
void h5::h5_write ( group g,
std::string const & name,
std::optional< T > const & opt )

#include <h5/stl/optional.hpp>

Write a std::optional to an HDF5 dataset/subgroup (if it is set).

Calls the specialized h5_write function for the value type of the std::optional.

Template Parameters
TValue type of std::optional.
Parameters
gh5::group in which the dataset/subgroup is created.
nameName of the dataset/subgroup to which the std::optional value is written.
optstd::optional to be written.

Definition at line 56 of file optional.hpp.