TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
object.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_OBJECT_HPP
23#define LIBH5_OBJECT_HPP
24
25#include "./utils.hpp"
26
27#include <string>
28
29namespace h5 {
30
49 class object {
50 protected:
51 // Wrapped HDF5 ID.
52 hid_t id = 0; // NOLINT (protected member is wanted here)
53
54 public:
61 [[nodiscard]] static object from_borrowed(hid_t id);
62
71 object(hid_t id = 0) : id(id) {}
72
77 object(object const &x);
78
83 object(object &&x) noexcept : id(x.id) { x.id = 0; }
84
89 object &operator=(object const &x) {
90 operator=(object(x));
91 return *this;
92 }
93
98 object &operator=(object &&x) noexcept;
99
101 ~object() { close(); }
102
104 void close();
105
107 operator hid_t() const { return id; }
108
110 [[nodiscard]] int get_ref_count() const;
111
113 [[nodiscard]] bool is_valid() const;
114 };
115
116 // simple but useful aliases. It does NOT check the h5 type of the object.
117 // FIXME : derive and make a check ??
118
121
124
127
130
133
141 namespace detail {
142
143 // Map a C++ type to an HDF5 type (specializations are in object.cpp).
144 template <typename T>
145 hid_t hid_t_of();
146
147 } // namespace detail
148
155 template <typename T>
156 [[nodiscard]] datatype hdf5_type() {
157 return object::from_borrowed(detail::hid_t_of<T>());
158 }
159
168 [[nodiscard]] std::string get_name_of_h5_type(datatype dt);
169
176 [[nodiscard]] datatype get_hdf5_type(dataset ds);
177
190 [[nodiscard]] bool hdf5_type_equal(datatype dt1, datatype dt2);
191
194} // namespace h5
195
196#endif // LIBH5_OBJECT_HPP
A generic handle for HDF5 objects.
Definition object.hpp:49
object(object &&x) noexcept
Move constructor steals the underlying HDF5 ID without increasing its reference count.
Definition object.hpp:83
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
Definition object.cpp:116
int get_ref_count() const
Get the current reference count.
Definition object.cpp:114
~object()
Destructor decreases the reference count and sets the object's ID to zero.
Definition object.hpp:101
object & operator=(object const &x)
Copy assignment operator copies the underlying HDF5 ID and increases its reference count.
Definition object.hpp:89
object(hid_t id=0)
Construct a new h5::object for a given HDF5 ID by taking ownership, i.e. without increasing the refer...
Definition object.hpp:71
static object from_borrowed(hid_t id)
Create an h5::object for a given HDF5 ID and increase its reference count.
Definition object.cpp:95
void close()
Release the HDF5 handle by decreasing the reference count and by setting the object's ID to zero.
Definition object.cpp:109
object datatype
Type alias for an HDF5 datatype.
Definition object.hpp:123
object dataset
Type alias for an HDF5 dataset.
Definition object.hpp:120
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
Definition object.cpp:198
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
Definition object.hpp:156
datatype get_hdf5_type(dataset ds)
Get the HDF5 type stored in a given h5::dataset.
Definition object.cpp:196
std::string get_name_of_h5_type(datatype dt)
Get the name of an h5::datatype (for error messages).
Definition object.cpp:183
int64_t hid_t
ID type used in HDF5.
Definition utils.hpp:45
Provides some utility functions for h5.