TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
tuple.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_TUPLE_HPP
23#define LIBH5_STL_TUPLE_HPP
24
25#include "../format.hpp"
26#include "../group.hpp"
27#include "./string.hpp"
28
29#include <cstddef>
30#include <stdexcept>
31#include <string>
32#include <tuple>
33#include <utility>
34
35namespace h5 {
36
43 template <typename... T>
44 struct hdf5_format_impl<std::tuple<T...>> {
45 static std::string invoke() { return "PythonTupleWrap"; }
46 };
47
48 namespace detail {
49
50 // Helper function to write a tuple to HDF5.
51 template <typename... Ts, std::size_t... Is>
52 void h5_write_tuple_impl(group g, std::string const &, std::tuple<Ts...> const &tup, std::index_sequence<Is...>) {
53 (h5_write(g, std::to_string(Is), std::get<Is>(tup)), ...);
54 }
55
56 // Helper function to read a tuple from HDF5.
57 template <typename... Ts, std::size_t... Is>
58 void h5_read_tuple_impl(group g, std::string const &, std::tuple<Ts...> &tup, std::index_sequence<Is...>) {
59 if (g.get_all_subgroup_dataset_names().size() != sizeof...(Is))
60 throw std::runtime_error(
61 "Error in h5_read_tuple_impl: Reading a std::tuple<Ts...> from a group with more/less than sizeof...(Ts) subgroups/datasets is not allowed");
62 (h5_read(g, std::to_string(Is), std::get<Is>(tup)), ...);
63 }
64
65 } // namespace detail
66
77 template <typename... Ts>
78 void h5_write(group g, std::string const &name, std::tuple<Ts...> const &tup) {
79 auto gr = g.create_group(name);
80 write_hdf5_format(gr, tup);
81 detail::h5_write_tuple_impl(gr, name, tup, std::index_sequence_for<Ts...>{});
82 }
83
94 template <typename... Ts>
95 void h5_read(group g, std::string const &name, std::tuple<Ts...> &tup) {
96 auto gr = g.open_group(name);
97 detail::h5_read_tuple_impl(gr, name, tup, std::index_sequence_for<Ts...>{});
98 }
99
102} // namespace h5
103
104#endif // LIBH5_STL_TUPLE_HPP
A handle to an HDF5 group.
Definition group.hpp:44
group create_group(std::string const &key, bool delete_if_exists=true) const
Create a subgroup with the given key in the group.
Definition group.cpp:109
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
Definition group.cpp:96
Provides utilities for reading and writing hdf5_format tags.
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
void write_hdf5_format(object obj, T const &)
Write an hdf5_format tag for type T to an HDF5 attribute with the name 'Format' using template argume...
Definition format.hpp:113
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