TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
format.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
26
27#ifndef LIBH5_FORMAT_HPP
28#define LIBH5_FORMAT_HPP
29
30#include "./macros.hpp"
31#include "./group.hpp"
32#include "./stl/string.hpp"
33
34#include <complex>
35#include <string>
36
37namespace h5 {
38
43
50 template <typename T>
52 static std::string invoke() { return T::hdf5_format(); }
53 };
54
55#define H5_SPECIALIZE_FORMAT2(X, Y) \
56 \
57 template <> \
58 struct hdf5_format_impl<X> { \
59 static std::string invoke() { return H5_AS_STRING(Y); } \
60 }
61
62#define H5_SPECIALIZE_FORMAT(X) H5_SPECIALIZE_FORMAT2(X, X)
63
64 H5_SPECIALIZE_FORMAT(bool);
65 H5_SPECIALIZE_FORMAT(int);
66 H5_SPECIALIZE_FORMAT(long);
67 H5_SPECIALIZE_FORMAT(long long);
68 H5_SPECIALIZE_FORMAT(unsigned int);
69 H5_SPECIALIZE_FORMAT(unsigned long);
70 H5_SPECIALIZE_FORMAT(unsigned long long);
71 H5_SPECIALIZE_FORMAT(float);
72 H5_SPECIALIZE_FORMAT(double);
73 H5_SPECIALIZE_FORMAT(long double);
74 H5_SPECIALIZE_FORMAT2(std::complex<double>, complex);
75
82 template <typename T>
83 std::string get_hdf5_format() {
84 return hdf5_format_impl<T>::invoke();
85 }
86
94 template <typename T>
95 std::string get_hdf5_format([[maybe_unused]] T const &t) {
96 return hdf5_format_impl<T>::invoke();
97 }
98
105 inline void write_hdf5_format_as_string(object obj, std::string const &s) { h5_write_attribute(obj, "Format", s); }
106
113 template <typename T>
114 inline void write_hdf5_format(object obj) {
115 h5_write_attribute(obj, "Format", get_hdf5_format<T>());
116 }
117
126 template <typename T>
127 inline void write_hdf5_format(object obj, [[maybe_unused]] T const &t) {
128 h5_write_attribute(obj, "Format", get_hdf5_format<T>());
129 }
130
137 void read_hdf5_format(object obj, std::string &s);
138
145 std::string read_hdf5_format(object obj);
146
154 void read_hdf5_format_from_key(group g, std::string const &key, std::string &s);
155
165 void assert_hdf5_format_as_string(object obj, const char *tag_expected, bool ignore_if_absent = false);
166
177 template <typename T>
178 void assert_hdf5_format(object obj, bool ignore_if_absent = false) {
179 assert_hdf5_format_as_string(obj, get_hdf5_format<T>().c_str(), ignore_if_absent);
180 }
181
193 template <typename T>
194 void assert_hdf5_format(object obj, [[maybe_unused]] T const &t, bool ignore_if_absent = false) {
195 assert_hdf5_format_as_string(obj, get_hdf5_format<T>().c_str(), ignore_if_absent);
196 }
197
199
200} // namespace h5
201
202#endif // LIBH5_FORMAT_HPP
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
void assert_hdf5_format(object obj, bool ignore_if_absent=false)
Assert that the hdf5_format tag attached to the given object is the same as the hdf5_format tag of th...
Definition format.hpp:178
void assert_hdf5_format_as_string(object obj, const char *tag_expected, bool ignore_if_absent)
Assert that the hdf5_format tag attached to the given object is the same as the given tag.
Definition format.cpp:52
void write_hdf5_format(object obj)
Write an hdf5_format tag for type T to an HDF5 attribute with the name 'Format'.
Definition format.hpp:114
std::string get_hdf5_format()
Get the hdf5_format tag of type T.
Definition format.hpp:83
void read_hdf5_format(object obj, std::string &s)
Read an hdf5_format tag from an HDF5 attribute with the name 'Format'.
Definition format.cpp:32
void read_hdf5_format_from_key(group g, std::string const &key, std::string &s)
Read an hdf5_format tag from an HDF5 attribute with the name 'Format'.
Definition format.cpp:45
void write_hdf5_format_as_string(object obj, std::string const &s)
Write a std::string to an HDF5 attribute with the name 'Format'.
Definition format.hpp:105
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
Macros used in the h5 library.
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:51