API
H5Utils.hpp
Go to the documentation of this file.
1 /** \file H5Utils.hpp
2  * \brief Utilities for HDF5.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * History:
6  * - 2017-08-28 created by JRM
7  */
8 
9 #ifndef utils_H5Utils_hpp
10 #define utils_H5Utils_hpp
11 
12 #include <hdf5.h>
13 
14 namespace MagAOX
15 {
16 namespace utils
17 {
18 
19 struct H5FileT
20 {
21  static herr_t close( hid_t & h )
22  {
23  return H5Fclose(h);
24  }
25 };
26 
27 struct H5DatasetT
28 {
29  static herr_t close( hid_t & h )
30  {
31  return H5Dclose(h);
32  }
33 };
34 
36 {
37  static herr_t close( hid_t & h )
38  {
39  return H5Sclose(h);
40  }
41 };
42 
44 {
45  static herr_t close( hid_t & h )
46  {
47  return H5Pclose(h);
48  }
49 };
50 
52 {
53  static herr_t close( hid_t & h )
54  {
55  return H5Aclose(h);
56  }
57 };
58 
59 ///A somewhat smart HDF5 handle.
60 /** Makes sure that the associated hdf5 library resources are closed when out of scope.
61  * Does not do reference counting, so copy and assignment are deleted. Assignment operator from hid_t is the only way to
62  * set the handle, and the hid_t conversion operator allows this to be passed directly to hdf5 library functions.
63  *
64  * \note You must never call the raw hdf5 close function (e.g. H5Fclose) on one of these handles.
65  *
66  * \tparam T is one of the handle types.
67  */
68 template<class T>
69 class H5Handle
70 {
71 protected:
72  hid_t _hand {0}; ///< The underlying handle.
73 
74 public:
75  //Have to define since we're deleting the copy constructor apparently.
76  H5Handle(){;}
77 
78 
79  //Prevent copying, which would risk deleting the handle without all copies knowing about it.
80  H5Handle(const H5Handle &) = delete;
81  H5Handle & operator=(const H5Handle &) = delete;
82 
83 
84 
85  ///Assignment from a basic hdf5 handle.
86  /** Note that if the handle has already been assigned, then it is closed first.
87  *
88  */
89  hid_t & operator=(const hid_t & hand /**< [in] the basic handle*/)
90  {
91  if(_hand) close();
92 
93  _hand = hand;
94 
95  return _hand; ///\returns a reference to the handle
96  }
97 
98  ///Conversion operator
99  operator hid_t()
100  {
101  return _hand;
102  }
103 
104  ///Close the handle
105  /** Calls the close function of the handle type T
106  */
107  herr_t close()
108  {
109  if(_hand == 0) return 0;
110 
111  herr_t rv = T::close(_hand); /// \returns the herr_t code from the hdf5 close function
112 
113  _hand = 0;
114 
115  return rv;
116  }
117 
118  ///Destructor. Calls the close function.
120  {
121  close();
122  }
123 };
124 
125 ///Handle for an HDF5 file.
127 
128 ///Handle for an HDF5 dataset.
130 
131 ///Handle for an HDF5 dataspace.
133 
134 ///Handle for an HDF5 property list.
136 
137 ///Handle for an HDF5 attribute.
139 
140 } //namespace utils
141 } //namespace MagAOX
142 
143 #endif //utils_H5Utils_hpp
144 
A somewhat smart HDF5 handle.
Definition: H5Utils.hpp:70
~H5Handle()
Destructor. Calls the close function.
Definition: H5Utils.hpp:119
herr_t close()
Close the handle.
Definition: H5Utils.hpp:107
hid_t _hand
The underlying handle.
Definition: H5Utils.hpp:72
H5Handle(const H5Handle &)=delete
hid_t & operator=(const hid_t &hand)
Assignment from a basic hdf5 handle.
Definition: H5Utils.hpp:89
H5Handle & operator=(const H5Handle &)=delete
H5Handle< H5PropertyT > H5Handle_P
Handle for an HDF5 property list.
Definition: H5Utils.hpp:135
H5Handle< H5AttributeT > H5Handle_A
Handle for an HDF5 attribute.
Definition: H5Utils.hpp:138
H5Handle< H5DatasetT > H5Handle_D
Handle for an HDF5 dataset.
Definition: H5Utils.hpp:129
H5Handle< H5DataspaceT > H5Handle_S
Handle for an HDF5 dataspace.
Definition: H5Utils.hpp:132
H5Handle< H5FileT > H5Handle_F
Handle for an HDF5 file.
Definition: H5Utils.hpp:126
Definition: dm.hpp:24
Definition: utils.py:1
static herr_t close(hid_t &h)
Definition: H5Utils.hpp:53
static herr_t close(hid_t &h)
Definition: H5Utils.hpp:29
static herr_t close(hid_t &h)
Definition: H5Utils.hpp:37
static herr_t close(hid_t &h)
Definition: H5Utils.hpp:21
static herr_t close(hid_t &h)
Definition: H5Utils.hpp:45