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#ifndef LIBH5_FORMAT_HPP
27#define LIBH5_FORMAT_HPP
28
29#include "./macros.hpp"
30#include "./group.hpp"
31#include "./stl/string.hpp"
32
33#include <complex>
34#include <string>
35
36namespace h5 {
37
49 template <typename T>
50 struct hdf5_format_impl {
51 static std::string invoke() { return T::hdf5_format(); }
52 };
53
54#define H5_SPECIALIZE_FORMAT2(X, Y) \
55 \
56 template <> \
57 struct hdf5_format_impl<X> { \
58 static std::string invoke() { return H5_AS_STRING(Y); } \
59 }
60
61#define H5_SPECIALIZE_FORMAT(X) H5_SPECIALIZE_FORMAT2(X, X)
62
63 H5_SPECIALIZE_FORMAT(bool);
64 H5_SPECIALIZE_FORMAT(int);
65 H5_SPECIALIZE_FORMAT(long);
66 H5_SPECIALIZE_FORMAT(long long);
67 H5_SPECIALIZE_FORMAT(unsigned int);
68 H5_SPECIALIZE_FORMAT(unsigned long);
69 H5_SPECIALIZE_FORMAT(unsigned long long);
70 H5_SPECIALIZE_FORMAT(float);
71 H5_SPECIALIZE_FORMAT(double);
72 H5_SPECIALIZE_FORMAT(long double);
73 H5_SPECIALIZE_FORMAT2(std::complex<double>, complex);
74
81 template <typename T>
82 std::string get_hdf5_format() {
84 }
85
92 template <typename T>
93 std::string get_hdf5_format(T const &) {
95 }
96
103 inline void write_hdf5_format_as_string(object obj, std::string const &s) { h5_write_attribute(obj, "Format", s); }
104
112 template <typename T>
113 inline void write_hdf5_format(object obj, T const &) {
114 h5_write_attribute(obj, "Format", get_hdf5_format<T>());
115 }
116
123 void read_hdf5_format(object obj, std::string &s);
124
131 std::string read_hdf5_format(group g);
132
140 void read_hdf5_format_from_key(group g, std::string const &key, std::string &s);
141
151 void assert_hdf5_format_as_string(group g, const char *tag_expected, bool ignore_if_absent = false);
152
163 template <typename T>
164 void assert_hdf5_format(group g, T const &, bool ignore_if_absent = false) {
165 assert_hdf5_format_as_string(g, get_hdf5_format<T>().c_str(), ignore_if_absent);
166 }
167
170} // namespace h5
171
172#endif // LIBH5_FORMAT_HPP
A handle to an HDF5 group.
Definition group.hpp:44
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
void assert_hdf5_format(group g, T const &, bool ignore_if_absent=false)
Assert that the hdf5_format tag attached to the given group is the same as the hdf5_format tag of the...
Definition format.hpp:164
std::string get_hdf5_format()
Get the hdf5_format tag of type T.
Definition format.hpp:82
void assert_hdf5_format_as_string(group g, const char *tag_expected, bool ignore_if_absent)
Assert that the hdf5_format tag attached to the given group is the same as the given tag.
Definition format.cpp:52
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:103
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 string.hpp:34