/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/handle.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/handle.hh Source File
DiFfRG
handle.hh
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdexcept>
6#include <string>
7#include <utility>
8
9namespace DiFfRG::hdf5
10{
11 inline void throw_if_negative(hid_t id, const char *what)
12 {
13 if (id < 0) throw std::runtime_error(std::string("hdf5lib: ") + what);
14 }
15
16 inline void throw_if_negative(herr_t err, const char *what)
17 {
18 if (err < 0) throw std::runtime_error(std::string("hdf5lib: ") + what);
19 }
20
24 class Handle
25 {
26 public:
27 Handle() = default;
28
29 explicit Handle(hid_t id) : id_(id)
30 {
31 if (id_ >= 0) H5Iinc_ref(id_);
32 }
33
37 static Handle take(hid_t id)
38 {
39 Handle h;
40 h.id_ = id;
41 return h;
42 }
43
44 Handle(const Handle &o) : id_(o.id_)
45 {
46 if (id_ >= 0) H5Iinc_ref(id_);
47 }
48
49 Handle(Handle &&o) noexcept : id_(o.id_) { o.id_ = H5I_INVALID_HID; }
50
52 {
53 if (this == &o) return *this;
54 reset();
55 id_ = o.id_;
56 if (id_ >= 0) H5Iinc_ref(id_);
57 return *this;
58 }
59
60 Handle &operator=(Handle &&o) noexcept
61 {
62 if (this == &o) return *this;
63 reset();
64 id_ = o.id_;
65 o.id_ = H5I_INVALID_HID;
66 return *this;
67 }
68
69 ~Handle() { reset(); }
70
71 void reset() noexcept
72 {
73 if (id_ >= 0) {
74 H5Idec_ref(id_);
75 id_ = H5I_INVALID_HID;
76 }
77 }
78
79 hid_t get() const noexcept { return id_; }
80 bool valid() const noexcept { return id_ >= 0; }
81
82 hid_t release() noexcept
83 {
84 hid_t t = id_;
85 id_ = H5I_INVALID_HID;
86 return t;
87 }
88
89 private:
90 hid_t id_ = H5I_INVALID_HID;
91 };
92} // namespace DiFfRG::hdf5
Definition handle.hh:25
bool valid() const noexcept
Definition handle.hh:80
hid_t release() noexcept
Definition handle.hh:82
void reset() noexcept
Definition handle.hh:71
Handle & operator=(const Handle &o)
Definition handle.hh:51
Handle(hid_t id)
Definition handle.hh:29
hid_t get() const noexcept
Definition handle.hh:79
~Handle()
Definition handle.hh:69
static Handle take(hid_t id)
Definition handle.hh:37
Handle & operator=(Handle &&o) noexcept
Definition handle.hh:60
Handle(const Handle &o)
Definition handle.hh:44
Handle(Handle &&o) noexcept
Definition handle.hh:49
hid_t id_
Definition handle.hh:90
Definition hdf5.hh:12
void throw_if_negative(hid_t id, const char *what)
Definition handle.hh:11