TRIQS/h5 1.3.0
C++ interface to HDF5
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
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
34namespace h5 {
35
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
102} // namespace h5
103
104#endif // LIBH5_STL_VARIANT_HPP
A handle to an HDF5 group.
Definition group.hpp:44
dataset open_dataset(std::string const &key) const
Open a dataset with the given key in the group.
Definition group.cpp:140
A generic handle for HDF5 objects.
Definition object.hpp:49
Provides utilities for reading and writing hdf5_format tags.
Provides a generic interface for reading/writing data from/to various HDF5 objects.
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
object datatype
Type alias for an HDF5 datatype.
Definition object.hpp:123
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
Definition object.cpp:198
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
Definition object.hpp:156
datatype get_hdf5_type(dataset ds)
Get the HDF5 type stored in a given h5::dataset.
Definition object.cpp:196
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:51
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
Definition scalar.hpp:70
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
Default type trait to get the hdf5_format tag of type T by calling its static member function T::hdf5...
Definition string.hpp:34