TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
optional.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_OPTIONAL_HPP
23#define LIBH5_STL_OPTIONAL_HPP
24
25#include "../format.hpp"
26#include "../group.hpp"
27#include "./string.hpp"
28
29#include <optional>
30#include <string>
31
32namespace h5 {
33
40 template <typename T>
41 struct hdf5_format_impl<std::optional<T>> {
42 static std::string invoke() { return hdf5_format_impl<T>::invoke(); }
43 };
44
55 template <typename T>
56 void h5_write(group g, std::string const &name, std::optional<T> const &opt) {
57 if (opt) h5_write(g, name, *opt);
58 }
59
70 template <typename T>
71 void h5_read(group g, std::string name, std::optional<T> &opt) {
72 opt.reset();
73 if (g.has_key(name)) opt.emplace(h5_read<T>(g, name));
74 }
75
78} // namespace h5
79
80#endif // LIBH5_STL_OPTIONAL_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 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,...
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