TRIQS/h5 1.3.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#ifndef LIBH5_GENERIC_HPP
25#define LIBH5_GENERIC_HPP
26
27#include "./group.hpp"
28
29#include <string>
30#include <type_traits>
31
32namespace h5 {
33
50 template <typename T>
51 T h5_read(group g, std::string const &key) {
52 // default constructible
53 if constexpr (std::is_default_constructible_v<T>) {
54 T x{};
55 h5_read(g, key, x);
56 return x;
57 } else { // not default constructible
58 return T::h5_read_construct(g, key);
59 }
60 }
61
72 template <typename T>
73 T read(group g, std::string const &key) {
74 return h5_read<T>(g, key);
75 }
76
88 template <typename T>
89 void read(group g, std::string const &key, T &x, auto const &...args) {
90 h5_read(g, key, x, args...);
91 }
92
104 template <typename T>
105 void write(group g, std::string const &key, T const &x, auto const &...args) {
106 h5_write(g, key, x, args...);
107 }
108
119 template <typename T>
120 T h5_read_attribute(object obj, std::string const &name) {
121 T x;
122 h5_read_attribute(obj, name, x);
123 return x;
124 }
125
136 template <typename T>
137 T read_attribute(object obj, std::string const &key) {
138 return h5_read_attribute<T>(obj, key);
139 }
140
151 template <typename T>
152 void read_attribute(object obj, std::string const &key, T &x) {
153 h5_read_attribute(obj, key, x);
154 }
155
166 template <typename T>
167 void write_attribute(object obj, std::string const &key, T const &x) {
168 h5_write_attribute(obj, key, x);
169 }
170
183 template <typename T>
184 T h5_read_attribute_from_key(group g, std::string const &key, std::string const &name) {
185 T x;
186 h5_read_attribute_from_key(g, key, name, x);
187 return x;
188 }
189
202 template <typename T>
203 inline bool try_read(group g, std::string const &key, T &x) {
204 if (g.has_key(key)) {
205 h5_read(g, key, x);
206 return true;
207 }
208 return false;
209 }
210
213} // namespace h5
214
215#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:51
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:184
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:203
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:167
T read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:73
T read_attribute(object obj, std::string const &key)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:137
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
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:120
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
void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 attribute.
Definition scalar.hpp:125