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

Detailed Description

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

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

#include <h5/h5.hpp>
#include <iostream>
#include <variant>
#include <string>
int main() {
// create file in read/write mode
h5::file file("variant.h5", 'w');
// write variant
h5::write(file, "myvariant", std::variant<double, std::string> { "Hello, World!" });
// read into variant
std::variant<double, std::string> var;
h5::read(file, "myvariant", var);
// output variant
std::visit([](const auto& x) { std::cout << x << std::endl; }, var);
}
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:

Hello, World!

Contents of variant.h5:

HDF5 "variant.h5" {
GROUP "/" {
DATASET "myvariant" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
DATA {
(0): "Hello, World!"
}
}
}
}

Classes

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

Functions

template<typename... Ts>
void h5::h5_read (group g, std::string const &name, std::variant< Ts... > &v)
 Read a std::variant from an HDF5 dataset.
 
template<typename... Ts>
void h5::h5_write (group g, std::string const &name, std::variant< Ts... > const &v)
 Write a std::variant to an HDF5 dataset/subgroup.
 

Function Documentation

◆ h5_read()

template<typename... Ts>
void h5::h5_read ( group g,
std::string const & name,
std::variant< Ts... > & v )

#include <h5/stl/variant.hpp>

Read a std::variant from an HDF5 dataset.

Warning
This function only works, if name points to a dataset and not a group. Depending on the HDF5 datatype of the dataset, it calls the specialized h5_read.
Template Parameters
TsVariant types.
Parameters
gh5::group containing the dataset.
nameName of the dataset from which the std::variant is read.
vstd::variant to read into.

Definition at line 92 of file variant.hpp.

◆ h5_write()

template<typename... Ts>
void h5::h5_write ( group g,
std::string const & name,
std::variant< Ts... > const & v )

#include <h5/stl/variant.hpp>

Write a std::variant to an HDF5 dataset/subgroup.

Calls the specialized h5_write for the type currently stored in the std::variant.

Template Parameters
TsVariant types.
Parameters
gh5::group in which the dataset/subgroup is created.
nameName of the dataset/subgroup to which the std::variant is written.
vstd::variant to be written.

Definition at line 58 of file variant.hpp.