Utopia  2
Framework for studying models of complex & adaptive systems.
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 
10 namespace Utopia
11 {
12 namespace DataIO
13 {
14 
28 class HDFIdentifier final
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);
45  }
46 
52  hid_t
53  get_id() const
54  {
55  return _id;
56  }
57 
64  void
65  set_id(hid_t id)
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
89  get_refcount() const
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  {
111  H5Iinc_ref(_id);
112  }
113  }
114 
120  void
122  {
123  if (is_valid())
124  {
125  H5Idec_ref(_id);
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;
165  _closing_func = closing_func;
166  }
167 
176  HDFIdentifier(hid_t id, std::function< herr_t(hid_t) > closing_func)
177  {
178  open(id, closing_func);
179  }
180 
185  HDFIdentifier() : _id(-1), _closing_func([](hid_t) -> herr_t { return 0; })
186  {
187  }
188 
194  HDFIdentifier(const HDFIdentifier& other) :
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 
218  operator=(const HDFIdentifier& other)
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 
261 bool
262 operator==(const HDFIdentifier& lhs, const HDFIdentifier& rhs)
263 {
264  return rhs.get_id() == lhs.get_id();
265 }
266 
274 bool
275 operator!=(const HDFIdentifier& lhs, const HDFIdentifier& rhs)
276 {
277  return not(lhs == rhs);
278 }
279 
286 void
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()
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
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(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=(HDFIdentifier &&other)
Assign HDFIdentifier by moving from another instance of HDFIdentifier.
Definition: hdfidentifier.hh:235
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
void swap(HDFIdentifier &lhs, HDFIdentifier &rhs)
Exchange the states of lhs and rhs.
Definition: hdfidentifier.hh:287
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