HighFive 2.9.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
h5t_wrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <H5Ipublic.h>
4#include <H5Tpublic.h>
5
6namespace HighFive {
7namespace detail {
8
9inline hid_t h5t_copy(hid_t original) {
10 auto copy = H5Tcopy(original);
11 if (copy == H5I_INVALID_HID) {
12 HDF5ErrMapper::ToException<DataTypeException>("Error copying datatype.");
13 }
14
15 return copy;
16}
17
18inline hsize_t h5t_get_size(hid_t hid) {
19 hsize_t size = H5Tget_size(hid);
20 if (size == 0) {
21 HDF5ErrMapper::ToException<DataTypeException>("Error getting size of datatype.");
22 }
23
24 return size;
25}
26
27inline H5T_cset_t h5t_get_cset(hid_t hid) {
28 auto cset = H5Tget_cset(hid);
29 if (cset == H5T_CSET_ERROR) {
30 HDF5ErrMapper::ToException<DataTypeException>("Error getting cset of datatype.");
31 }
32
33 return cset;
34}
35
36inline H5T_str_t h5t_get_strpad(hid_t hid) {
37 auto strpad = H5Tget_strpad(hid);
38 if (strpad == H5T_STR_ERROR) {
39 HDF5ErrMapper::ToException<DataTypeException>("Error getting strpad of datatype.");
40 }
41
42 return strpad;
43}
44
45inline void h5t_set_size(hid_t hid, hsize_t size) {
46 if (H5Tset_size(hid, size) < 0) {
47 HDF5ErrMapper::ToException<DataTypeException>("Error setting size of datatype.");
48 }
49}
50
51inline void h5t_set_cset(hid_t hid, H5T_cset_t cset) {
52 if (H5Tset_cset(hid, cset) < 0) {
53 HDF5ErrMapper::ToException<DataTypeException>("Error setting cset of datatype.");
54 }
55}
56
57inline void h5t_set_strpad(hid_t hid, H5T_str_t strpad) {
58 if (H5Tset_strpad(hid, strpad) < 0) {
59 HDF5ErrMapper::ToException<DataTypeException>("Error setting strpad of datatype.");
60 }
61}
62
63inline int h5t_get_nmembers(hid_t hid) {
64 auto result = H5Tget_nmembers(hid);
65
66 if (result < 0) {
67 throw DataTypeException("Could not get members of compound datatype");
68 }
69
70 return result;
71}
72
73inline char* h5t_get_member_name(hid_t type_id, unsigned membno) {
74 char* name = H5Tget_member_name(type_id, membno);
75 if (name == nullptr) {
76 throw DataTypeException("Failed to get member names of compound datatype");
77 }
78
79 return name;
80}
81
82
83inline size_t h5t_get_member_offset(hid_t type_id, unsigned membno) {
84 // Note, this function is peculiar. On failure it returns 0, yet 0 is also
85 // what's returned on failure.
86 return H5Tget_member_offset(type_id, membno);
87}
88
89inline hid_t h5t_get_member_type(hid_t type_id, unsigned membno) {
90 hid_t member_id = H5Tget_member_type(type_id, membno);
91
92 if (member_id < 0) {
93 throw DataTypeException("Failed to get member type of compound datatype");
94 }
95
96 return member_id;
97}
98
99#if H5_VERSION_GE(1, 12, 0)
100inline herr_t h5t_reclaim(hid_t type_id, hid_t space_id, hid_t plist_id, void* buf) {
101 herr_t err = H5Treclaim(type_id, space_id, plist_id, buf);
102 if (err < 0) {
103 throw DataTypeException("Failed to reclaim HDF5 internal memory");
104 }
105
106 return err;
107}
108#endif
109
110inline H5T_class_t h5t_get_class(hid_t type_id) {
111 H5T_class_t class_id = H5Tget_class(type_id);
112 if (class_id == H5T_NO_CLASS) {
113 throw DataTypeException("Failed to get class of type");
114 }
115
116 return class_id;
117}
118
119inline htri_t h5t_equal(hid_t type1_id, hid_t type2_id) {
120 htri_t equal = H5Tequal(type1_id, type2_id);
121 if (equal < 0) {
122 throw DataTypeException("Failed to compare two datatypes");
123 }
124
125 return equal;
126}
127
128inline htri_t h5t_is_variable_str(hid_t type_id) {
129 htri_t is_variable = H5Tis_variable_str(type_id);
130 if (is_variable < 0) {
132 "Failed to check if string is variable length");
133 }
134 return is_variable;
135}
136
137inline herr_t h5t_set_fields(hid_t type_id,
138 size_t spos,
139 size_t epos,
140 size_t esize,
141 size_t mpos,
142 size_t msize) {
143 herr_t err = H5Tset_fields(type_id, spos, epos, esize, mpos, msize);
144 if (err < 0) {
146 "Failed to create custom floating point data type");
147 }
148 return err;
149}
150
151inline herr_t h5t_set_ebias(hid_t type_id, size_t ebias) {
152 herr_t err = H5Tset_ebias(type_id, ebias);
153 if (err < 0) {
155 "Failed to exponent bias of floating point data type");
156 }
157
158 return err;
159}
160
161inline hid_t h5t_create(H5T_class_t type, size_t size) {
162 hid_t type_id = H5Tcreate(type, size);
163 if (type_id == H5I_INVALID_HID) {
165 }
166
167 return type_id;
168}
169
170inline herr_t h5t_insert(hid_t parent_id, const char* name, size_t offset, hid_t member_id) {
171 herr_t err = H5Tinsert(parent_id, name, offset, member_id);
172 if (err < 0) {
173 HDF5ErrMapper::ToException<DataTypeException>("Failed to not add new member to datatype");
174 }
175
176 return err;
177}
178
179inline herr_t h5t_commit2(hid_t loc_id,
180 const char* name,
181 hid_t type_id,
182 hid_t lcpl_id,
183 hid_t tcpl_id,
184 hid_t tapl_id) {
185 herr_t err = H5Tcommit2(loc_id, name, type_id, lcpl_id, tcpl_id, tapl_id);
186 if (err < 0) {
187 HDF5ErrMapper::ToException<DataTypeException>("Failed to commit datatype");
188 }
189
190 return err;
191}
192
193inline herr_t h5t_close(hid_t type_id) {
194 auto err = H5Tclose(type_id);
195 if (err < 0) {
196 HDF5ErrMapper::ToException<DataTypeException>("Failed to close datatype");
197 }
198
199 return err;
200}
201
202inline hid_t h5t_enum_create(hid_t base_id) {
203 hid_t type_id = H5Tenum_create(base_id);
204 if (type_id == H5I_INVALID_HID) {
205 HDF5ErrMapper::ToException<DataTypeException>("Failed to create new enum datatype");
206 }
207 return type_id;
208}
209
210inline herr_t h5t_enum_insert(hid_t type, const char* name, const void* value) {
211 herr_t err = H5Tenum_insert(type, name, value);
212 if (err < 0) {
214 "Failed to add new member to this enum datatype");
215 }
216 return err;
217}
218
219inline hid_t h5t_open2(hid_t loc_id, const char* name, hid_t tapl_id) {
220 hid_t datatype_id = H5Topen2(loc_id, name, tapl_id);
221 if (datatype_id == H5I_INVALID_HID) {
223 std::string("Unable to open the datatype \"") + name + "\":");
224 }
225
226 return datatype_id;
227}
228
229} // namespace detail
230} // namespace HighFive
Definition H5_definitions.hpp:22
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:43