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