TRIQS/h5
2.0.0
C++ interface to HDF5
Toggle main menu visibility
Loading...
Searching...
No Matches
variant.hpp
Go to the documentation of this file.
1
// Copyright (c) 2019-2024 Simons Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0.txt
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
21
22
#ifndef LIBH5_STL_VARIANT_HPP
23
#define LIBH5_STL_VARIANT_HPP
24
25
#include "
../format.hpp
"
26
#include "
../generic.hpp
"
27
#include "
../group.hpp
"
28
#include "
./string.hpp
"
29
30
#include <stdexcept>
31
#include <string>
32
#include <variant>
33
34
namespace
h5 {
35
40
42
template
<
typename
... T>
43
struct
hdf5_format_impl
<std::variant<T...>> {
44
static
std::string invoke() =
delete
;
45
};
46
57
template
<
typename
... Ts>
58
void
h5_write
(
group
g, std::string
const
&name, std::variant<Ts...>
const
&v) {
59
std::visit([&](
auto
const
&x) {
h5_write
(g, name, x); }, v);
60
}
61
62
namespace
detail {
63
64
// Helper function to read a `std::variant` from HDF5.
65
template
<
typename
VT,
typename
U,
typename
... Ts>
66
void
h5_read_variant_helper(VT &v,
datatype
dt, group g, std::string
const
&name) {
67
// finds the correct h5_read recursively
68
if
(
hdf5_type_equal
(
hdf5_type<U>
(), dt)) {
69
v = VT{
h5_read<U>
(g, name)};
70
return
;
71
}
72
if
constexpr
(
sizeof
...(Ts) > 0)
73
h5_read_variant_helper<VT, Ts...>(v, dt, g, name);
74
else
75
throw
std::runtime_error(
"Error in h5_read_variant_helper: Type stored in the variant has no corresponding HDF5 datatype"
);
76
}
77
78
}
// namespace detail
79
91
template
<
typename
... Ts>
92
void
h5_read
(
group
g, std::string
const
&name, std::variant<Ts...> &v) {
93
// name is a group --> triqs object
94
// assume for the moment, name is a dataset.
95
dataset
ds = g.
open_dataset
(name);
96
datatype
dt =
get_hdf5_type
(ds);
97
detail::h5_read_variant_helper<std::variant<Ts...>, Ts...>(v, dt, g, name);
98
}
99
101
102
}
// namespace h5
103
104
#endif
// LIBH5_STL_VARIANT_HPP
h5::group
A handle to an HDF5 group.
Definition
group.hpp:44
h5::group::open_dataset
dataset open_dataset(std::string const &key) const
Open a dataset with the given key in the group.
Definition
group.cpp:140
format.hpp
Provides utilities for reading and writing hdf5_format tags.
generic.hpp
Provides a generic interface for reading/writing data from/to various HDF5 objects.
group.hpp
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
h5::datatype
object datatype
Type alias for an HDF5 datatype.
Definition
object.hpp:123
h5::dataset
object dataset
Type alias for an HDF5 dataset.
Definition
object.hpp:120
h5::hdf5_type_equal
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
Definition
object.cpp:198
h5::hdf5_type
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
Definition
object.hpp:156
h5::get_hdf5_type
datatype get_hdf5_type(dataset ds)
Get the HDF5 type stored in a given h5::dataset.
Definition
object.cpp:196
h5::h5_read
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition
generic.hpp:53
h5::h5_write
void h5_write(group g, std::string const &name, T const &x)
Write a scalar to an HDF5 dataset.
Definition
scalar.hpp:71
string.hpp
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
h5::hdf5_format_impl
Default type trait to get the hdf5_format tag of type T by calling its static member function T::hdf5...
Definition
format.hpp:52
h5
stl
variant.hpp
Generated by
1.17.0