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