Mantid
Loading...
Searching...
No Matches
UniqueID.h
Go to the documentation of this file.
1#pragma once
2
3#include "MantidNexus/DllConfig.h"
5
6// Forward declarations for needed HDF5 functions
7// NOTE declare extern "C" to prevent conflict with actual declaration
8// NOTE use MYH5DLL, set to match HDF5's H5_DLL, to allow Windows build
9#if defined(_MSC_VER) // on Windows builds
10#define MYH5DLL __declspec(dllimport)
11#else
12#define MYH5DLL // gcc and clang do not need a DLL specifier
13#endif
14extern "C" {
18}
19
20namespace Mantid::Nexus {
21
29template <herr_t (*const D)(hid_t)> class UniqueID {
30protected:
32
33private:
34 UniqueID(UniqueID<D> const &uid) = delete;
35 UniqueID &operator=(UniqueID<D> const &) = delete;
36 void close();
37
38public:
39 // constructors / destructor
41 UniqueID(hid_t const id) : m_id(id) {}
43 UniqueID(UniqueID<D> &&uid) noexcept : m_id(uid.m_id) { uid.m_id = INVALID_ID; }
44
45 // assignment
46 UniqueID<D> &operator=(hid_t const id);
48
49 // comparators
50 bool operator==(int const id) const { return static_cast<int>(m_id) == id; }
51 bool operator<=(int const id) const { return static_cast<int>(m_id) <= id; }
52 bool operator<(int const id) const { return static_cast<int>(m_id) < id; }
53
54 // using the id
55 operator hid_t() const { return m_id; }
56
59 hid_t get() const { return m_id; }
60
61 hid_t release();
62 void reset(hid_t const id = INVALID_ID);
63 bool isValid() const;
64
66 static hid_t constexpr INVALID_ID{-1};
67};
68
71template <herr_t (*const D)(hid_t)> inline bool UniqueID<D>::isValid() const {
72 // fail early condition
73 if (m_id < 0) {
74 return false;
75 } else {
76 return H5Iis_valid(m_id) > 0;
77 }
78}
79
82template <herr_t (*const deleter)(hid_t)> inline void UniqueID<deleter>::close() {
83 if (isValid()) {
84 deleter(this->m_id);
85 this->m_id = INVALID_ID;
86 }
87}
88
90template <> inline void UniqueID<&H5Fclose>::close() {
91 if (isValid()) {
92 H5Fclose(this->m_id);
93 this->m_id = INVALID_ID;
94 // call garbage collection to close any and all open objects on this file
96 }
97}
98
101template <herr_t (*const D)(hid_t)> inline hid_t UniqueID<D>::release() {
102 hid_t tmp = m_id;
103 m_id = INVALID_ID;
104 return tmp;
105}
106
109template <herr_t (*const D)(hid_t)> inline void UniqueID<D>::reset(hid_t const id) {
110 if (m_id != id) {
111 close();
112 m_id = id;
113 }
114}
115
118template <herr_t (*const D)(hid_t)> inline UniqueID<D> &UniqueID<D>::operator=(hid_t const id) {
119 reset(id);
120 return *this;
121}
122
125template <herr_t (*const D)(hid_t)> inline UniqueID<D> &UniqueID<D>::operator=(UniqueID<D> &&uid) {
126 if (this != &uid) {
127 reset(uid.m_id);
128 uid.m_id = INVALID_ID;
129 }
130 return *this;
131}
132
133} // namespace Mantid::Nexus
gsl_vector * tmp
int64_t hid_t
This class defines data types which are used as part of the Nexus API.
int herr_t
MYH5DLL herr_t H5Fclose(hid_t)
MYH5DLL herr_t H5Iis_valid(hid_t)
MYH5DLL herr_t H5garbage_collect()
#define MYH5DLL
Definition UniqueID.h:12
A wrapper class for managing HDF5 object handles (hid_t).
Definition UniqueID.h:29
hid_t release()
Release hold on the managed ID; it will not be closed by this UniqueID.
Definition UniqueID.h:101
void reset(hid_t const id=INVALID_ID)
Close the existing ID and replace with the new ID; or, set to invalid.
Definition UniqueID.h:109
bool operator==(int const id) const
Definition UniqueID.h:50
hid_t get() const
Return the managed HDF5 handle.
Definition UniqueID.h:59
static hid_t constexpr INVALID_ID
represents an invalid ID value
Definition UniqueID.h:66
bool isValid() const
Return whether the UniqueId corresponds to a valid HDF5 object.
Definition UniqueID.h:71
UniqueID(UniqueID< D > &&uid) noexcept
Definition UniqueID.h:43
bool operator<(int const id) const
Definition UniqueID.h:52
bool operator<=(int const id) const
Definition UniqueID.h:51
UniqueID(UniqueID< D > const &uid)=delete
UniqueID(hid_t const id)
Definition UniqueID.h:41
UniqueID & operator=(UniqueID< D > const &)=delete
void close()
Close the held ID by calling its deleter function.
Definition UniqueID.h:82
Header for a base Nexus::Exception.