41static_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");
42static_assert(
sizeof(::hsize_t) ==
sizeof(
h5::hsize_t),
"h5::hsize_t type is not compatible with HDF5");
50 inline void xdecref(
hid_t id) {
51 if (H5Iis_valid(
id)) H5Idec_ref(
id);
55 inline void xincref(
hid_t id) {
56 if (H5Iis_valid(
id)) H5Iinc_ref(
id);
66 std::vector<h5_name_t> h5_name_table;
69 void init_h5_name_table() {
70 h5_name_table = std::vector<h5_name_t>{
123 template <>
hid_t hid_t_of<char> (){
return H5T_NATIVE_CHAR;}
124 template <>
hid_t hid_t_of<signed char> (){
return H5T_NATIVE_SCHAR;}
125 template <>
hid_t hid_t_of<unsigned char> (){
return H5T_NATIVE_UCHAR;}
127 template <>
hid_t hid_t_of<short> (){
return H5T_NATIVE_SHORT;}
128 template <>
hid_t hid_t_of<int> (){
return H5T_NATIVE_INT;}
129 template <>
hid_t hid_t_of<long> (){
return H5T_NATIVE_LONG;}
130 template <>
hid_t hid_t_of<long long> (){
return H5T_NATIVE_LLONG;}
132 template <>
hid_t hid_t_of<unsigned short> (){
return H5T_NATIVE_USHORT;}
133 template <>
hid_t hid_t_of<unsigned int> (){
return H5T_NATIVE_UINT;}
134 template <>
hid_t hid_t_of<unsigned long> (){
return H5T_NATIVE_ULONG;}
135 template <>
hid_t hid_t_of<unsigned long long> (){
return H5T_NATIVE_ULLONG;}
137 template <>
hid_t hid_t_of<float> (){
return H5T_NATIVE_FLOAT;}
138 template <>
hid_t hid_t_of<double> (){
return H5T_NATIVE_DOUBLE;}
139 template <>
hid_t hid_t_of<long double> (){
return H5T_NATIVE_LDOUBLE;}
141 template <>
hid_t hid_t_of<std::complex<float>> (){
return H5T_NATIVE_FLOAT;}
142 template <>
hid_t hid_t_of<std::complex<double>> (){
return H5T_NATIVE_DOUBLE;}
143 template <>
hid_t hid_t_of<std::complex<long double>> (){
return H5T_NATIVE_LDOUBLE;}
146 hid_t const str_dt = [](){
147 hid_t dt = H5Tcopy(H5T_C_S1);
148 H5Tset_size(dt, H5T_VARIABLE);
149 H5Tset_cset(dt, H5T_CSET_UTF8);
155 template <>
hid_t hid_t_of<std::string> (){
return detail::str_dt;}
156 template <>
hid_t hid_t_of<char *> (){
return detail::str_dt;}
157 template <>
hid_t hid_t_of<const char *> (){
return detail::str_dt;}
160 hid_t const cplx_cmpd_dt = [](){
161 hid_t dt = H5Tcreate(H5T_COMPOUND, 16);
162 H5Tinsert(dt,
"r", 0, H5T_NATIVE_DOUBLE);
163 H5Tinsert(dt,
"i", 8, H5T_NATIVE_DOUBLE);
169 template <>
hid_t hid_t_of<dcplx_t> (){
return detail::cplx_cmpd_dt;}
174 hid_t hid_t_of<bool>() {
175 hid_t bool_enum_h5type = H5Tenum_create(H5T_NATIVE_CHAR);
177 H5Tenum_insert(bool_enum_h5type,
"FALSE", (val = 0, &val));
178 H5Tenum_insert(bool_enum_h5type,
"TRUE", (val = 1, &val));
179 return bool_enum_h5type;
186 if (h5_name_table.empty()) init_h5_name_table();
189 auto _end = h5_name_table.end();
190 auto pos = std::find_if(h5_name_table.begin(), _end, [dt](
auto const &x) { return hdf5_type_equal(dt, x.hdf5_type); });
193 if (pos == _end)
throw std::logic_error(
"Error in h5::get_name_of_h5_type: datatype not supported");
200 attribute attr = H5Aopen(obj, name.c_str(), H5P_DEFAULT);
201 return H5Aget_type(attr);
206 if (H5Tget_class(dt1) == H5T_STRING) {
return H5Tget_class(dt2) == H5T_STRING; }
209 auto res = H5Tequal(dt1, dt2);
210 if (res < 0) {
throw std::runtime_error(
"Error in h5::hdf5_type_equal: H5Tequal call failed"); }
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 &rhs)
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.
object attribute
Type alias for an HDF5 attribute.
object dataset
Type alias for an HDF5 dataset.
bool hdf5_type_equal(datatype dt1, datatype dt2)
Check if two HDF5 datatypes are equal.
datatype get_hdf5_attribute_type(object obj, std::string const &name)
Get the HDF5 type of a named attribute attached to a given h5::object.
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()
Get the name of the HDF5 datatype corresponding to a C++ type.
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.