40static_assert(std::is_same_v<hid_t, int64_t> or std::is_same_v<hid_t, int>,
"h5::hid_t type is not compatible with HDF5");
41static_assert(
sizeof(::hsize_t) ==
sizeof(
h5::hsize_t),
"h5::hsize_t type is not compatible with HDF5");
49 inline void xdecref(
hid_t id) {
50 if (H5Iis_valid(
id)) H5Idec_ref(
id);
54 inline void xincref(
hid_t id) {
55 if (H5Iis_valid(
id)) H5Iinc_ref(
id);
65 std::vector<h5_name_t> h5_name_table;
68 void init_h5_name_table() {
69 h5_name_table = std::vector<h5_name_t>{
122 template <>
hid_t hid_t_of<char> (){
return H5T_NATIVE_CHAR;}
123 template <>
hid_t hid_t_of<signed char> (){
return H5T_NATIVE_SCHAR;}
124 template <>
hid_t hid_t_of<unsigned char> (){
return H5T_NATIVE_UCHAR;}
126 template <>
hid_t hid_t_of<short> (){
return H5T_NATIVE_SHORT;}
127 template <>
hid_t hid_t_of<int> (){
return H5T_NATIVE_INT;}
128 template <>
hid_t hid_t_of<long> (){
return H5T_NATIVE_LONG;}
129 template <>
hid_t hid_t_of<long long> (){
return H5T_NATIVE_LLONG;}
131 template <>
hid_t hid_t_of<unsigned short> (){
return H5T_NATIVE_USHORT;}
132 template <>
hid_t hid_t_of<unsigned int> (){
return H5T_NATIVE_UINT;}
133 template <>
hid_t hid_t_of<unsigned long> (){
return H5T_NATIVE_ULONG;}
134 template <>
hid_t hid_t_of<unsigned long long> (){
return H5T_NATIVE_ULLONG;}
136 template <>
hid_t hid_t_of<float> (){
return H5T_NATIVE_FLOAT;}
137 template <>
hid_t hid_t_of<double> (){
return H5T_NATIVE_DOUBLE;}
138 template <>
hid_t hid_t_of<long double> (){
return H5T_NATIVE_LDOUBLE;}
140 template <>
hid_t hid_t_of<std::complex<float>> (){
return H5T_NATIVE_FLOAT;}
141 template <>
hid_t hid_t_of<std::complex<double>> (){
return H5T_NATIVE_DOUBLE;}
142 template <>
hid_t hid_t_of<std::complex<long double>> (){
return H5T_NATIVE_LDOUBLE;}
145 hid_t const str_dt = [](){
146 hid_t dt = H5Tcopy(H5T_C_S1);
147 H5Tset_size(dt, H5T_VARIABLE);
148 H5Tset_cset(dt, H5T_CSET_UTF8);
154 template <>
hid_t hid_t_of<std::string> (){
return detail::str_dt;}
155 template <>
hid_t hid_t_of<char *> (){
return detail::str_dt;}
156 template <>
hid_t hid_t_of<const char *> (){
return detail::str_dt;}
159 hid_t const cplx_cmpd_dt = [](){
160 hid_t dt = H5Tcreate(H5T_COMPOUND, 16);
161 H5Tinsert(dt,
"r", 0, H5T_NATIVE_DOUBLE);
162 H5Tinsert(dt,
"i", 8, H5T_NATIVE_DOUBLE);
168 template <>
hid_t hid_t_of<dcplx_t> (){
return detail::cplx_cmpd_dt;}
173 hid_t hid_t_of<bool>() {
174 hid_t bool_enum_h5type = H5Tenum_create(H5T_NATIVE_CHAR);
176 H5Tenum_insert(bool_enum_h5type,
"FALSE", (val = 0, &val));
177 H5Tenum_insert(bool_enum_h5type,
"TRUE", (val = 1, &val));
178 return bool_enum_h5type;
185 if (h5_name_table.empty()) init_h5_name_table();
188 auto _end = h5_name_table.end();
189 auto pos = std::find_if(h5_name_table.begin(), _end, [dt](
auto const &x) { return hdf5_type_equal(dt, x.hdf5_type); });
192 if (pos == _end)
throw std::logic_error(
"Error in h5::get_name_of_h5_type: datatype not supported");
200 if (H5Tget_class(dt1) == H5T_STRING) {
return H5Tget_class(dt2) == H5T_STRING; }
203 auto res = H5Tequal(dt1, dt2);
204 if (res < 0) {
throw std::runtime_error(
"Error in h5::hdf5_type_equal: H5Tequal call failed"); }
A generic handle for HDF5 objects.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
int get_ref_count() const
Get the current reference count.
object & operator=(object const &x)
Copy assignment operator copies the underlying HDF5 ID and increases its reference count.
object(hid_t id=0)
Construct a new h5::object for a given HDF5 ID by taking ownership, i.e. without increasing the refer...
static object from_borrowed(hid_t id)
Create an h5::object for a given HDF5 ID and increase its reference count.
void close()
Release the HDF5 handle by decreasing the reference count and by setting the object's ID to zero.
Provides a compound type and type traits for complex numbers.
object datatype
Type alias for an HDF5 datatype.
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
datatype get_hdf5_type(dataset ds)
Get the HDF5 type stored in a given h5::dataset.
std::string get_name_of_h5_type(datatype dt)
Get the name of an h5::datatype (for error messages).
unsigned long long hsize_t
Size type used in HDF5.
int64_t hid_t
ID type used in HDF5.
Macros used in the h5 library.
Provides a generic handle for HDF5 objects.
Provides some utility functions for h5.