Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
hdfidentifier.hh
Go to the documentation of this file.
1#ifndef UTOPIA_DATAIO_HDFIDENTIFIER_HH
2#define UTOPIA_DATAIO_HDFIDENTIFIER_HH
3
4#include <functional>
5
6#include <hdf5.h>
7
8#include "hdfutilities.hh"
9
10namespace Utopia
11{
12namespace DataIO
13{
14
29{
30 hid_t _id = -1;
31 std::function< herr_t(hid_t) > _closing_func;
32
33 public:
39 void
41 {
42 using std::swap;
43 swap(_id, other._id);
44 swap(_closing_func, other._closing_func);
45 }
46
52 hid_t
53 get_id() const
54 {
55 return _id;
56 }
57
64 void
66 {
67 _id = id;
68 }
69
76 bool
77 is_valid() const
78 {
79 return check_validity(H5Iis_valid(_id), "identifier");
80 }
81
88 auto
90 {
91 if (is_valid())
92 {
93 return H5Iget_ref(_id);
94 }
95 else
96 {
97 return -1;
98 }
99 }
100
106 void
108 {
109 if (is_valid())
110 {
112 }
113 }
114
120 void
122 {
123 if (is_valid())
124 {
126 }
127 }
128
133 void
135 {
136 if (is_valid())
137 {
138 if (H5Iget_ref(_id) > 1)
139 {
141 }
142 else
143 {
145 }
146 }
147 _id = -1;
148 }
149
155 void
156 open(hid_t id, std::function< herr_t(hid_t) > closing_func)
157 {
158 if (is_valid())
159 {
160 throw std::runtime_error(
161 "Error, HDFIdentifier cannot bind to new identifier while "
162 "still being valid. Close first.");
163 }
164 _id = id;
166 }
167
177 {
178 open(id, closing_func);
179 }
180
186 {
187 }
188
195 _id(other._id), _closing_func(other._closing_func)
196 {
197 increment_refcount();
198
199 }
200
207 _id(std::move(other._id)), _closing_func(std::move(other._closing_func))
208 {
209 other._id = -1;
210 }
211
219 {
220 _id = other._id;
221 _closing_func = other._closing_func;
222
223 increment_refcount();
224 return *this;
225 }
226
236 {
237 _id = std::move(other._id);
238 other._id = -1;
239 return *this;
240 }
241
247 {
248 close();
249 }
250};
251
252// comparison operators
253
261bool
263{
264 return rhs.get_id() == lhs.get_id();
265}
266
274bool
276{
277 return not(lhs == rhs);
278}
279
286void
288{
289 lhs.swap(rhs);
290}
291
// end of group HDF5 // end of group DataIO
294
295}
296}
297
298#endif
Wrapper class around an hdf5 identifier, used to manage reference counts of the object this identifie...
Definition hdfidentifier.hh:29
auto get_refcount() const
Get the number of references currently referring to the object identified by this ID.
Definition hdfidentifier.hh:89
HDFIdentifier(const HDFIdentifier &other)
Construct HDFIdentifier by copying another instance of HDFIdentifier, incrementing the refcount of th...
Definition hdfidentifier.hh:194
~HDFIdentifier()
Destroy the HDFIdentifier object, decrementing its refcount.
Definition hdfidentifier.hh:246
void close()
Close the identifier and render the C-Level id held invalid.
Definition hdfidentifier.hh:134
hid_t get_id() const
Get the HDF5 id held by this object.
Definition hdfidentifier.hh:53
void open(hid_t id, std::function< herr_t(hid_t) > closing_func)
Open the object and bind it to another C-Level id.
Definition hdfidentifier.hh:156
HDFIdentifier(hid_t id, std::function< herr_t(hid_t) > closing_func)
Construct HDFIdentifier from the given arguments.
Definition hdfidentifier.hh:176
HDFIdentifier(HDFIdentifier &&other)
Construct HDFIdentifier by moving from another instance of HDFIdentifier.
Definition hdfidentifier.hh:206
void decrement_refcount()
Decrease the reference count of the object referred to by this ID by one.
Definition hdfidentifier.hh:121
std::function< herr_t(hid_t) > _closing_func
Definition hdfidentifier.hh:31
HDFIdentifier & operator=(HDFIdentifier &&other)
Assign HDFIdentifier by moving from another instance of HDFIdentifier.
Definition hdfidentifier.hh:235
HDFIdentifier()
Construct HDFIdentifier from the given arguments.
Definition hdfidentifier.hh:185
bool is_valid() const
Check if thi ID refers to a valid object.
Definition hdfidentifier.hh:77
hid_t _id
Definition hdfidentifier.hh:30
void swap(HDFIdentifier &other)
Exchange states between caller and 'other'.
Definition hdfidentifier.hh:40
void increment_refcount()
Increase the reference count of the object referred to by this ID by one.
Definition hdfidentifier.hh:107
void set_id(hid_t id)
Set id to the given argument. Only to be used to invalidate objects upon move or similar.
Definition hdfidentifier.hh:65
HDFIdentifier & operator=(const HDFIdentifier &other)
Assign HDFIdentifier by copying another instance of HDFIdentifier, incrementing the refcount of the h...
Definition hdfidentifier.hh:218
void swap(WriteTask< BGB, DW, DB, AWG, AWD > &lhs, WriteTask< BGB, DW, DB, AWG, AWD > &rhs)
Swaps the state of lhs and rhs.
Definition write_task.hh:240
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
bool check_validity(htri_t valid, const std::string_view object_name)
Check for validity of a hdf5 htri_t type or similar.
Definition hdfutilities.hh:73
bool operator!=(const HDFIdentifier &lhs, const HDFIdentifier &rhs)
Comparsion operator for inequality.
Definition hdfidentifier.hh:275
bool operator==(const HDFIdentifier &lhs, const HDFIdentifier &rhs)
Comparsion operator for equality.
Definition hdfidentifier.hh:262
This file provides metafunctions for automatically determining the nature of a C/C++ types at compile...
Definition agent.hh:11
Definition parallel.hh:235