TRIQS/h5 2.0.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
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 <type_traits>
33#include <variant>
34
35namespace h5 {
36
41
43 template <typename... T>
44 struct hdf5_format_impl<std::variant<T...>> {
45 static std::string invoke() = delete;
46 };
47
58 template <typename... Ts>
59 void h5_write(group g, std::string const &name, std::variant<Ts...> const &v) {
60 std::visit([&](auto const &x) { h5_write(g, name, x); }, v);
61 }
62
74 template <typename T, typename... Ts>
75 void h5_read(group g, std::string const &name, std::variant<T, Ts...> &v) {
76 dataset ds = g.open_dataset(name);
77 datatype dt = get_hdf5_type(ds);
78 auto try_read = [&]<typename U>(std::type_identity<U>) {
79 if (!hdf5_type_equal(hdf5_type<U>(), dt)) return false;
80 v = h5_read<U>(g, name);
81 return true;
82 };
83 if ((try_read(std::type_identity<T>{}) || ... || try_read(std::type_identity<Ts>{}))) return;
84 throw std::runtime_error("Error in h5_read for std::variant: stored HDF5 datatype matches no variant alternative (allowed: "
85 + (get_name_of_h5_type<T>() + ... + (", " + get_name_of_h5_type<Ts>())) + ")");
86 }
87
98 template <typename... Ts>
99 void h5_write_attribute(object obj, std::string const &name, std::variant<Ts...> const &v) {
100 std::visit([&](auto const &x) { h5_write_attribute(obj, name, x); }, v);
101 }
102
113 template <typename T, typename... Ts>
114 void h5_read_attribute(object obj, std::string const &name, std::variant<T, Ts...> &v) {
115 datatype dt = get_hdf5_attribute_type(obj, name);
116 auto try_read = [&]<typename U>(std::type_identity<U>) {
117 if (!hdf5_type_equal(hdf5_type<U>(), dt)) return false;
118 v = h5_read_attribute<U>(obj, name);
119 return true;
120 };
121 if ((try_read(std::type_identity<T>{}) || ... || try_read(std::type_identity<Ts>{}))) return;
122 throw std::runtime_error("Error in h5_read_attribute for std::variant: stored HDF5 datatype matches no variant alternative (allowed: "
123 + (get_name_of_h5_type<T>() + ... + (", " + get_name_of_h5_type<Ts>())) + ")");
124 }
125
127
128} // namespace h5
129
130#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
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
object dataset
Type alias for an HDF5 dataset.
Definition object.hpp:120
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
Definition object.cpp:204
datatype get_hdf5_attribute_type(object obj, std::string const &name)
Get the HDF5 type of a named attribute attached to a given h5::object.
Definition object.cpp:199
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:197
std::string get_name_of_h5_type(datatype dt)
Get the name of an h5::datatype (for error messages).
Definition object.cpp:184
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:53
bool try_read(group g, std::string const &key, T &x)
Check if an HDF5 dataset/subgroup with the given key exists in the given parent group before performi...
Definition generic.hpp:211
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:125
void h5_write(group g, std::string const &name, T const &x)
Write a scalar to an HDF5 dataset.
Definition scalar.hpp:71
void h5_write_attribute(object, std::string const &, std::string const &)
Write a std::string to an HDF5 attribute.
Definition string.cpp:99
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 format.hpp:52