File indexing completed on 2026-09-17 09:16:53
0001 #pragma once
0002
0003 #include <H5Ipublic.h>
0004 #include <H5Tpublic.h>
0005
0006 namespace HighFive {
0007 namespace detail {
0008
0009 inline hid_t h5t_copy(hid_t original) {
0010 auto copy = H5Tcopy(original);
0011 if (copy == H5I_INVALID_HID) {
0012 HDF5ErrMapper::ToException<DataTypeException>("Error copying datatype.");
0013 }
0014
0015 return copy;
0016 }
0017
0018 inline hsize_t h5t_get_size(hid_t hid) {
0019 hsize_t size = H5Tget_size(hid);
0020 if (size == 0) {
0021 HDF5ErrMapper::ToException<DataTypeException>("Error getting size of datatype.");
0022 }
0023
0024 return size;
0025 }
0026
0027 inline H5T_sign_t h5t_get_sign(hid_t type_id) {
0028 auto sign = H5Tget_sign(type_id);
0029 if (sign == H5T_SGN_ERROR) {
0030 HDF5ErrMapper::ToException<DataTypeException>("Error getting the sign of datatype.");
0031 }
0032 return sign;
0033 }
0034
0035 inline H5T_cset_t h5t_get_cset(hid_t hid) {
0036 auto cset = H5Tget_cset(hid);
0037 if (cset == H5T_CSET_ERROR) {
0038 HDF5ErrMapper::ToException<DataTypeException>("Error getting cset of datatype.");
0039 }
0040
0041 return cset;
0042 }
0043
0044 inline H5T_str_t h5t_get_strpad(hid_t hid) {
0045 auto strpad = H5Tget_strpad(hid);
0046 if (strpad == H5T_STR_ERROR) {
0047 HDF5ErrMapper::ToException<DataTypeException>("Error getting strpad of datatype.");
0048 }
0049
0050 return strpad;
0051 }
0052
0053 inline void h5t_set_size(hid_t hid, hsize_t size) {
0054 if (H5Tset_size(hid, size) < 0) {
0055 HDF5ErrMapper::ToException<DataTypeException>("Error setting size of datatype.");
0056 }
0057 }
0058
0059 inline void h5t_set_cset(hid_t hid, H5T_cset_t cset) {
0060 if (H5Tset_cset(hid, cset) < 0) {
0061 HDF5ErrMapper::ToException<DataTypeException>("Error setting cset of datatype.");
0062 }
0063 }
0064
0065 inline void h5t_set_strpad(hid_t hid, H5T_str_t strpad) {
0066 if (H5Tset_strpad(hid, strpad) < 0) {
0067 HDF5ErrMapper::ToException<DataTypeException>("Error setting strpad of datatype.");
0068 }
0069 }
0070
0071 inline int h5t_get_nmembers(hid_t hid) {
0072 auto result = H5Tget_nmembers(hid);
0073
0074 if (result < 0) {
0075 throw DataTypeException("Could not get members of compound datatype");
0076 }
0077
0078 return result;
0079 }
0080
0081 inline char* h5t_get_member_name(hid_t type_id, unsigned membno) {
0082 char* name = H5Tget_member_name(type_id, membno);
0083 if (name == nullptr) {
0084 throw DataTypeException("Failed to get member names of compound datatype");
0085 }
0086
0087 return name;
0088 }
0089
0090
0091 inline size_t h5t_get_member_offset(hid_t type_id, unsigned membno) {
0092
0093
0094 return H5Tget_member_offset(type_id, membno);
0095 }
0096
0097 inline hid_t h5t_get_member_type(hid_t type_id, unsigned membno) {
0098 hid_t member_id = H5Tget_member_type(type_id, membno);
0099
0100 if (member_id < 0) {
0101 throw DataTypeException("Failed to get member type of compound datatype");
0102 }
0103
0104 return member_id;
0105 }
0106
0107 #if H5_VERSION_GE(1, 12, 0)
0108 inline herr_t h5t_reclaim(hid_t type_id, hid_t space_id, hid_t plist_id, void* buf) {
0109 herr_t err = H5Treclaim(type_id, space_id, plist_id, buf);
0110 if (err < 0) {
0111 throw DataTypeException("Failed to reclaim HDF5 internal memory");
0112 }
0113
0114 return err;
0115 }
0116 #endif
0117
0118 inline H5T_class_t h5t_get_class(hid_t type_id) {
0119 H5T_class_t class_id = H5Tget_class(type_id);
0120 if (class_id == H5T_NO_CLASS) {
0121 throw DataTypeException("Failed to get class of type");
0122 }
0123
0124 return class_id;
0125 }
0126
0127 inline htri_t h5t_equal(hid_t type1_id, hid_t type2_id) {
0128 htri_t equal = H5Tequal(type1_id, type2_id);
0129 if (equal < 0) {
0130 throw DataTypeException("Failed to compare two datatypes");
0131 }
0132
0133 return equal;
0134 }
0135
0136 inline htri_t h5t_is_variable_str(hid_t type_id) {
0137 htri_t is_variable = H5Tis_variable_str(type_id);
0138 if (is_variable < 0) {
0139 HDF5ErrMapper::ToException<DataTypeException>(
0140 "Failed to check if string is variable length");
0141 }
0142 return is_variable;
0143 }
0144
0145 inline herr_t h5t_set_fields(hid_t type_id,
0146 size_t spos,
0147 size_t epos,
0148 size_t esize,
0149 size_t mpos,
0150 size_t msize) {
0151 herr_t err = H5Tset_fields(type_id, spos, epos, esize, mpos, msize);
0152 if (err < 0) {
0153 HDF5ErrMapper::ToException<DataTypeException>(
0154 "Failed to create custom floating point data type");
0155 }
0156 return err;
0157 }
0158
0159 inline herr_t h5t_set_ebias(hid_t type_id, size_t ebias) {
0160 herr_t err = H5Tset_ebias(type_id, ebias);
0161 if (err < 0) {
0162 HDF5ErrMapper::ToException<DataTypeException>(
0163 "Failed to exponent bias of floating point data type");
0164 }
0165
0166 return err;
0167 }
0168
0169 inline hid_t h5t_create(H5T_class_t type, size_t size) {
0170 hid_t type_id = H5Tcreate(type, size);
0171 if (type_id == H5I_INVALID_HID) {
0172 HDF5ErrMapper::ToException<DataTypeException>("Failed to datatype");
0173 }
0174
0175 return type_id;
0176 }
0177
0178 inline herr_t h5t_insert(hid_t parent_id, const char* name, size_t offset, hid_t member_id) {
0179 herr_t err = H5Tinsert(parent_id, name, offset, member_id);
0180 if (err < 0) {
0181 HDF5ErrMapper::ToException<DataTypeException>("Failed to not add new member to datatype");
0182 }
0183
0184 return err;
0185 }
0186
0187 inline herr_t h5t_commit2(hid_t loc_id,
0188 const char* name,
0189 hid_t type_id,
0190 hid_t lcpl_id,
0191 hid_t tcpl_id,
0192 hid_t tapl_id) {
0193 herr_t err = H5Tcommit2(loc_id, name, type_id, lcpl_id, tcpl_id, tapl_id);
0194 if (err < 0) {
0195 HDF5ErrMapper::ToException<DataTypeException>("Failed to commit datatype");
0196 }
0197
0198 return err;
0199 }
0200
0201 inline herr_t h5t_close(hid_t type_id) {
0202 auto err = H5Tclose(type_id);
0203 if (err < 0) {
0204 HDF5ErrMapper::ToException<DataTypeException>("Failed to close datatype");
0205 }
0206
0207 return err;
0208 }
0209
0210 inline hid_t h5t_enum_create(hid_t base_id) {
0211 hid_t type_id = H5Tenum_create(base_id);
0212 if (type_id == H5I_INVALID_HID) {
0213 HDF5ErrMapper::ToException<DataTypeException>("Failed to create new enum datatype");
0214 }
0215 return type_id;
0216 }
0217
0218 inline herr_t h5t_enum_insert(hid_t type, const char* name, const void* value) {
0219 herr_t err = H5Tenum_insert(type, name, value);
0220 if (err < 0) {
0221 HDF5ErrMapper::ToException<DataTypeException>(
0222 "Failed to add new member to this enum datatype");
0223 }
0224 return err;
0225 }
0226
0227 inline hid_t h5t_open2(hid_t loc_id, const char* name, hid_t tapl_id) {
0228 hid_t datatype_id = H5Topen2(loc_id, name, tapl_id);
0229 if (datatype_id == H5I_INVALID_HID) {
0230 HDF5ErrMapper::ToException<DataTypeException>(
0231 std::string("Unable to open the datatype \"") + name + "\":");
0232 }
0233
0234 return datatype_id;
0235 }
0236
0237 }
0238 }