TRIQS/h5 2.0.0
C++ interface to HDF5
Loading...
Searching...
No Matches
generic.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
24
25#ifndef LIBH5_GENERIC_HPP
26#define LIBH5_GENERIC_HPP
27
28#include "./group.hpp"
29
30#include <string>
31#include <type_traits>
32
33namespace h5 {
34
39
52 template <typename T>
53 T h5_read(group g, std::string const &key) {
54 // default constructible
55 if constexpr (std::is_default_constructible_v<T>) {
56 T x{};
57 h5_read(g, key, x);
58 return x;
59 } else { // not default constructible
60 return T::h5_read_construct(g, key);
61 }
62 }
63
74 template <typename T>
75 T read(group g, std::string const &key) {
76 return h5_read<T>(g, key);
77 }
78
90 template <typename T>
91 void read(group g, std::string const &key, T &x, auto const &...args) {
92 h5_read(g, key, x, args...);
93 }
94
107 template <typename T>
108 void write(group g, std::string const &key, T const &x, auto const &...args) {
109 h5_write(g, key, x, args...);
110 }
111
124 template <typename T>
125 T h5_read_attribute(object obj, std::string const &name) {
126 T x;
127 h5_read_attribute(obj, name, x);
128 return x;
129 }
130
141 template <typename T>
142 T read_attribute(object obj, std::string const &key) {
143 return h5_read_attribute<T>(obj, key);
144 }
145
156 template <typename T>
157 void read_attribute(object obj, std::string const &key, T &x) {
158 h5_read_attribute(obj, key, x);
159 }
160
171 template <typename T>
172 void write_attribute(object obj, std::string const &key, T const &x) {
173 h5_write_attribute(obj, key, x);
174 }
175
190 template <typename T>
191 T h5_read_attribute_from_key(group g, std::string const &key, std::string const &name) {
192 T x;
193 h5_read_attribute_from_key(g, key, name, x);
194 return x;
195 }
196
210 template <typename T>
211 inline bool try_read(group g, std::string const &key, T &x) {
212 if (g.has_key(key)) {
213 h5_read(g, key, x);
214 return true;
215 }
216 return false;
217 }
218
220
221} // namespace h5
222
223#endif // LIBH5_GENERIC_HPP
A handle to an HDF5 group.
Definition group.hpp:44
bool has_key(std::string const &key) const
Check if a link with the given key exists in the group.
Definition group.cpp:53
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:53
T h5_read_attribute_from_key(group g, std::string const &key, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:191
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
void write_attribute(object obj, std::string const &key, T const &x)
Generic implementation for writing a variable to an HDF5 attribute.
Definition generic.hpp:172
T read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:75
T read_attribute(object obj, std::string const &key)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:142
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:108
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