diff --git a/include/highfive/H5DataType.hpp b/include/highfive/H5DataType.hpp index 886107961..f8fa218ca 100644 --- a/include/highfive/H5DataType.hpp +++ b/include/highfive/H5DataType.hpp @@ -19,6 +19,9 @@ #include "bits/string_padding.hpp" #include "H5PropertyList.hpp" +#include "bits/h5_wrapper.hpp" +#include "bits/h5t_wrapper.hpp" + namespace HighFive { @@ -236,21 +239,16 @@ class CompoundType: public DataType { ss << "hid " << _hid << " does not refer to a compound data type"; throw DataTypeException(ss.str()); } - int result = H5Tget_nmembers(_hid); - if (result < 0) { - throw DataTypeException("Could not get members of compound datatype"); - } - size_t n_members = static_cast(result); + size_t n_members = static_cast(detail::h5t_get_nmembers(_hid)); members.reserve(n_members); for (unsigned i = 0; i < n_members; i++) { - char* name = H5Tget_member_name(_hid, i); - size_t offset = H5Tget_member_offset(_hid, i); - hid_t member_hid = H5Tget_member_type(_hid, i); + char* name = detail::h5t_get_member_name(_hid, i); + size_t offset = detail::h5t_get_member_offset(_hid, i); + hid_t member_hid = detail::h5t_get_member_type(_hid, i); DataType member_type{member_hid}; members.emplace_back(std::string(name), member_type, offset); - if (H5free_memory(name) < 0) { - throw DataTypeException("Could not free names from the compound datatype"); - } + + detail::h5_free_memory(name); } } diff --git a/include/highfive/H5Exception.hpp b/include/highfive/H5Exception.hpp index 54905aa0f..e51013520 100644 --- a/include/highfive/H5Exception.hpp +++ b/include/highfive/H5Exception.hpp @@ -151,6 +151,15 @@ class PropertyException: public Exception { : Exception(err_msg) {} }; +/// +/// \brief Exception specific to HighFive Link interface +/// +class LinkException: public Exception { + public: + LinkException(const std::string& err_msg) + : Exception(err_msg) {} +}; + /// /// \brief Exception specific to HighFive Reference interface /// diff --git a/include/highfive/H5Utility.hpp b/include/highfive/H5Utility.hpp index 64ac1e5c8..3c51eb3e2 100644 --- a/include/highfive/H5Utility.hpp +++ b/include/highfive/H5Utility.hpp @@ -9,11 +9,11 @@ #pragma once -#include #include #include #include +#include "bits/h5e_wrapper.hpp" #include "bits/H5Friends.hpp" namespace HighFive { @@ -25,13 +25,15 @@ class SilenceHDF5 { public: inline SilenceHDF5(bool enable = true) : _client_data(nullptr) { - H5Eget_auto2(H5E_DEFAULT, &_func, &_client_data); - if (enable) - H5Eset_auto2(H5E_DEFAULT, NULL, NULL); + detail::nothrow::h5e_get_auto2(H5E_DEFAULT, &_func, &_client_data); + + if (enable) { + detail::nothrow::h5e_set_auto2(H5E_DEFAULT, nullptr, nullptr); + } } inline ~SilenceHDF5() { - H5Eset_auto2(H5E_DEFAULT, _func, _client_data); + detail::nothrow::h5e_set_auto2(H5E_DEFAULT, _func, _client_data); } private: diff --git a/include/highfive/bits/H5Annotate_traits_misc.hpp b/include/highfive/bits/H5Annotate_traits_misc.hpp index 85d2798fe..99509eb69 100644 --- a/include/highfive/bits/H5Annotate_traits_misc.hpp +++ b/include/highfive/bits/H5Annotate_traits_misc.hpp @@ -16,6 +16,7 @@ #include "H5Attribute_misc.hpp" #include "H5Iterables_misc.hpp" +#include "h5a_wrapper.hpp" namespace HighFive { @@ -23,16 +24,12 @@ template inline Attribute AnnotateTraits::createAttribute(const std::string& attribute_name, const DataSpace& space, const DataType& dtype) { - auto attr_id = H5Acreate2(static_cast(this)->getId(), - attribute_name.c_str(), - dtype.getId(), - space.getId(), - H5P_DEFAULT, - H5P_DEFAULT); - if (attr_id < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to create the attribute \"") + attribute_name + "\":"); - } + auto attr_id = detail::h5a_create2(static_cast(this)->getId(), + attribute_name.c_str(), + dtype.getId(), + space.getId(), + H5P_DEFAULT, + H5P_DEFAULT); return detail::make_attribute(attr_id); } @@ -57,30 +54,20 @@ inline Attribute AnnotateTraits::createAttribute(const std::string& at template inline void AnnotateTraits::deleteAttribute(const std::string& attribute_name) { - if (H5Adelete(static_cast(this)->getId(), attribute_name.c_str()) < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to delete attribute \"") + attribute_name + "\":"); - } + detail::h5a_delete(static_cast(this)->getId(), attribute_name.c_str()); } template inline Attribute AnnotateTraits::getAttribute(const std::string& attribute_name) const { - const auto attr_id = - H5Aopen(static_cast(this)->getId(), attribute_name.c_str(), H5P_DEFAULT); - if (attr_id < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to open the attribute \"") + attribute_name + "\":"); - } + const auto attr_id = detail::h5a_open(static_cast(this)->getId(), + attribute_name.c_str(), + H5P_DEFAULT); return detail::make_attribute(attr_id); } template inline size_t AnnotateTraits::getNumberAttributes() const { - int res = H5Aget_num_attrs(static_cast(this)->getId()); - if (res < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to count attributes in existing group or file")); - } + int res = detail::h5a_get_num_attrs(static_cast(this)->getId()); return static_cast(res); } @@ -92,27 +79,20 @@ inline std::vector AnnotateTraits::listAttributeNames() c size_t num_objs = getNumberAttributes(); names.reserve(num_objs); - if (H5Aiterate2(static_cast(this)->getId(), - H5_INDEX_NAME, - H5_ITER_INC, - NULL, - &details::internal_high_five_iterate, - static_cast(&iterateData)) < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to list attributes in group")); - } + detail::h5a_iterate2(static_cast(this)->getId(), + H5_INDEX_NAME, + H5_ITER_INC, + nullptr, + &details::internal_high_five_iterate, + static_cast(&iterateData)); + // TODO catch and rethrow with a better message. return names; } template inline bool AnnotateTraits::hasAttribute(const std::string& attr_name) const { - int res = H5Aexists(static_cast(this)->getId(), attr_name.c_str()); - if (res < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to check for attribute in group")); - } - return res; + return detail::h5a_exists(static_cast(this)->getId(), attr_name.c_str()); } } // namespace HighFive diff --git a/include/highfive/bits/H5Attribute_misc.hpp b/include/highfive/bits/H5Attribute_misc.hpp index 651678829..c25e1dd91 100644 --- a/include/highfive/bits/H5Attribute_misc.hpp +++ b/include/highfive/bits/H5Attribute_misc.hpp @@ -14,36 +14,36 @@ #include #include -#include #include #include "../H5DataSpace.hpp" #include "H5Converter_misc.hpp" #include "H5ReadWrite_misc.hpp" #include "H5Utils.hpp" +#include "h5a_wrapper.hpp" +#include "h5d_wrapper.hpp" +#include "h5t_wrapper.hpp" namespace HighFive { inline std::string Attribute::getName() const { return details::get_name( - [&](char* buffer, size_t length) { return H5Aget_name(_hid, length, buffer); }); + [&](char* buffer, size_t length) { return detail::h5a_get_name(_hid, length, buffer); }); } inline size_t Attribute::getStorageSize() const { - return static_cast(H5Aget_storage_size(_hid)); + return static_cast(detail::h5a_get_storage_size(_hid)); } inline DataType Attribute::getDataType() const { DataType res; - res._hid = H5Aget_type(_hid); + res._hid = detail::h5a_get_type(_hid); return res; } inline DataSpace Attribute::getSpace() const { DataSpace space; - if ((space._hid = H5Aget_space(_hid)) < 0) { - HDF5ErrMapper::ToException("Unable to get DataSpace out of Attribute"); - } + space._hid = detail::h5a_get_space(_hid); return space; } @@ -94,10 +94,10 @@ inline void Attribute::read(T& array) const { if (c == DataTypeClass::VarLen || t.isVariableStr()) { #if H5_VERSION_GE(1, 12, 0) // This one have been created in 1.12.0 - (void) H5Treclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); + (void) detail::h5t_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); #else // This one is deprecated since 1.12.0 - (void) H5Dvlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); + (void) detail::h5d_vlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); #endif } } @@ -107,9 +107,7 @@ inline void Attribute::read(T* array, const DataType& mem_datatype) const { static_assert(!std::is_const::value, "read() requires a non-const structure to read data into"); - if (H5Aread(getId(), mem_datatype.getId(), static_cast(array)) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Read: "); - } + detail::h5a_read(getId(), mem_datatype.getId(), static_cast(array)); } template @@ -147,9 +145,7 @@ inline void Attribute::write(const T& buffer) { template inline void Attribute::write_raw(const T* buffer, const DataType& mem_datatype) { - if (H5Awrite(getId(), mem_datatype.getId(), buffer) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Write: "); - } + detail::h5a_write(getId(), mem_datatype.getId(), buffer); } template diff --git a/include/highfive/bits/H5DataSet_misc.hpp b/include/highfive/bits/H5DataSet_misc.hpp index 4411b4c0d..4817fe001 100644 --- a/include/highfive/bits/H5DataSet_misc.hpp +++ b/include/highfive/bits/H5DataSet_misc.hpp @@ -14,26 +14,24 @@ #include #include -#include #include +#include "h5d_wrapper.hpp" #include "H5Utils.hpp" namespace HighFive { inline uint64_t DataSet::getStorageSize() const { - return H5Dget_storage_size(_hid); + return detail::h5d_get_storage_size(_hid); } inline DataType DataSet::getDataType() const { - return DataType(H5Dget_type(_hid)); + return DataType(detail::h5d_get_type(_hid)); } inline DataSpace DataSet::getSpace() const { DataSpace space; - if ((space._hid = H5Dget_space(_hid)) < 0) { - HDF5ErrMapper::ToException("Unable to get DataSpace out of DataSet"); - } + space._hid = detail::h5d_get_space(_hid); return space; } @@ -42,11 +40,7 @@ inline DataSpace DataSet::getMemSpace() const { } inline uint64_t DataSet::getOffset() const { - uint64_t addr = H5Dget_offset(_hid); - if (addr == HADDR_UNDEF) { - HDF5ErrMapper::ToException("Cannot get offset of DataSet."); - } - return addr; + return static_cast(detail::h5d_get_offset(_hid)); } inline void DataSet::resize(const std::vector& dims) { @@ -58,10 +52,7 @@ inline void DataSet::resize(const std::vector& dims) { } std::vector real_dims(dims.begin(), dims.end()); - - if (H5Dset_extent(getId(), real_dims.data()) < 0) { - HDF5ErrMapper::ToException("Could not resize dataset."); - } + detail::h5d_set_extent(getId(), real_dims.data()); } } // namespace HighFive diff --git a/include/highfive/bits/H5DataType_misc.hpp b/include/highfive/bits/H5DataType_misc.hpp index 27f863d3d..988acd2a4 100644 --- a/include/highfive/bits/H5DataType_misc.hpp +++ b/include/highfive/bits/H5DataType_misc.hpp @@ -16,7 +16,6 @@ #endif #include -#include #ifdef H5_USE_HALF_FLOAT #include @@ -24,6 +23,7 @@ #include "H5Inspector_misc.hpp" #include "h5t_wrapper.hpp" +#include "h5i_wrapper.hpp" namespace HighFive { @@ -38,7 +38,7 @@ inline bool DataType::empty() const noexcept { } inline DataTypeClass DataType::getClass() const { - return convert_type_class(H5Tget_class(_hid)); + return convert_type_class(detail::h5t_get_class(_hid)); } inline size_t DataType::getSize() const { @@ -46,7 +46,7 @@ inline size_t DataType::getSize() const { } inline bool DataType::operator==(const DataType& other) const { - return (H5Tequal(_hid, other._hid) > 0); + return detail::h5t_equal(_hid, other._hid) > 0; } inline bool DataType::operator!=(const DataType& other) const { @@ -54,11 +54,7 @@ inline bool DataType::operator!=(const DataType& other) const { } inline bool DataType::isVariableStr() const { - auto var_value = H5Tis_variable_str(_hid); - if (var_value < 0) { - HDF5ErrMapper::ToException("Unable to define datatype size to variable"); - } - return static_cast(var_value); + return detail::h5t_is_variable_str(_hid); } inline bool DataType::isFixedLenStr() const { @@ -66,7 +62,7 @@ inline bool DataType::isFixedLenStr() const { } inline bool DataType::isReference() const { - return H5Tequal(_hid, H5T_STD_REF_OBJ) > 0; + return detail::h5t_equal(_hid, H5T_STD_REF_OBJ) > 0; } inline StringType DataType::asStringType() const { @@ -74,8 +70,8 @@ inline StringType DataType::asStringType() const { throw DataTypeException("Invalid conversion to StringType."); } - if (isValid() && H5Iinc_ref(_hid) < 0) { - throw ObjectException("Reference counter increase failure"); + if (isValid()) { + detail::h5i_inc_ref(_hid); } return StringType(_hid); @@ -183,11 +179,11 @@ template <> inline AtomicType::AtomicType() { _hid = detail::h5t_copy(H5T_NATIVE_FLOAT); // Sign position, exponent position, exponent size, mantissa position, mantissa size - H5Tset_fields(_hid, 15, 10, 5, 0, 10); + detail::h5t_set_fields(_hid, 15, 10, 5, 0, 10); // Total datatype size (in bytes) detail::h5t_set_size(_hid, 2); // Floating point exponent bias - H5Tset_ebias(_hid, 15); + detail::h5t_set_ebias(_hid, 15); } #endif @@ -316,8 +312,8 @@ inline AtomicType::AtomicType() { inline size_t find_first_atomic_member_size(hid_t hid) { // Recursive exit condition - if (H5Tget_class(hid) == H5T_COMPOUND) { - auto number_of_members = H5Tget_nmembers(hid); + if (detail::h5t_get_class(hid) == H5T_COMPOUND) { + auto number_of_members = detail::h5t_get_nmembers(hid); if (number_of_members == -1) { throw DataTypeException("Cannot get members of CompoundType with hid: " + std::to_string(hid)); @@ -327,11 +323,11 @@ inline size_t find_first_atomic_member_size(hid_t hid) { std::to_string(hid)); } - auto member_type = H5Tget_member_type(hid, 0); + auto member_type = detail::h5t_get_member_type(hid, 0); auto size = find_first_atomic_member_size(member_type); - H5Tclose(member_type); + detail::h5t_close(member_type); return size; - } else if (H5Tget_class(hid) == H5T_STRING) { + } else if (detail::h5t_get_class(hid) == H5T_STRING) { return 1; } return detail::h5t_get_size(hid); @@ -391,43 +387,36 @@ inline void CompoundType::create(size_t size) { } // Create the HDF5 type - if ((_hid = H5Tcreate(H5T_COMPOUND, size)) < 0) { - HDF5ErrMapper::ToException("Could not create new compound datatype"); - } + _hid = detail::h5t_create(H5T_COMPOUND, size); // Loop over all the members and insert them into the datatype for (const auto& member: members) { - if (H5Tinsert(_hid, member.name.c_str(), member.offset, member.base_type.getId()) < 0) { - HDF5ErrMapper::ToException("Could not add new member to datatype"); - } + detail::h5t_insert(_hid, member.name.c_str(), member.offset, member.base_type.getId()); } } #undef _H5_STRUCT_PADDING inline void CompoundType::commit(const Object& object, const std::string& name) const { - H5Tcommit2(object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + detail::h5t_commit2( + object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } template inline void EnumType::create() { // Create the HDF5 type - if ((_hid = H5Tenum_create(AtomicType::type>{}.getId())) < 0) { - HDF5ErrMapper::ToException("Could not create new enum datatype"); - } + _hid = detail::h5t_enum_create(AtomicType::type>{}.getId()); // Loop over all the members and insert them into the datatype for (const auto& member: members) { - if (H5Tenum_insert(_hid, member.name.c_str(), &(member.value)) < 0) { - HDF5ErrMapper::ToException( - "Could not add new member to this enum datatype"); - } + detail::h5t_enum_insert(_hid, member.name.c_str(), &(member.value)); } } template inline void EnumType::commit(const Object& object, const std::string& name) const { - H5Tcommit2(object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + detail::h5t_commit2( + object.getId(), name.c_str(), getId(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } namespace { diff --git a/include/highfive/bits/H5Dataspace_misc.hpp b/include/highfive/bits/H5Dataspace_misc.hpp index 0fdcacefd..03fb4a950 100644 --- a/include/highfive/bits/H5Dataspace_misc.hpp +++ b/include/highfive/bits/H5Dataspace_misc.hpp @@ -17,6 +17,7 @@ #include "H5Utils.hpp" #include "H5Converter_misc.hpp" +#include "h5s_wrapper.hpp" namespace HighFive { @@ -38,9 +39,7 @@ template inline DataSpace::DataSpace(const IT begin, const IT end) { std::vector real_dims(begin, end); - if ((_hid = H5Screate_simple(int(real_dims.size()), real_dims.data(), NULL)) < 0) { - throw DataSpaceException("Impossible to create dataspace"); - } + _hid = detail::h5s_create_simple(int(real_dims.size()), real_dims.data(), nullptr); } inline DataSpace::DataSpace(const std::vector& dims, const std::vector& maxdims) { @@ -57,10 +56,8 @@ inline DataSpace::DataSpace(const std::vector& dims, const std::vector(DataSpace::UNLIMITED), H5S_UNLIMITED); - if ((_hid = H5Screate_simple(int(dims.size()), real_dims.data(), real_maxdims.data())) < 0) { - throw DataSpaceException("Impossible to create dataspace"); - } -} // namespace HighFive + _hid = detail::h5s_create_simple(int(dims.size()), real_dims.data(), real_maxdims.data()); +} inline DataSpace::DataSpace(DataSpace::DataspaceType space_type) { H5S_class_t h5_dataspace_type; @@ -77,53 +74,34 @@ inline DataSpace::DataSpace(DataSpace::DataspaceType space_type) { "dataspace_scalar or dataspace_null"); } - if ((_hid = H5Screate(h5_dataspace_type)) < 0) { - throw DataSpaceException("Unable to create dataspace"); - } + _hid = detail::h5s_create(h5_dataspace_type); } inline DataSpace DataSpace::clone() const { DataSpace res; - if ((res._hid = H5Scopy(_hid)) < 0) { - throw DataSpaceException("Unable to copy dataspace"); - } + res._hid = detail::h5s_copy(_hid); return res; } inline size_t DataSpace::getNumberDimensions() const { - const int ndim = H5Sget_simple_extent_ndims(_hid); - if (ndim < 0) { - HDF5ErrMapper::ToException( - "Unable to get dataspace number of dimensions"); - } - return size_t(ndim); + return static_cast(detail::h5s_get_simple_extent_ndims(_hid)); } inline std::vector DataSpace::getDimensions() const { std::vector dims(getNumberDimensions()); if (!dims.empty()) { - if (H5Sget_simple_extent_dims(_hid, dims.data(), NULL) < 0) { - HDF5ErrMapper::ToException("Unable to get dataspace dimensions"); - } + detail::h5s_get_simple_extent_dims(_hid, dims.data(), nullptr); } return details::to_vector_size_t(std::move(dims)); } inline size_t DataSpace::getElementCount() const { - hssize_t nelements = H5Sget_simple_extent_npoints(_hid); - if (nelements < 0) { - HDF5ErrMapper::ToException( - "Unable to get number of elements in dataspace"); - } - - return static_cast(nelements); + return static_cast(detail::h5s_get_simple_extent_npoints(_hid)); } inline std::vector DataSpace::getMaxDimensions() const { std::vector maxdims(getNumberDimensions()); - if (H5Sget_simple_extent_dims(_hid, NULL, maxdims.data()) < 0) { - HDF5ErrMapper::ToException("Unable to get dataspace dimensions"); - } + detail::h5s_get_simple_extent_dims(_hid, nullptr, maxdims.data()); std::replace(maxdims.begin(), maxdims.end(), diff --git a/include/highfive/bits/H5Exception_misc.hpp b/include/highfive/bits/H5Exception_misc.hpp index f7382f2c2..c8d61d407 100644 --- a/include/highfive/bits/H5Exception_misc.hpp +++ b/include/highfive/bits/H5Exception_misc.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include "h5e_wrapper.hpp" namespace HighFive { @@ -21,14 +21,14 @@ struct HDF5ErrMapper { auto** e_iter = static_cast(client_data); (void) n; - const char* major_err = H5Eget_major(err_desc->maj_num); - const char* minor_err = H5Eget_minor(err_desc->min_num); + const char* major_err = detail::nothrow::h5e_get_major(err_desc->maj_num); + const char* minor_err = detail::nothrow::h5e_get_minor(err_desc->min_num); std::ostringstream oss; oss << '(' << major_err << ") " << minor_err; - H5free_memory((void*) major_err); - H5free_memory((void*) minor_err); + detail::nothrow::h5_free_memory((void*) major_err); + detail::nothrow::h5_free_memory((void*) minor_err); auto* e = new ExceptionType(oss.str()); e->_err_major = err_desc->maj_num; @@ -45,8 +45,11 @@ struct HDF5ErrMapper { ExceptionType e(""); ExceptionType* e_iter = &e; - H5Ewalk2(err_stack, H5E_WALK_UPWARD, &HDF5ErrMapper::stackWalk, &e_iter); - H5Eclear2(err_stack); + detail::nothrow::h5e_walk2(err_stack, + H5E_WALK_UPWARD, + &HDF5ErrMapper::stackWalk, + &e_iter); + detail::nothrow::h5e_clear2(err_stack); const char* next_err_msg = (e.nextException() != NULL) ? (e.nextException()->what()) : (""); diff --git a/include/highfive/bits/H5File_misc.hpp b/include/highfive/bits/H5File_misc.hpp index b90792a71..52ae59516 100644 --- a/include/highfive/bits/H5File_misc.hpp +++ b/include/highfive/bits/H5File_misc.hpp @@ -14,6 +14,7 @@ #include "../H5Utility.hpp" #include "H5Utils.hpp" +#include "h5f_wrapper.hpp" namespace HighFive { @@ -62,7 +63,7 @@ inline File::File(const std::string& filename, if (openOrCreate) silencer.reset(new SilenceHDF5()); - _hid = H5Fopen(filename.c_str(), openMode, fileAccessProps.getId()); + _hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId()); if (isValid()) return; // Done @@ -78,15 +79,14 @@ inline File::File(const std::string& filename, auto fcpl = fileCreateProps.getId(); auto fapl = fileAccessProps.getId(); - if ((_hid = H5Fcreate(filename.c_str(), createMode, fcpl, fapl)) < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create file " + filename)); - } + _hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl); } inline const std::string& File::getName() const noexcept { if (_filename.empty()) { - _filename = details::get_name( - [this](char* buffer, size_t length) { return H5Fget_name(getId(), buffer, length); }); + _filename = details::get_name([this](char* buffer, size_t length) { + return detail::h5f_get_name(getId(), buffer, length); + }); } return _filename; } @@ -122,27 +122,17 @@ inline hsize_t File::getFileSpacePageSize() const { #endif inline void File::flush() { - if (H5Fflush(_hid, H5F_SCOPE_GLOBAL) < 0) { - HDF5ErrMapper::ToException(std::string("Unable to flush file " + getName())); - } + detail::h5f_flush(_hid, H5F_SCOPE_GLOBAL); } inline size_t File::getFileSize() const { hsize_t sizeValue = 0; - if (H5Fget_filesize(_hid, &sizeValue) < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to retrieve size of file " + getName())); - } + detail::h5f_get_filesize(_hid, &sizeValue); return static_cast(sizeValue); } inline size_t File::getFreeSpace() const { - hssize_t unusedSize = H5Fget_freespace(_hid); - if (unusedSize < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to retrieve unused space of file " + getName())); - } - return static_cast(unusedSize); + return static_cast(detail::h5f_get_freespace(_hid)); } } // namespace HighFive diff --git a/include/highfive/bits/H5Node_traits_misc.hpp b/include/highfive/bits/H5Node_traits_misc.hpp index 420842da2..229ebd433 100644 --- a/include/highfive/bits/H5Node_traits_misc.hpp +++ b/include/highfive/bits/H5Node_traits_misc.hpp @@ -12,9 +12,7 @@ #include #include -#include #include -#include #include #include @@ -27,6 +25,11 @@ #include "H5Selection_misc.hpp" #include "H5Slice_traits_misc.hpp" +#include "h5l_wrapper.hpp" +#include "h5g_wrapper.hpp" +#include "h5o_wrapper.hpp" + + namespace HighFive { @@ -39,18 +42,13 @@ inline DataSet NodeTraits::createDataSet(const std::string& dataset_na bool parents) { LinkCreateProps lcpl; lcpl.add(CreateIntermediateGroup(parents)); - const auto hid = H5Dcreate2(static_cast(this)->getId(), - dataset_name.c_str(), - dtype.getId(), - space.getId(), - lcpl.getId(), - createProps.getId(), - accessProps.getId()); - if (hid < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to create the dataset \"") + dataset_name + "\":"); - } - return DataSet(hid); + return DataSet(detail::h5d_create2(static_cast(this)->getId(), + dataset_name.c_str(), + dtype.getId(), + space.getId(), + lcpl.getId(), + createProps.getId(), + accessProps.getId())); } template @@ -119,30 +117,20 @@ inline DataSet NodeTraits::createDataSet(const std::string& dataset_na template inline DataSet NodeTraits::getDataSet(const std::string& dataset_name, const DataSetAccessProps& accessProps) const { - const auto hid = H5Dopen2(static_cast(this)->getId(), - dataset_name.c_str(), - accessProps.getId()); - if (hid < 0) { - HDF5ErrMapper::ToException(std::string("Unable to open the dataset \"") + - dataset_name + "\":"); - } - return DataSet(hid); + return DataSet(detail::h5d_open2(static_cast(this)->getId(), + dataset_name.c_str(), + accessProps.getId())); } template inline Group NodeTraits::createGroup(const std::string& group_name, bool parents) { LinkCreateProps lcpl; lcpl.add(CreateIntermediateGroup(parents)); - const auto hid = H5Gcreate2(static_cast(this)->getId(), - group_name.c_str(), - lcpl.getId(), - H5P_DEFAULT, - H5P_DEFAULT); - if (hid < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create the group \"") + - group_name + "\":"); - } - return detail::make_group(hid); + return detail::make_group(detail::h5g_create2(static_cast(this)->getId(), + group_name.c_str(), + lcpl.getId(), + H5P_DEFAULT, + H5P_DEFAULT)); } template @@ -151,63 +139,47 @@ inline Group NodeTraits::createGroup(const std::string& group_name, bool parents) { LinkCreateProps lcpl; lcpl.add(CreateIntermediateGroup(parents)); - const auto hid = H5Gcreate2(static_cast(this)->getId(), - group_name.c_str(), - lcpl.getId(), - createProps.getId(), - H5P_DEFAULT); - if (hid < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create the group \"") + - group_name + "\":"); - } - return detail::make_group(hid); + return detail::make_group(detail::h5g_create2(static_cast(this)->getId(), + group_name.c_str(), + lcpl.getId(), + createProps.getId(), + H5P_DEFAULT)); } template inline Group NodeTraits::getGroup(const std::string& group_name) const { - const auto hid = - H5Gopen2(static_cast(this)->getId(), group_name.c_str(), H5P_DEFAULT); - if (hid < 0) { - HDF5ErrMapper::ToException(std::string("Unable to open the group \"") + - group_name + "\":"); - } - return detail::make_group(hid); + return detail::make_group(detail::h5g_open2(static_cast(this)->getId(), + group_name.c_str(), + H5P_DEFAULT)); } template inline DataType NodeTraits::getDataType(const std::string& type_name, const DataTypeAccessProps& accessProps) const { - const auto hid = H5Topen2(static_cast(this)->getId(), - type_name.c_str(), - accessProps.getId()); - if (hid < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to open the datatype \"") + type_name + "\":"); - } - return DataType(hid); + return DataType(detail::h5t_open2(static_cast(this)->getId(), + type_name.c_str(), + accessProps.getId())); } template inline size_t NodeTraits::getNumberObjects() const { hsize_t res; - if (H5Gget_num_objs(static_cast(this)->getId(), &res) < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to count objects in existing group or file")); - } + detail::h5g_get_num_objs(static_cast(this)->getId(), &res); return static_cast(res); } template inline std::string NodeTraits::getObjectName(size_t index) const { return details::get_name([&](char* buffer, size_t length) { - return H5Lget_name_by_idx(static_cast(this)->getId(), - ".", - H5_INDEX_NAME, - H5_ITER_INC, - index, - buffer, - length, - H5P_DEFAULT); + return detail::h5l_get_name_by_idx( + static_cast(this)->getId(), + ".", + H5_INDEX_NAME, + H5_ITER_INC, + index, + buffer, + length, + H5P_DEFAULT); }); } @@ -217,18 +189,14 @@ inline bool NodeTraits::rename(const std::string& src_path, bool parents) const { LinkCreateProps lcpl; lcpl.add(CreateIntermediateGroup(parents)); - herr_t status = H5Lmove(static_cast(this)->getId(), - src_path.c_str(), - static_cast(this)->getId(), - dst_path.c_str(), - lcpl.getId(), - H5P_DEFAULT); - if (status < 0) { - HDF5ErrMapper::ToException(std::string("Unable to move link to \"") + - dst_path + "\":"); - return false; - } - return true; + herr_t err = detail::h5l_move(static_cast(this)->getId(), + src_path.c_str(), + static_cast(this)->getId(), + dst_path.c_str(), + lcpl.getId(), + H5P_DEFAULT); + + return err >= 0; } template @@ -239,23 +207,21 @@ inline std::vector NodeTraits::listObjectNames(IndexType size_t num_objs = getNumberObjects(); names.reserve(num_objs); - if (H5Literate(static_cast(this)->getId(), - static_cast(idx_type), - H5_ITER_INC, - NULL, - &details::internal_high_five_iterate, - static_cast(&iterateData)) < 0) { - HDF5ErrMapper::ToException(std::string("Unable to list objects in group")); - } - + detail::h5l_iterate(static_cast(this)->getId(), + static_cast(idx_type), + H5_ITER_INC, + NULL, + &details::internal_high_five_iterate, + static_cast(&iterateData)); return names; } template inline bool NodeTraits::_exist(const std::string& node_name, bool raise_errors) const { SilenceHDF5 silencer{}; - const auto val = - H5Lexists(static_cast(this)->getId(), node_name.c_str(), H5P_DEFAULT); + const auto val = detail::nothrow::h5l_exists(static_cast(this)->getId(), + node_name.c_str(), + H5P_DEFAULT); if (val < 0) { if (raise_errors) { HDF5ErrMapper::ToException("Invalid link for exist()"); @@ -285,11 +251,9 @@ inline bool NodeTraits::exist(const std::string& group_path) const { template inline void NodeTraits::unlink(const std::string& node_name) const { - const herr_t val = - H5Ldelete(static_cast(this)->getId(), node_name.c_str(), H5P_DEFAULT); - if (val < 0) { - HDF5ErrMapper::ToException(std::string("Invalid name for unlink() ")); - } + detail::h5l_delete(static_cast(this)->getId(), + node_name.c_str(), + H5P_DEFAULT); } @@ -313,13 +277,14 @@ static inline LinkType _convert_link_type(const H5L_type_t& ltype) noexcept { template inline LinkType NodeTraits::getLinkType(const std::string& node_name) const { H5L_info_t linkinfo; - if (H5Lget_info(static_cast(this)->getId(), - node_name.c_str(), - &linkinfo, - H5P_DEFAULT) < 0 || - linkinfo.type == H5L_TYPE_ERROR) { - HDF5ErrMapper::ToException(std::string("Unable to obtain info for link ") + - node_name); + detail::h5l_get_info(static_cast(this)->getId(), + node_name.c_str(), + &linkinfo, + H5P_DEFAULT); + + if (linkinfo.type == H5L_TYPE_ERROR) { + HDF5ErrMapper::ToException(std::string("Link type of \"") + node_name + + "\" is H5L_TYPE_ERROR"); } return _convert_link_type(linkinfo.type); } @@ -339,14 +304,11 @@ inline void NodeTraits::createSoftLink(const std::string& link_name, if (parents) { linkCreateProps.add(CreateIntermediateGroup{}); } - auto status = H5Lcreate_soft(obj_path.c_str(), - static_cast(this)->getId(), - link_name.c_str(), - linkCreateProps.getId(), - linkAccessProps.getId()); - if (status < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create soft link: ")); - } + detail::h5l_create_soft(obj_path.c_str(), + static_cast(this)->getId(), + link_name.c_str(), + linkCreateProps.getId(), + linkAccessProps.getId()); } @@ -360,15 +322,12 @@ inline void NodeTraits::createExternalLink(const std::string& link_nam if (parents) { linkCreateProps.add(CreateIntermediateGroup{}); } - auto status = H5Lcreate_external(h5_file.c_str(), - obj_path.c_str(), - static_cast(this)->getId(), - link_name.c_str(), - linkCreateProps.getId(), - linkAccessProps.getId()); - if (status < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create external link: ")); - } + detail::h5l_create_external(h5_file.c_str(), + obj_path.c_str(), + static_cast(this)->getId(), + link_name.c_str(), + linkCreateProps.getId(), + linkAccessProps.getId()); } template @@ -383,26 +342,20 @@ inline void NodeTraits::createHardLink(const std::string& link_name, if (parents) { linkCreateProps.add(CreateIntermediateGroup{}); } - auto status = H5Lcreate_hard(target_obj.getId(), - ".", - static_cast(this)->getId(), - link_name.c_str(), - linkCreateProps.getId(), - linkAccessProps.getId()); - if (status < 0) { - HDF5ErrMapper::ToException(std::string("Unable to create hard link: ")); - } + detail::h5l_create_hard(target_obj.getId(), + ".", + static_cast(this)->getId(), + link_name.c_str(), + linkCreateProps.getId(), + linkAccessProps.getId()); } template inline Object NodeTraits::_open(const std::string& node_name) const { - const auto id = - H5Oopen(static_cast(this)->getId(), node_name.c_str(), H5P_DEFAULT); - if (id < 0) { - HDF5ErrMapper::ToException(std::string("Unable to open \"") + node_name + - "\":"); - } + const auto id = detail::h5o_open(static_cast(this)->getId(), + node_name.c_str(), + H5P_DEFAULT); return detail::make_object(id); } diff --git a/include/highfive/bits/H5Object_misc.hpp b/include/highfive/bits/H5Object_misc.hpp index f477d7fdf..43dd421ae 100644 --- a/include/highfive/bits/H5Object_misc.hpp +++ b/include/highfive/bits/H5Object_misc.hpp @@ -12,6 +12,7 @@ #include "../H5Exception.hpp" #include "../H5Utility.hpp" +#include "h5i_wrapper.hpp" namespace HighFive { namespace detail { @@ -29,8 +30,8 @@ inline Object::Object(hid_t hid) inline Object::Object(const Object& other) : _hid(other._hid) { - if (other.isValid() && H5Iinc_ref(_hid) < 0) { - throw ObjectException("Reference counter increase failure"); + if (_hid > 0) { + detail::h5i_inc_ref(_hid); } } @@ -41,25 +42,28 @@ inline Object::Object(Object&& other) noexcept inline Object& Object::operator=(const Object& other) { if (this != &other) { - if (isValid()) - H5Idec_ref(_hid); + if (_hid > 0) { + detail::h5i_dec_ref(_hid); + } _hid = other._hid; - if (other.isValid() && H5Iinc_ref(_hid) < 0) { - throw ObjectException("Reference counter increase failure"); + if (_hid > 0) { + detail::h5i_inc_ref(_hid); } } return *this; } inline Object::~Object() { - if (isValid() && H5Idec_ref(_hid) < 0) { - HIGHFIVE_LOG_ERROR("HighFive::~Object: reference counter decrease failure"); + if (_hid > 0) { + if (detail::nothrow::h5i_dec_ref(_hid) < 0) { + HIGHFIVE_LOG_ERROR("Failed to decrease reference count of HID"); + } } } inline bool Object::isValid() const noexcept { - return (_hid != H5I_INVALID_HID) && (H5Iis_valid(_hid) != false); + return (_hid > 0) && (detail::h5i_is_valid(_hid) > 0); } inline hid_t Object::getId() const noexcept { @@ -87,11 +91,7 @@ static inline ObjectType _convert_object_type(const H5I_type_t& h5type) { inline ObjectType Object::getType() const { // H5Iget_type is a very lightweight func which extracts the type from the id - H5I_type_t h5type; - if ((h5type = H5Iget_type(_hid)) == H5I_BADID) { - HDF5ErrMapper::ToException("Invalid hid or object type"); - } - return _convert_object_type(h5type); + return _convert_object_type(detail::h5i_get_type(_hid)); } inline ObjectInfo Object::getInfo() const { diff --git a/include/highfive/bits/H5Path_traits_misc.hpp b/include/highfive/bits/H5Path_traits_misc.hpp index 444e9294b..acde06d1e 100644 --- a/include/highfive/bits/H5Path_traits_misc.hpp +++ b/include/highfive/bits/H5Path_traits_misc.hpp @@ -21,20 +21,16 @@ inline PathTraits::PathTraits() { std::is_same::value, "PathTraits can only be applied to Group, DataSet and Attribute"); const auto& obj = static_cast(*this); - if (!obj.isValid()) { - return; + if (obj.isValid()) { + const hid_t file_id = detail::h5i_get_file_id(obj.getId()); + _file_obj.reset(new File(file_id)); } - const hid_t file_id = H5Iget_file_id(obj.getId()); - if (file_id < 0) { - HDF5ErrMapper::ToException("getFile(): Could not obtain file of object"); - } - _file_obj.reset(new File(file_id)); } template inline std::string PathTraits::getPath() const { return details::get_name([this](char* buffer, size_t length) { - return H5Iget_name(static_cast(*this).getId(), buffer, length); + return detail::h5i_get_name(static_cast(*this).getId(), buffer, length); }); } diff --git a/include/highfive/bits/H5PropertyList_misc.hpp b/include/highfive/bits/H5PropertyList_misc.hpp index cef301e53..55aabe58f 100644 --- a/include/highfive/bits/H5PropertyList_misc.hpp +++ b/include/highfive/bits/H5PropertyList_misc.hpp @@ -8,7 +8,7 @@ */ #pragma once -#include +#include "h5p_wrapper.hpp" namespace HighFive { @@ -64,9 +64,7 @@ inline void PropertyList::_initializeIfNeeded() { if (_hid != H5P_DEFAULT) { return; } - if ((_hid = H5Pcreate(convert_plist_type(T))) < 0) { - HDF5ErrMapper::ToException("Unable to create property list"); - } + _hid = detail::h5p_create(convert_plist_type(T)); } template @@ -95,15 +93,11 @@ inline FileSpaceStrategy::FileSpaceStrategy(H5F_fspace_strategy_t strategy, , _threshold(threshold) {} inline FileSpaceStrategy::FileSpaceStrategy(const FileCreateProps& fcpl) { - if (H5Pget_file_space_strategy(fcpl.getId(), &_strategy, &_persist, &_threshold) < 0) { - HDF5ErrMapper::ToException("Unable to get file space strategy"); - } + detail::h5p_get_file_space_strategy(fcpl.getId(), &_strategy, &_persist, &_threshold); } inline void FileSpaceStrategy::apply(const hid_t list) const { - if (H5Pset_file_space_strategy(list, _strategy, _persist, _threshold) < 0) { - HDF5ErrMapper::ToException("Error setting file space strategy."); - } + detail::h5p_set_file_space_strategy(list, _strategy, _persist, _threshold); } inline H5F_fspace_strategy_t FileSpaceStrategy::getStrategy() const { @@ -122,15 +116,11 @@ inline FileSpacePageSize::FileSpacePageSize(hsize_t page_size) : _page_size(page_size) {} inline void FileSpacePageSize::apply(const hid_t list) const { - if (H5Pset_file_space_page_size(list, _page_size) < 0) { - HDF5ErrMapper::ToException("Error setting file space page size."); - } + detail::h5p_set_file_space_page_size(list, _page_size); } inline FileSpacePageSize::FileSpacePageSize(const FileCreateProps& fcpl) { - if (H5Pget_file_space_page_size(fcpl.getId(), &_page_size) < 0) { - HDF5ErrMapper::ToException("Unable to get file space page size"); - } + detail::h5p_get_file_space_page_size(fcpl.getId(), &_page_size); } inline hsize_t FileSpacePageSize::getPageSize() const { @@ -146,15 +136,11 @@ inline PageBufferSize::PageBufferSize(size_t page_buffer_size, , _min_raw(min_raw_percent) {} inline PageBufferSize::PageBufferSize(const FileAccessProps& plist) { - if (H5Pget_page_buffer_size(plist.getId(), &_page_buffer_size, &_min_meta, &_min_raw) < 0) { - HDF5ErrMapper::ToException("Error setting page buffer size."); - } + detail::h5p_get_page_buffer_size(plist.getId(), &_page_buffer_size, &_min_meta, &_min_raw); } inline void PageBufferSize::apply(const hid_t list) const { - if (H5Pset_page_buffer_size(list, _page_buffer_size, _min_meta, _min_raw) < 0) { - HDF5ErrMapper::ToException("Error setting page buffer size."); - } + detail::h5p_set_page_buffer_size(list, _page_buffer_size, _min_meta, _min_raw); } inline size_t PageBufferSize::getPageBufferSize() const { @@ -178,9 +164,7 @@ inline MPIOFileAccess::MPIOFileAccess(MPI_Comm comm, MPI_Info info) , _info(info) {} inline void MPIOFileAccess::apply(const hid_t list) const { - if (H5Pset_fapl_mpio(list, _comm, _info) < 0) { - HDF5ErrMapper::ToException("Unable to set-up MPIO Driver configuration"); - } + detail::h5p_set_fapl_mpio(list, _comm, _info); } inline void MPIOCollectiveMetadata::apply(const hid_t plist) const { @@ -210,9 +194,7 @@ inline bool MPIOCollectiveMetadata::isCollectiveWrite() const { inline void MPIOCollectiveMetadataRead::apply(const hid_t plist) const { - if (H5Pset_all_coll_metadata_ops(plist, collective_) < 0) { - HDF5ErrMapper::ToException("Unable to request collective metadata reads"); - } + detail::h5p_set_all_coll_metadata_ops(plist, collective_); } inline bool MPIOCollectiveMetadataRead::isCollective() const { @@ -220,18 +202,14 @@ inline bool MPIOCollectiveMetadataRead::isCollective() const { } inline MPIOCollectiveMetadataRead::MPIOCollectiveMetadataRead(const FileAccessProps& plist) { - if (H5Pget_all_coll_metadata_ops(plist.getId(), &collective_) < 0) { - HDF5ErrMapper::ToException("Error loading MPI metadata read."); - } + detail::h5p_get_all_coll_metadata_ops(plist.getId(), &collective_); } inline MPIOCollectiveMetadataRead::MPIOCollectiveMetadataRead(bool collective) : collective_(collective) {} inline void MPIOCollectiveMetadataWrite::apply(const hid_t plist) const { - if (H5Pset_coll_metadata_write(plist, collective_) < 0) { - HDF5ErrMapper::ToException("Unable to request collective metadata writes"); - } + detail::h5p_set_coll_metadata_write(plist, collective_); } inline bool MPIOCollectiveMetadataWrite::isCollective() const { @@ -239,9 +217,7 @@ inline bool MPIOCollectiveMetadataWrite::isCollective() const { } inline MPIOCollectiveMetadataWrite::MPIOCollectiveMetadataWrite(const FileAccessProps& plist) { - if (H5Pget_coll_metadata_write(plist.getId(), &collective_) < 0) { - HDF5ErrMapper::ToException("Error loading MPI metadata write."); - } + detail::h5p_get_coll_metadata_write(plist.getId(), &collective_); } inline MPIOCollectiveMetadataWrite::MPIOCollectiveMetadataWrite(bool collective) @@ -254,9 +230,7 @@ inline FileVersionBounds::FileVersionBounds(H5F_libver_t low, H5F_libver_t high) , _high(high) {} inline FileVersionBounds::FileVersionBounds(const FileAccessProps& fapl) { - if (H5Pget_libver_bounds(fapl.getId(), &_low, &_high) < 0) { - HDF5ErrMapper::ToException("Unable to access file version bounds"); - } + detail::h5p_get_libver_bounds(fapl.getId(), &_low, &_high); } inline std::pair FileVersionBounds::getVersion() const { @@ -264,24 +238,18 @@ inline std::pair FileVersionBounds::getVersion() con } inline void FileVersionBounds::apply(const hid_t list) const { - if (H5Pset_libver_bounds(list, _low, _high) < 0) { - HDF5ErrMapper::ToException("Error setting file version bounds"); - } + detail::h5p_set_libver_bounds(list, _low, _high); } inline MetadataBlockSize::MetadataBlockSize(hsize_t size) : _size(size) {} inline MetadataBlockSize::MetadataBlockSize(const FileAccessProps& fapl) { - if (H5Pget_meta_block_size(fapl.getId(), &_size) < 0) { - HDF5ErrMapper::ToException("Unable to access file metadata block size"); - } + detail::h5p_get_meta_block_size(fapl.getId(), &_size); } inline void MetadataBlockSize::apply(const hid_t list) const { - if (H5Pset_meta_block_size(list, _size) < 0) { - HDF5ErrMapper::ToException("Error setting metadata block size"); - } + detail::h5p_set_meta_block_size(list, _size); } inline hsize_t MetadataBlockSize::getSize() const { @@ -289,9 +257,7 @@ inline hsize_t MetadataBlockSize::getSize() const { } inline void EstimatedLinkInfo::apply(const hid_t hid) const { - if (H5Pset_est_link_info(hid, _entries, _length) < 0) { - HDF5ErrMapper::ToException("Error setting estimated link info"); - } + detail::h5p_set_est_link_info(hid, _entries, _length); } inline EstimatedLinkInfo::EstimatedLinkInfo(unsigned entries, unsigned length) @@ -299,9 +265,7 @@ inline EstimatedLinkInfo::EstimatedLinkInfo(unsigned entries, unsigned length) , _length(length) {} inline EstimatedLinkInfo::EstimatedLinkInfo(const GroupCreateProps& gcpl) { - if (H5Pget_est_link_info(gcpl.getId(), &_entries, &_length) < 0) { - HDF5ErrMapper::ToException("Unable to access group link size property"); - } + detail::h5p_get_est_link_info(gcpl.getId(), &_entries, &_length); } inline unsigned EstimatedLinkInfo::getEntries() const { @@ -313,9 +277,7 @@ inline unsigned EstimatedLinkInfo::getNameLength() const { } inline void Chunking::apply(const hid_t hid) const { - if (H5Pset_chunk(hid, static_cast(_dims.size()), _dims.data()) < 0) { - HDF5ErrMapper::ToException("Error setting chunk property"); - } + detail::h5p_set_chunk(hid, static_cast(_dims.size()), _dims.data()); } inline Chunking::Chunking(const std::vector& dims) @@ -326,10 +288,8 @@ inline Chunking::Chunking(const std::initializer_list& items) inline Chunking::Chunking(DataSetCreateProps& plist, size_t max_dims) : _dims(max_dims + 1) { - auto n_loaded = H5Pget_chunk(plist.getId(), static_cast(_dims.size()), _dims.data()); - if (n_loaded < 0) { - HDF5ErrMapper::ToException("Error getting chunk size"); - } + auto n_loaded = + detail::h5p_get_chunk(plist.getId(), static_cast(_dims.size()), _dims.data()); if (n_loaded >= static_cast(_dims.size())) { *this = Chunking(plist, 8 * max_dims); @@ -347,22 +307,22 @@ inline Chunking::Chunking(hsize_t item, Args... args) : Chunking(std::vector{item, static_cast(args)...}) {} inline void Deflate::apply(const hid_t hid) const { - if (!H5Zfilter_avail(H5Z_FILTER_DEFLATE) || H5Pset_deflate(hid, _level) < 0) { - HDF5ErrMapper::ToException("Error setting deflate property"); + if (detail::h5z_filter_avail(H5Z_FILTER_DEFLATE) == 0) { + HDF5ErrMapper::ToException("Deflate filter unavailable."); } + + detail::h5p_set_deflate(hid, _level); } inline Deflate::Deflate(unsigned int level) : _level(level) {} inline void Szip::apply(const hid_t hid) const { - if (!H5Zfilter_avail(H5Z_FILTER_SZIP)) { - HDF5ErrMapper::ToException("Error setting szip property"); + if (detail::h5z_filter_avail(H5Z_FILTER_SZIP) == 0) { + HDF5ErrMapper::ToException("SZIP filter unavailable."); } - if (H5Pset_szip(hid, _options_mask, _pixels_per_block) < 0) { - HDF5ErrMapper::ToException("Error setting szip property"); - } + detail::h5p_set_szip(hid, _options_mask, _pixels_per_block); } inline Szip::Szip(unsigned int options_mask, unsigned int pixels_per_block) @@ -378,28 +338,22 @@ inline unsigned Szip::getPixelsPerBlock() const { } inline void Shuffle::apply(const hid_t hid) const { - if (!H5Zfilter_avail(H5Z_FILTER_SHUFFLE)) { - HDF5ErrMapper::ToException("Error setting shuffle property"); + if (detail::h5z_filter_avail(H5Z_FILTER_SHUFFLE) == 0) { + HDF5ErrMapper::ToException("Shuffle filter unavailable."); } - if (H5Pset_shuffle(hid) < 0) { - HDF5ErrMapper::ToException("Error setting shuffle property"); - } + detail::h5p_set_shuffle(hid); } inline AllocationTime::AllocationTime(H5D_alloc_time_t alloc_time) : _alloc_time(alloc_time) {} inline AllocationTime::AllocationTime(const DataSetCreateProps& dcpl) { - if (H5Pget_alloc_time(dcpl.getId(), &_alloc_time) < 0) { - HDF5ErrMapper::ToException("Error getting allocation time"); - } + detail::h5p_get_alloc_time(dcpl.getId(), &_alloc_time); } inline void AllocationTime::apply(hid_t dcpl) const { - if (H5Pset_alloc_time(dcpl, _alloc_time) < 0) { - HDF5ErrMapper::ToException("Error setting allocation time"); - } + detail::h5p_set_alloc_time(dcpl, _alloc_time); } inline H5D_alloc_time_t AllocationTime::getAllocationTime() { @@ -407,15 +361,11 @@ inline H5D_alloc_time_t AllocationTime::getAllocationTime() { } inline Caching::Caching(const DataSetCreateProps& dcpl) { - if (H5Pget_chunk_cache(dcpl.getId(), &_numSlots, &_cacheSize, &_w0) < 0) { - HDF5ErrMapper::ToException("Error getting dataset cache parameters"); - } + detail::h5p_get_chunk_cache(dcpl.getId(), &_numSlots, &_cacheSize, &_w0); } inline void Caching::apply(const hid_t hid) const { - if (H5Pset_chunk_cache(hid, _numSlots, _cacheSize, _w0) < 0) { - HDF5ErrMapper::ToException("Error setting dataset cache parameters"); - } + detail::h5p_set_chunk_cache(hid, _numSlots, _cacheSize, _w0); } inline Caching::Caching(const size_t numSlots, const size_t cacheSize, const double w0) @@ -444,10 +394,7 @@ inline CreateIntermediateGroup::CreateIntermediateGroup(const ObjectCreateProps& inline void CreateIntermediateGroup::apply(const hid_t hid) const { - if (H5Pset_create_intermediate_group(hid, _create ? 1 : 0) < 0) { - HDF5ErrMapper::ToException( - "Error setting property for create intermediate groups"); - } + detail::h5p_set_create_intermediate_group(hid, _create ? 1 : 0); } inline CreateIntermediateGroup::CreateIntermediateGroup(const LinkCreateProps& lcpl) { @@ -456,12 +403,7 @@ inline CreateIntermediateGroup::CreateIntermediateGroup(const LinkCreateProps& l inline void CreateIntermediateGroup::fromPropertyList(hid_t hid) { unsigned c_bool = 0; - if (H5Pget_create_intermediate_group(hid, &c_bool) < 0) { - HDF5ErrMapper::ToException( - "Error getting property for create intermediate groups"); - } - - _create = bool(c_bool); + _create = bool(detail::h5p_get_create_intermediate_group(hid, &c_bool)); } inline bool CreateIntermediateGroup::isSet() const { @@ -473,17 +415,13 @@ inline UseCollectiveIO::UseCollectiveIO(bool enable) : _enable(enable) {} inline void UseCollectiveIO::apply(const hid_t hid) const { - if (H5Pset_dxpl_mpio(hid, _enable ? H5FD_MPIO_COLLECTIVE : H5FD_MPIO_INDEPENDENT) < 0) { - HDF5ErrMapper::ToException("Error setting H5Pset_dxpl_mpio."); - } + detail::h5p_set_dxpl_mpio(hid, _enable ? H5FD_MPIO_COLLECTIVE : H5FD_MPIO_INDEPENDENT); } inline UseCollectiveIO::UseCollectiveIO(const DataTransferProps& dxpl) { H5FD_mpio_xfer_t collective; - if (H5Pget_dxpl_mpio(dxpl.getId(), &collective) < 0) { - HDF5ErrMapper::ToException("Error getting H5Pset_dxpl_mpio."); - } + detail::h5p_get_dxpl_mpio(dxpl.getId(), &collective); if (collective != H5FD_MPIO_COLLECTIVE && collective != H5FD_MPIO_INDEPENDENT) { throw std::logic_error("H5Pget_dxpl_mpio returned something strange."); @@ -497,9 +435,7 @@ inline bool UseCollectiveIO::isCollective() const { } inline MpioNoCollectiveCause::MpioNoCollectiveCause(const DataTransferProps& dxpl) { - if (H5Pget_mpio_no_collective_cause(dxpl.getId(), &_local_cause, &_global_cause) < 0) { - HDF5ErrMapper::ToException("Failed to check mpio_no_collective_cause."); - } + detail::h5p_get_mpio_no_collective_cause(dxpl.getId(), &_local_cause, &_global_cause); } inline bool MpioNoCollectiveCause::wasCollective() const { @@ -532,16 +468,11 @@ inline unsigned LinkCreationOrder::getFlags() const { } inline void LinkCreationOrder::apply(const hid_t hid) const { - if (H5Pset_link_creation_order(hid, _flags) < 0) { - HDF5ErrMapper::ToException("Error setting LinkCreationOrder."); - } + detail::h5p_set_link_creation_order(hid, _flags); } inline void LinkCreationOrder::fromPropertyList(hid_t hid) { - if (H5Pget_link_creation_order(hid, &_flags) < 0) { - HDF5ErrMapper::ToException( - "Error getting property for link creation order"); - } + detail::h5p_get_link_creation_order(hid, &_flags); } inline AttributePhaseChange::AttributePhaseChange(unsigned max_compact, unsigned min_dense) @@ -549,10 +480,7 @@ inline AttributePhaseChange::AttributePhaseChange(unsigned max_compact, unsigned , _min_dense(min_dense) {} inline AttributePhaseChange::AttributePhaseChange(const GroupCreateProps& gcpl) { - if (H5Pget_attr_phase_change(gcpl.getId(), &_max_compact, &_min_dense) < 0) { - HDF5ErrMapper::ToException( - "Error getting property for attribute phase change"); - } + detail::h5p_get_attr_phase_change(gcpl.getId(), &_max_compact, &_min_dense); } inline unsigned AttributePhaseChange::max_compact() const { @@ -564,10 +492,7 @@ inline unsigned AttributePhaseChange::min_dense() const { } inline void AttributePhaseChange::apply(hid_t hid) const { - if (H5Pset_attr_phase_change(hid, _max_compact, _min_dense) < 0) { - HDF5ErrMapper::ToException( - "Error getting property for attribute phase change"); - } + detail::h5p_set_attr_phase_change(hid, _max_compact, _min_dense); } diff --git a/include/highfive/bits/H5ReadWrite_misc.hpp b/include/highfive/bits/H5ReadWrite_misc.hpp index c8e736174..984919262 100644 --- a/include/highfive/bits/H5ReadWrite_misc.hpp +++ b/include/highfive/bits/H5ReadWrite_misc.hpp @@ -76,15 +76,18 @@ inline void enforce_ascii_hack(const DataType& dst, const DataType& src) { // TEMP. CHANGE: Ensure that the character set is properly configured to prevent // converter issues on HDF5 <=v1.12.0 when loading ASCII strings first. // See https://github.com/HDFGroup/hdf5/issues/544 for further information. - if (H5Tget_cset(src.getId()) == H5T_CSET_ASCII) { - H5Tset_cset(dst.getId(), H5T_CSET_ASCII); + if (detail::h5t_get_class(dst.getId()) == H5T_STRING && + detail::h5t_get_class(dst.getId()) == H5T_STRING) { + if (detail::h5t_get_cset(src.getId()) == H5T_CSET_ASCII) { + detail::h5t_set_cset(dst.getId(), H5T_CSET_ASCII); + } } } template <> struct string_type_checker { inline static DataType getDataType(const DataType& element_type, const DataType& dtype) { - if (H5Tget_class(element_type.getId()) == H5T_STRING) { + if (detail::h5t_get_class(element_type.getId()) == H5T_STRING) { enforce_ascii_hack(element_type, dtype); } return element_type; diff --git a/include/highfive/bits/H5Reference_misc.hpp b/include/highfive/bits/H5Reference_misc.hpp index 7c8db36fb..c73deee2a 100644 --- a/include/highfive/bits/H5Reference_misc.hpp +++ b/include/highfive/bits/H5Reference_misc.hpp @@ -16,19 +16,19 @@ #include "../H5Object.hpp" +#include "h5r_wrapper.hpp" + namespace HighFive { inline Reference::Reference(const Object& location, const Object& object) : parent_id(location.getId()) { - obj_name = details::get_name( - [&](char* buffer, size_t length) { return H5Iget_name(object.getId(), buffer, length); }); + obj_name = details::get_name([&](char* buffer, size_t length) { + return detail::h5i_get_name(object.getId(), buffer, length); + }); } inline void Reference::create_ref(hobj_ref_t* refptr) const { - if (H5Rcreate(refptr, parent_id, obj_name.c_str(), H5R_OBJECT, -1) < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to create the reference for \"") + obj_name + "\":"); - } + detail::h5r_create(refptr, parent_id, obj_name.c_str(), H5R_OBJECT, -1); } inline ObjectType Reference::getType(const Object& location) const { @@ -51,15 +51,10 @@ inline T Reference::dereference(const Object& location) const { } inline Object Reference::get_ref(const Object& location) const { - hid_t res; #if (H5Rdereference_vers == 2) - if ((res = H5Rdereference(location.getId(), H5P_DEFAULT, H5R_OBJECT, &href)) < 0) { - HDF5ErrMapper::ToException("Unable to dereference."); - } + hid_t res = detail::h5r_dereference(location.getId(), H5P_DEFAULT, H5R_OBJECT, &href); #else - if ((res = H5Rdereference(location.getId(), H5R_OBJECT, &href)) < 0) { - HDF5ErrMapper::ToException("Unable to dereference."); - } + hid_t res = detail::h5r_dereference(location.getId(), H5R_OBJECT, &href); #endif return Object(res); } diff --git a/include/highfive/bits/H5Slice_traits.hpp b/include/highfive/bits/H5Slice_traits.hpp index 52c52713f..c753026c3 100644 --- a/include/highfive/bits/H5Slice_traits.hpp +++ b/include/highfive/bits/H5Slice_traits.hpp @@ -15,6 +15,7 @@ #include "H5Utils.hpp" #include "../H5PropertyList.hpp" +#include "h5s_wrapper.hpp" namespace HighFive { @@ -174,19 +175,14 @@ class HyperSlab { auto space = space_.clone(); for (const auto& sel: selects) { if (sel.op == Op::None) { - H5Sselect_none(space.getId()); + detail::h5s_select_none(space.getId()); } else { - auto error_code = - H5Sselect_hyperslab(space.getId(), - convert(sel.op), - sel.offset.empty() ? nullptr : sel.offset.data(), - sel.stride.empty() ? nullptr : sel.stride.data(), - sel.count.empty() ? nullptr : sel.count.data(), - sel.block.empty() ? nullptr : sel.block.data()); - - if (error_code < 0) { - HDF5ErrMapper::ToException("Unable to select hyperslab"); - } + detail::h5s_select_hyperslab(space.getId(), + convert(sel.op), + sel.offset.empty() ? nullptr : sel.offset.data(), + sel.stride.empty() ? nullptr : sel.stride.data(), + sel.count.empty() ? nullptr : sel.count.data(), + sel.block.empty() ? nullptr : sel.block.data()); } } return space; diff --git a/include/highfive/bits/H5Slice_traits_misc.hpp b/include/highfive/bits/H5Slice_traits_misc.hpp index 7b07c9abf..ae8d2f328 100644 --- a/include/highfive/bits/H5Slice_traits_misc.hpp +++ b/include/highfive/bits/H5Slice_traits_misc.hpp @@ -15,8 +15,8 @@ #include #include -#include -#include +#include "h5d_wrapper.hpp" +#include "h5p_wrapper.hpp" #include "H5ReadWrite_misc.hpp" #include "H5Converter_misc.hpp" @@ -84,7 +84,7 @@ inline Selection SliceTraits::select(const HyperSlab& hyper_slab) cons auto filespace = slice.getSpace(); filespace = hyper_slab.apply(filespace); - auto n_elements = H5Sget_select_npoints(filespace.getId()); + auto n_elements = detail::h5s_get_select_npoints(filespace.getId()); auto memspace = DataSpace(std::array{size_t(n_elements)}); return detail::make_selection(memspace, filespace, details::get_dataset(slice)); @@ -149,9 +149,7 @@ inline Selection SliceTraits::select(const ElementSet& elements) const data = raw_elements.data(); } - if (H5Sselect_elements(space.getId(), H5S_SELECT_SET, num_elements, data) < 0) { - HDF5ErrMapper::ToException("Unable to select elements"); - } + detail::h5s_select_elements(space.getId(), H5S_SELECT_SET, num_elements, data); return detail::make_selection(DataSpace(num_elements), space, details::get_dataset(slice)); } @@ -205,10 +203,14 @@ inline void SliceTraits::read(T& array, const DataTransferProps& xfer_ if (c == DataTypeClass::VarLen || t.isVariableStr()) { #if H5_VERSION_GE(1, 12, 0) // This one have been created in 1.12.0 - (void) H5Treclaim(t.getId(), mem_space.getId(), xfer_props.getId(), r.getPointer()); + (void) + detail::h5t_reclaim(t.getId(), mem_space.getId(), xfer_props.getId(), r.getPointer()); #else // This one is deprecated since 1.12.0 - (void) H5Dvlen_reclaim(t.getId(), mem_space.getId(), xfer_props.getId(), r.getPointer()); + (void) detail::h5d_vlen_reclaim(t.getId(), + mem_space.getId(), + xfer_props.getId(), + r.getPointer()); #endif } } @@ -224,14 +226,12 @@ inline void SliceTraits::read(T* array, const auto& slice = static_cast(*this); - if (H5Dread(details::get_dataset(slice).getId(), - mem_datatype.getId(), - details::get_memspace_id(slice), - slice.getSpace().getId(), - xfer_props.getId(), - static_cast(array)) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Read."); - } + detail::h5d_read(details::get_dataset(slice).getId(), + mem_datatype.getId(), + details::get_memspace_id(slice), + slice.getSpace().getId(), + xfer_props.getId(), + static_cast(array)); } template @@ -280,14 +280,12 @@ inline void SliceTraits::write_raw(const T* buffer, const DataTransferProps& xfer_props) { const auto& slice = static_cast(*this); - if (H5Dwrite(details::get_dataset(slice).getId(), - mem_datatype.getId(), - details::get_memspace_id(slice), - slice.getSpace().getId(), - xfer_props.getId(), - static_cast(buffer)) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Write: "); - } + detail::h5d_write(details::get_dataset(slice).getId(), + mem_datatype.getId(), + details::get_memspace_id(slice), + slice.getSpace().getId(), + xfer_props.getId(), + static_cast(buffer)); } template diff --git a/include/highfive/bits/h5_wrapper.hpp b/include/highfive/bits/h5_wrapper.hpp new file mode 100644 index 000000000..75ed9d8cb --- /dev/null +++ b/include/highfive/bits/h5_wrapper.hpp @@ -0,0 +1,12 @@ +#pragma once +#include + +namespace HighFive { +namespace detail { +void h5_free_memory(void* mem) { + if (H5free_memory(mem) < 0) { + throw DataTypeException("Could not free memory allocated by HDF5"); + } +} +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5a_wrapper.hpp b/include/highfive/bits/h5a_wrapper.hpp new file mode 100644 index 000000000..c77b4f72d --- /dev/null +++ b/include/highfive/bits/h5a_wrapper.hpp @@ -0,0 +1,131 @@ +#pragma once + +#include +#include + +namespace HighFive { +namespace detail { + +hid_t h5a_create2(hid_t loc_id, + char const* const attr_name, + hid_t type_id, + hid_t space_id, + hid_t acpl_id, + hid_t aapl_id) { + auto attr_id = H5Acreate2(loc_id, attr_name, type_id, space_id, acpl_id, aapl_id); + if (attr_id < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to create the attribute \"") + attr_name + "\":"); + } + + return attr_id; +} + +void h5a_delete(hid_t loc_id, char const* const attr_name) { + if (H5Adelete(loc_id, attr_name) < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to delete attribute \"") + attr_name + "\":"); + } +} + +hid_t h5a_open(hid_t loc_id, char const* const attr_name, hid_t aapl_id) { + const auto attr_id = H5Aopen(loc_id, attr_name, aapl_id); + if (attr_id < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to open the attribute \"") + attr_name + "\":"); + } + + return attr_id; +} + + +int h5a_get_num_attrs(hid_t loc_id) { + int res = H5Aget_num_attrs(loc_id); + if (res < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to count attributes in existing group or file")); + } + + return res; +} + + +void h5a_iterate2(hid_t loc_id, + H5_index_t idx_type, + H5_iter_order_t order, + hsize_t* idx, + H5A_operator2_t op, + void* op_data) { + if (H5Aiterate2(loc_id, idx_type, order, idx, op, op_data) < 0) { + HDF5ErrMapper::ToException(std::string("Failed H5Aiterate2.")); + } +} + +int h5a_exists(hid_t obj_id, char const* const attr_name) { + int res = H5Aexists(obj_id, attr_name); + if (res < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to check for attribute in group")); + } + + return res; +} + +ssize_t h5a_get_name(hid_t attr_id, size_t buf_size, char* buf) { + ssize_t name_length = H5Aget_name(attr_id, buf_size, buf); + if (name_length < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to get name of attribute")); + } + + return name_length; +} + + +hid_t h5a_get_space(hid_t attr_id) { + hid_t attr = H5Aget_space(attr_id); + if (attr < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to get dataspace of attribute")); + } + + return attr; +} + +hsize_t h5a_get_storage_size(hid_t attr_id) { + // Docs: + // Returns the amount of storage size allocated for the attribute; + // otherwise returns 0 (zero). + return H5Aget_storage_size(attr_id); +} + +hid_t h5a_get_type(hid_t attr_id) { + hid_t type_id = H5Aget_type(attr_id); + if (type_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to get datatype of attribute")); + } + + return type_id; +} + +herr_t h5a_read(hid_t attr_id, hid_t type_id, void* buf) { + herr_t err = H5Aread(attr_id, type_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to read attribute")); + } + + return err; +} + +herr_t h5a_write(hid_t attr_id, hid_t type_id, void const* buf) { + herr_t err = H5Awrite(attr_id, type_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to write attribute")); + } + + return err; +} + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5d_wrapper.hpp b/include/highfive/bits/h5d_wrapper.hpp new file mode 100644 index 000000000..8bf3c5245 --- /dev/null +++ b/include/highfive/bits/h5d_wrapper.hpp @@ -0,0 +1,123 @@ +#pragma once + +#include +#include + +namespace HighFive { +namespace detail { + + +herr_t h5d_vlen_reclaim(hid_t type_id, hid_t space_id, hid_t dxpl_id, void* buf) { + herr_t err = H5Dvlen_reclaim(type_id, space_id, dxpl_id, buf); + if (err < 0) { + throw DataSetException("Failed to reclaim HDF5 internal memory"); + } + + return err; +} + +hsize_t h5d_get_storage_size(hid_t dset_id) { + // Docs: + // H5Dget_storage_size() does not differentiate between 0 (zero), the + // value returned for the storage size of a dataset with no stored values, + // and 0 (zero), the value returned to indicate an error. + return H5Dget_storage_size(dset_id); +} + +hid_t h5d_get_space(hid_t dset_id) { + hid_t dset = H5Dget_space(dset_id); + if (dset == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to get dataspace of the dataset")); + } + + return dset; +} + +hid_t h5d_get_type(hid_t dset_id) { + hid_t type_id = H5Dget_type(dset_id); + if (type_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to get datatype of the dataset")); + } + + return type_id; +} + +herr_t h5d_read(hid_t dset_id, + hid_t mem_type_id, + hid_t mem_space_id, + hid_t file_space_id, + hid_t dxpl_id, + void* buf) { + herr_t err = H5Dread(dset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to read the dataset")); + } + + return err; +} + +herr_t h5d_write(hid_t dset_id, + hid_t mem_type_id, + hid_t mem_space_id, + hid_t file_space_id, + hid_t dxpl_id, + const void* buf) { + herr_t err = H5Dwrite(dset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to write the dataset")); + } + + return err; +} + +haddr_t h5d_get_offset(hid_t dset_id) { + uint64_t addr = H5Dget_offset(dset_id); + if (addr == HADDR_UNDEF) { + HDF5ErrMapper::ToException("Cannot get offset of DataSet."); + } + return addr; +} + + +herr_t h5d_set_extent(hid_t dset_id, const hsize_t size[]) { + herr_t err = H5Dset_extent(dset_id, size); + if (H5Dset_extent(dset_id, size) < 0) { + HDF5ErrMapper::ToException("Could not resize dataset."); + } + + return err; +} + +hid_t h5d_create2(hid_t loc_id, + const char* name, + hid_t type_id, + hid_t space_id, + hid_t lcpl_id, + hid_t dcpl_id, + hid_t dapl_id) { + hid_t dataset_id = H5Dcreate2(loc_id, name, type_id, space_id, lcpl_id, dcpl_id, dapl_id); + + if (dataset_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Failed to create the dataset \"") + name + "\":"); + } + + return dataset_id; +} + +hid_t h5d_open2(hid_t loc_id, const char* name, hid_t dapl_id) { + hid_t dataset_id = H5Dopen2(loc_id, name, dapl_id); + + if (dataset_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException(std::string("Unable to open the dataset \"") + + name + "\":"); + } + + return dataset_id; +} + + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5e_wrapper.hpp b/include/highfive/bits/h5e_wrapper.hpp new file mode 100644 index 000000000..de2ff4a5f --- /dev/null +++ b/include/highfive/bits/h5e_wrapper.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +namespace HighFive { +namespace detail { +namespace nothrow { + + +void h5e_get_auto2(hid_t estack_id, H5E_auto2_t* func, void** client_data) { + H5Eget_auto2(estack_id, func, client_data); +} + +void h5e_set_auto2(hid_t estack_id, H5E_auto2_t func, void* client_data) { + H5Eset_auto2(estack_id, func, client_data); +} + +char* h5e_get_major(H5E_major_t maj) { + return H5Eget_major(maj); +} + +char* h5e_get_minor(H5E_minor_t min) { + return H5Eget_minor(min); +} + +herr_t h5_free_memory(void* mem) { + return H5free_memory(mem); +} + +herr_t h5e_walk2(hid_t err_stack, H5E_direction_t direction, H5E_walk2_t func, void* client_data) { + return H5Ewalk2(err_stack, direction, func, client_data); +} + +herr_t h5e_clear2(hid_t err_stack) { + return H5Eclear2(err_stack); +} + + +} // namespace nothrow +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5f_wrapper.hpp b/include/highfive/bits/h5f_wrapper.hpp new file mode 100644 index 000000000..f66ce9192 --- /dev/null +++ b/include/highfive/bits/h5f_wrapper.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include +namespace HighFive { +namespace detail { +namespace nothrow { +hid_t h5f_open(const char* filename, unsigned flags, hid_t fapl_id) { + return H5Fopen(filename, flags, fapl_id); +} +} // namespace nothrow + +hid_t h5f_create(const char* filename, unsigned flags, hid_t fcpl_id, hid_t fapl_id) { + hid_t file_id = H5Fcreate(filename, flags, fcpl_id, fapl_id); + + if (file_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException(std::string("Failed to create file ") + filename); + } + return file_id; +} + +ssize_t h5f_get_name(hid_t obj_id, char* name, size_t size) { + ssize_t nread = H5Fget_name(obj_id, name, size); + if (nread < 0) { + HDF5ErrMapper::ToException(std::string("Failed to get file from id")); + } + + return nread; +} + +herr_t h5f_flush(hid_t object_id, H5F_scope_t scope) { + herr_t err = H5Fflush(object_id, scope); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Failed to flush file")); + } + + return err; +} + +herr_t h5f_get_filesize(hid_t file_id, hsize_t* size) { + herr_t err = H5Fget_filesize(file_id, size); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to retrieve size of file")); + } + + return err; +} + +hssize_t h5f_get_freespace(hid_t file_id) { + hssize_t free_space = H5Fget_freespace(file_id); + if (free_space < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to retrieve unused space of file ")); + } + return free_space; +} + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5g_wrapper.hpp b/include/highfive/bits/h5g_wrapper.hpp new file mode 100644 index 000000000..1bbe2722c --- /dev/null +++ b/include/highfive/bits/h5g_wrapper.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +#include + +namespace HighFive { +namespace detail { + +hid_t h5g_create2(hid_t loc_id, const char* name, hid_t lcpl_id, hid_t gcpl_id, hid_t gapl_id) { + hid_t group_id = H5Gcreate2(loc_id, name, lcpl_id, gcpl_id, gapl_id); + if (group_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException(std::string("Unable to create the group \"") + + name + "\":"); + } + + return group_id; +} + +hid_t h5g_open2(hid_t loc_id, const char* name, hid_t gapl_id) { + hid_t group_id = H5Gopen2(loc_id, name, gapl_id); + if (group_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException(std::string("Unable to open the group \"") + + name + "\":"); + } + return group_id; +} + +herr_t h5g_get_num_objs(hid_t loc_id, hsize_t* num_objs) { + herr_t err = H5Gget_num_objs(loc_id, num_objs); + if (err < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to count objects in existing group or file")); + } + + return err; +} + + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5i_wrapper.hpp b/include/highfive/bits/h5i_wrapper.hpp new file mode 100644 index 000000000..44f254956 --- /dev/null +++ b/include/highfive/bits/h5i_wrapper.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include + +namespace HighFive { +namespace detail { +int h5i_inc_ref(hid_t id) { + auto count = H5Iinc_ref(id); + + if (count < 0) { + throw ObjectException("Failed to increase reference count of HID"); + } + + return count; +} + +namespace nothrow { + +int h5i_dec_ref(hid_t id) { + return H5Idec_ref(id); +} + +} // namespace nothrow + +int h5i_dec_ref(hid_t id) { + int count = H5Idec_ref(id); + if (count < 0) { + throw ObjectException("Failed to decrease reference count of HID"); + } + + return count; +} + +namespace nothrow { +htri_t h5i_is_valid(hid_t id) { + return H5Iis_valid(id); +} + +} // namespace nothrow + +htri_t h5i_is_valid(hid_t id) { + htri_t tri = H5Iis_valid(id); + if (tri < 0) { + throw ObjectException("Failed to check if HID is valid"); + } + + return tri; +} + +H5I_type_t h5i_get_type(hid_t id) { + H5I_type_t type = H5Iget_type(id); + if (type == H5I_BADID) { + HDF5ErrMapper::ToException("Failed to get type of HID"); + } + + return type; +} + +template +hid_t h5i_get_file_id(hid_t id) { + hid_t file_id = H5Iget_file_id(id); + if (file_id < 0) { + HDF5ErrMapper::ToException("Failed not obtain file HID of object"); + } + + return file_id; +} + +ssize_t h5i_get_name(hid_t id, char* name, size_t size) { + ssize_t n_chars = H5Iget_name(id, name, size); + if (n_chars < 0) { + HDF5ErrMapper::ToException("Failed to get name of HID."); + } + + return n_chars; +} + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5l_wrapper.hpp b/include/highfive/bits/h5l_wrapper.hpp new file mode 100644 index 000000000..43ea06e13 --- /dev/null +++ b/include/highfive/bits/h5l_wrapper.hpp @@ -0,0 +1,141 @@ +#pragma once + +#include + +namespace HighFive { +namespace detail { + +template +herr_t h5l_create_external(const char* file_name, + const char* obj_name, + hid_t link_loc_id, + const char* link_name, + hid_t lcpl_id, + hid_t lapl_id) { + herr_t err = H5Lcreate_external(file_name, obj_name, link_loc_id, link_name, lcpl_id, lapl_id); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to create external link: ")); + } + + return err; +} + +template +herr_t h5l_create_soft(const char* link_target, + hid_t link_loc_id, + const char* link_name, + hid_t lcpl_id, + hid_t lapl_id) { + herr_t err = H5Lcreate_soft(link_target, link_loc_id, link_name, lcpl_id, lapl_id); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to create soft link: ")); + } + + return err; +} + +template +herr_t h5l_create_hard(hid_t cur_loc, + const char* cur_name, + hid_t dst_loc, + const char* dst_name, + hid_t lcpl_id, + hid_t lapl_id) { + herr_t err = H5Lcreate_hard(cur_loc, cur_name, dst_loc, dst_name, lcpl_id, lapl_id); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to create hard link: ")); + } + + return err; +} + +template +herr_t h5l_get_info(hid_t loc_id, const char* name, H5L_info_t* linfo, hid_t lapl_id) { + herr_t err = H5Lget_info(loc_id, name, linfo, lapl_id); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to obtain info for link ")); + } + + return err; +} + +template +herr_t h5l_delete(hid_t loc_id, const char* name, hid_t lapl_id) { + herr_t err = H5Ldelete(loc_id, name, lapl_id); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Invalid name for unlink() ")); + } + + return err; +} + +template +htri_t h5l_exists(hid_t loc_id, const char* name, hid_t lapl_id) { + htri_t tri = H5Lexists(loc_id, name, lapl_id); + if (tri < 0) { + HDF5ErrMapper::ToException("Invalid link for exist()"); + } + + return tri; +} + +namespace nothrow { + +htri_t h5l_exists(hid_t loc_id, const char* name, hid_t lapl_id) { + return H5Lexists(loc_id, name, lapl_id); +} + +} // namespace nothrow + +template +herr_t h5l_iterate(hid_t grp_id, + H5_index_t idx_type, + H5_iter_order_t order, + hsize_t* idx, + H5L_iterate_t op, + void* op_data) { + herr_t err = H5Literate(grp_id, idx_type, order, idx, op, op_data); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to list objects in group")); + } + return err; +} + +template +herr_t h5l_move(hid_t src_loc, + const char* src_name, + hid_t dst_loc, + const char* dst_name, + hid_t lcpl_id, + hid_t lapl_id) { + herr_t err = H5Lmove(src_loc, src_name, dst_loc, dst_name, lcpl_id, lapl_id); + + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to move link to \"") + dst_name + + "\":"); + } + return err; +} + +template +ssize_t h5l_get_name_by_idx(hid_t loc_id, + const char* group_name, + H5_index_t idx_type, + H5_iter_order_t order, + hsize_t n, + char* name, + size_t size, + hid_t lapl_id) { + ssize_t n_chars = + H5Lget_name_by_idx(loc_id, group_name, idx_type, order, n, name, size, lapl_id); + + if (n_chars < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to obtain link name from index.")); + } + + return n_chars; +} + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5o_wrapper.hpp b/include/highfive/bits/h5o_wrapper.hpp new file mode 100644 index 000000000..3cd51b817 --- /dev/null +++ b/include/highfive/bits/h5o_wrapper.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace HighFive { +namespace detail { + +template +hid_t h5o_open(hid_t loc_id, const char* name, hid_t lapl_id) { + hid_t hid = H5Oopen(loc_id, name, lapl_id); + if (hid < 0) { + HDF5ErrMapper::ToException(std::string("Unable to open \"") + name + "\":"); + } + + return hid; +} + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5p_wrapper.hpp b/include/highfive/bits/h5p_wrapper.hpp new file mode 100644 index 000000000..67f0b54e1 --- /dev/null +++ b/include/highfive/bits/h5p_wrapper.hpp @@ -0,0 +1,366 @@ +#pragma once + +#include +#include + +namespace HighFive { +namespace detail { +hid_t h5p_create(hid_t cls_id) { + hid_t plist_id = H5Pcreate(cls_id); + if (plist_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException("Failed to create property list"); + } + + return plist_id; +} + +herr_t h5p_set_file_space_strategy(hid_t plist_id, + H5F_fspace_strategy_t strategy, + hbool_t persist, + hsize_t threshold) { + herr_t err = H5Pset_file_space_strategy(plist_id, strategy, persist, threshold); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to get file space strategy"); + } + + return err; +} + +#if H5_VERSION_GE(1, 10, 1) +herr_t h5p_get_file_space_strategy(hid_t plist_id, + H5F_fspace_strategy_t* strategy, + hbool_t* persist, + hsize_t* threshold) { + herr_t err = H5Pget_file_space_strategy(plist_id, strategy, persist, threshold); + if (err) { + HDF5ErrMapper::ToException("Error setting file space strategy."); + } + + return err; +} + +herr_t h5p_set_file_space_page_size(hid_t plist_id, hsize_t fsp_size) { + herr_t err = H5Pset_file_space_page_size(plist_id, fsp_size); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting file space page size."); + } + + return err; +} + +herr_t h5p_get_file_space_page_size(hid_t plist_id, hsize_t* fsp_size) { + herr_t err = H5Pget_file_space_page_size(plist_id, fsp_size); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to get file space page size"); + } + + return err; +} + +#ifndef H5_HAVE_PARALLEL +herr_t h5p_get_page_buffer_size(hid_t plist_id, + size_t* buf_size, + unsigned* min_meta_perc, + unsigned* min_raw_perc) { + herr_t err = H5Pget_page_buffer_size(plist_id, buf_size, min_meta_perc, min_raw_perc); + + if (err < 0) { + HDF5ErrMapper::ToException("Error setting page buffer size."); + } + + return err; +} + +herr_t h5p_set_page_buffer_size(hid_t plist_id, + size_t buf_size, + unsigned min_meta_per, + unsigned min_raw_per) { + herr_t err = H5Pset_page_buffer_size(plist_id, buf_size, min_meta_per, min_raw_per); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting page buffer size."); + } + + return err; +} +#endif +#endif + +#ifdef H5_HAVE_PARALLEL +herr_t h5p_set_fapl_mpio(hid_t fapl_id, MPI_Comm comm, MPI_Info info) { + herr_t err = H5Pset_fapl_mpio(fapl_id, comm, info); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to set-up MPIO Driver configuration"); + } + + return err; +} + +herr_t h5p_set_all_coll_metadata_ops(hid_t plist_id, hbool_t is_collective) { + herr_t err = H5Pset_all_coll_metadata_ops(plist_id, is_collective); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to request collective metadata reads"); + } + + return err; +} + +herr_t h5p_get_all_coll_metadata_ops(hid_t plist_id, hbool_t* is_collective) { + herr_t err = H5Pget_all_coll_metadata_ops(plist_id, is_collective); + if (err < 0) { + HDF5ErrMapper::ToException("Error loading MPI metadata read."); + } + + return err; +} + +herr_t h5p_set_coll_metadata_write(hid_t plist_id, hbool_t is_collective) { + herr_t err = H5Pset_coll_metadata_write(plist_id, is_collective); + + if (err < 0) { + HDF5ErrMapper::ToException("Unable to request collective metadata writes"); + } + + return err; +} + +herr_t h5p_get_coll_metadata_write(hid_t plist_id, hbool_t* is_collective) { + herr_t err = H5Pget_coll_metadata_write(plist_id, is_collective); + + if (err < 0) { + HDF5ErrMapper::ToException("Error loading MPI metadata write."); + } + + return err; +} + +#endif + +herr_t h5p_get_libver_bounds(hid_t plist_id, H5F_libver_t* low, H5F_libver_t* high) { + herr_t err = H5Pget_libver_bounds(plist_id, low, high); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to access file version bounds"); + } + + return err; +} + +herr_t h5p_set_libver_bounds(hid_t plist_id, H5F_libver_t low, H5F_libver_t high) { + herr_t err = H5Pset_libver_bounds(plist_id, low, high); + + if (err < 0) { + HDF5ErrMapper::ToException("Error setting file version bounds"); + } + + return err; +} + +herr_t h5p_get_meta_block_size(hid_t fapl_id, hsize_t* size) { + herr_t err = H5Pget_meta_block_size(fapl_id, size); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to access file metadata block size"); + } + + return err; +} + +herr_t h5p_set_meta_block_size(hid_t fapl_id, hsize_t size) { + herr_t err = H5Pset_meta_block_size(fapl_id, size); + + if (err < 0) { + HDF5ErrMapper::ToException("Error setting metadata block size"); + } + + return err; +} + +herr_t h5p_set_est_link_info(hid_t plist_id, unsigned est_num_entries, unsigned est_name_len) { + herr_t err = H5Pset_est_link_info(plist_id, est_num_entries, est_name_len); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting estimated link info"); + } + + return err; +} + +herr_t h5p_get_est_link_info(hid_t plist_id, unsigned* est_num_entries, unsigned* est_name_len) { + herr_t err = H5Pget_est_link_info(plist_id, est_num_entries, est_name_len); + + if (err < 0) { + HDF5ErrMapper::ToException("Unable to access group link size property"); + } + + return err; +} + +herr_t h5p_set_chunk(hid_t plist_id, int ndims, const hsize_t dim[]) { + herr_t err = H5Pset_chunk(plist_id, ndims, dim); + + if (err < 0) { + HDF5ErrMapper::ToException("Error setting chunk property"); + } + + return err; +} + +int h5p_get_chunk(hid_t plist_id, int max_ndims, hsize_t dim[]) { + int chunk_dims = H5Pget_chunk(plist_id, max_ndims, dim); + if (chunk_dims < 0) { + HDF5ErrMapper::ToException("Error getting chunk size"); + } + return chunk_dims; +} + +htri_t h5z_filter_avail(H5Z_filter_t id) { + htri_t tri = H5Zfilter_avail(id); + if (tri < 0) { + HDF5ErrMapper::ToException("Error checking filter availability"); + } + return tri; +} + +herr_t h5p_set_deflate(hid_t plist_id, unsigned level) { + herr_t err = H5Pset_deflate(plist_id, level); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting deflate property"); + } + return err; +} + +herr_t h5p_set_szip(hid_t plist_id, unsigned options_mask, unsigned pixels_per_block) { + herr_t err = H5Pset_szip(plist_id, options_mask, pixels_per_block); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting szip property"); + } + return err; +} + +herr_t h5p_set_shuffle(hid_t plist_id) { + herr_t err = H5Pset_shuffle(plist_id); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting shuffle property"); + } + return err; +} + +herr_t h5p_get_alloc_time(hid_t plist_id, H5D_alloc_time_t* alloc_time) { + herr_t err = H5Pget_alloc_time(plist_id, alloc_time); + if (err < 0) { + HDF5ErrMapper::ToException("Error getting allocation time"); + } + return err; +} + +herr_t h5p_set_alloc_time(hid_t plist_id, H5D_alloc_time_t alloc_time) { + herr_t err = H5Pset_alloc_time(plist_id, alloc_time); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting allocation time"); + } + return err; +} + +herr_t h5p_get_chunk_cache(hid_t dapl_id, + size_t* rdcc_nslots, + size_t* rdcc_nbytes, + double* rdcc_w0) { + herr_t err = H5Pget_chunk_cache(dapl_id, rdcc_nslots, rdcc_nbytes, rdcc_w0); + if (err < 0) { + HDF5ErrMapper::ToException("Error getting dataset cache parameters"); + } + return err; +} + +herr_t h5p_set_chunk_cache(hid_t dapl_id, size_t rdcc_nslots, size_t rdcc_nbytes, double rdcc_w0) { + herr_t err = H5Pset_chunk_cache(dapl_id, rdcc_nslots, rdcc_nbytes, rdcc_w0); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting dataset cache parameters"); + } + return err; +} + +herr_t h5p_set_create_intermediate_group(hid_t plist_id, unsigned crt_intmd) { + herr_t err = H5Pset_create_intermediate_group(plist_id, crt_intmd); + if (err < 0) { + HDF5ErrMapper::ToException( + "Error setting property for create intermediate groups"); + } + return err; +} + +herr_t h5p_get_create_intermediate_group(hid_t plist_id, unsigned* crt_intmd) { + herr_t err = H5Pget_create_intermediate_group(plist_id, crt_intmd); + if (err < 0) { + HDF5ErrMapper::ToException( + "Error getting property for create intermediate groups"); + } + return err; +} + +#ifdef H5_HAVE_PARALLEL +herr_t h5p_set_dxpl_mpio(hid_t dxpl_id, H5FD_mpio_xfer_t xfer_mode) { + herr_t err = H5Pset_dxpl_mpio(dxpl_id, xfer_mode); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting H5Pset_dxpl_mpio."); + } + return err; +} + +herr_t h5p_get_dxpl_mpio(hid_t dxpl_id, H5FD_mpio_xfer_t* xfer_mode) { + herr_t err = H5Pget_dxpl_mpio(dxpl_id, xfer_mode); + if (err < 0) { + HDF5ErrMapper::ToException("Error getting H5Pset_dxpl_mpio."); + } + return err; +} + +herr_t h5p_get_mpio_no_collective_cause(hid_t plist_id, + uint32_t* local_no_collective_cause, + uint32_t* global_no_collective_cause) { + herr_t err = H5Pget_mpio_no_collective_cause(plist_id, + local_no_collective_cause, + global_no_collective_cause); + if (err < 0) { + HDF5ErrMapper::ToException("Failed to check mpio_no_collective_cause."); + } + return err; +} + +#endif + +herr_t h5p_set_link_creation_order(hid_t plist_id, unsigned crt_order_flags) { + herr_t err = H5Pset_link_creation_order(plist_id, crt_order_flags); + if (err < 0) { + HDF5ErrMapper::ToException("Error setting LinkCreationOrder."); + } + return err; +} + +herr_t h5p_get_link_creation_order(hid_t plist_id, unsigned* crt_order_flags) { + herr_t err = H5Pget_link_creation_order(plist_id, crt_order_flags); + if (err < 0) { + HDF5ErrMapper::ToException( + "Error getting property for link creation order"); + } + return err; +} + +herr_t h5p_get_attr_phase_change(hid_t plist_id, unsigned* max_compact, unsigned* min_dense) { + herr_t err = H5Pget_attr_phase_change(plist_id, max_compact, min_dense); + if (err < 0) { + HDF5ErrMapper::ToException( + "Error getting property for attribute phase change"); + } + return err; +} + +herr_t h5p_set_attr_phase_change(hid_t plist_id, unsigned max_compact, unsigned min_dense) { + herr_t err = H5Pset_attr_phase_change(plist_id, max_compact, min_dense); + if (err < 0) { + HDF5ErrMapper::ToException( + "Error getting property for attribute phase change"); + } + return err; +} + + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5r_wrapper.hpp b/include/highfive/bits/h5r_wrapper.hpp new file mode 100644 index 000000000..2f4277bcb --- /dev/null +++ b/include/highfive/bits/h5r_wrapper.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include + +namespace HighFive { +namespace detail { +herr_t h5r_create(void* ref, hid_t loc_id, const char* name, H5R_type_t ref_type, hid_t space_id) { + herr_t err = H5Rcreate(ref, loc_id, name, ref_type, space_id); + if (err < 0) { + HDF5ErrMapper::ToException( + std::string("Unable to create the reference for \"") + name + "\":"); + } + + return err; +} + +#if (H5Rdereference_vers == 2) +hid_t h5r_dereference(hid_t obj_id, hid_t oapl_id, H5R_type_t ref_type, const void* ref) { + hid_t hid = H5Rdereference(obj_id, oapl_id, ref_type, ref); + if (hid < 0) { + HDF5ErrMapper::ToException("Unable to dereference."); + } + + return hid; +} +#else +hid_t h5r_dereference(hid_t dataset, H5R_type_t ref_type, const void* ref) { + hid_t hid = H5Rdereference(dataset, ref_type, ref); + if (hid < 0) { + HDF5ErrMapper::ToException("Unable to dereference."); + } + + return hid; +} +#endif + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5s_wrapper.hpp b/include/highfive/bits/h5s_wrapper.hpp new file mode 100644 index 000000000..3d7a9eddf --- /dev/null +++ b/include/highfive/bits/h5s_wrapper.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include +namespace HighFive { +namespace detail { + +hid_t h5s_create_simple(int rank, const hsize_t dims[], const hsize_t maxdims[]) { + hid_t space_id = H5Screate_simple(rank, dims, maxdims); + if (space_id < H5I_INVALID_HID) { + throw DataSpaceException("Unable to create simple dataspace"); + } + + return space_id; +} + +hid_t h5s_create(H5S_class_t type) { + hid_t space_id = H5Screate(type); + + if (space_id < H5I_INVALID_HID) { + throw DataSpaceException("Unable to create dataspace"); + } + + return space_id; +} + +hid_t h5s_copy(hid_t space_id) { + hid_t copy_id = H5Scopy(space_id); + + if (copy_id < 0) { + throw DataSpaceException("Unable to copy dataspace"); + } + + return copy_id; +} + +herr_t h5s_select_none(hid_t spaceid) { + herr_t err = H5Sselect_none(spaceid); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to select None space"); + } + return err; +} + +herr_t h5s_select_hyperslab(hid_t space_id, + H5S_seloper_t op, + const hsize_t start[], + const hsize_t stride[], + const hsize_t count[], + const hsize_t block[]) { + herr_t err = H5Sselect_hyperslab(space_id, op, start, stride, count, block); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to select hyperslab"); + } + return err; +} + +hssize_t h5s_get_select_npoints(hid_t spaceid) { + hssize_t n_points = H5Sget_select_npoints(spaceid); + if (n_points < 0) { + HDF5ErrMapper::ToException( + "Unable to get number of points in selection"); + } + return n_points; +} + +herr_t h5s_select_elements(hid_t space_id, + H5S_seloper_t op, + size_t num_elem, + const hsize_t* coord) { + herr_t err = H5Sselect_elements(space_id, op, num_elem, coord); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to select elements"); + } + return err; +} + +int h5s_get_simple_extent_ndims(hid_t space_id) { + int ndim = H5Sget_simple_extent_ndims(space_id); + if (ndim < 0) { + HDF5ErrMapper::ToException( + "Unable to get number of dimensions of dataspace"); + } + return ndim; +} + +herr_t h5s_get_simple_extent_dims(hid_t space_id, hsize_t dims[], hsize_t maxdims[]) { + herr_t err = H5Sget_simple_extent_dims(space_id, dims, maxdims); + if (err < 0) { + HDF5ErrMapper::ToException("Unable to get dimensions of dataspace"); + } + return err; +} + +hssize_t h5s_get_simple_extent_npoints(hid_t space_id) { + hssize_t nelements = H5Sget_simple_extent_npoints(space_id); + if (nelements < 0) { + HDF5ErrMapper::ToException( + "Unable to get number of elements in dataspace"); + } + + return nelements; +} + + +} // namespace detail +} // namespace HighFive diff --git a/include/highfive/bits/h5t_wrapper.hpp b/include/highfive/bits/h5t_wrapper.hpp index 5e5371d13..7f55527d9 100644 --- a/include/highfive/bits/h5t_wrapper.hpp +++ b/include/highfive/bits/h5t_wrapper.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace HighFive { @@ -59,5 +60,180 @@ inline void h5t_set_strpad(hid_t hid, H5T_str_t strpad) { } } +inline int h5t_get_nmembers(hid_t hid) { + auto result = H5Tget_nmembers(hid); + + if (result < 0) { + throw DataTypeException("Could not get members of compound datatype"); + } + + return result; +} + +char* h5t_get_member_name(hid_t type_id, unsigned membno) { + char* name = H5Tget_member_name(type_id, membno); + if (name == nullptr) { + throw DataTypeException("Failed to get member names of compound datatype"); + } + + return name; +} + + +size_t h5t_get_member_offset(hid_t type_id, unsigned membno) { + // Note, this function is peculiar. On failure it returns 0, yet 0 is also + // what's returned on failure. + return H5Tget_member_offset(type_id, membno); +} + +hid_t h5t_get_member_type(hid_t type_id, unsigned membno) { + hid_t member_id = H5Tget_member_type(type_id, membno); + + if (member_id < 0) { + throw DataTypeException("Failed to get member type of compound datatype"); + } + + return member_id; +} + +#if H5_VERSION_GE(1, 12, 0) +herr_t h5t_reclaim(hid_t type_id, hid_t space_id, hid_t plist_id, void* buf) { + herr_t err = H5Treclaim(type_id, space_id, plist_id, buf); + if (err < 0) { + throw DataTypeException("Failed to reclaim HDF5 internal memory"); + } + + return err; +} +#else +herr_t h5d_vlen_reclaim(hid_t type_id, hid_t space_id, hid_t dxpl_id, void* buf) { + herr_t err = H5Dvlen_reclaim(type_id, space_id, dxpl_id, buf); + if (err < 0) { + throw DataTypeException("Failed to reclaim HDF5 internal memory"); + } + return err; +} +#endif + +H5T_class_t h5t_get_class(hid_t type_id) { + H5T_class_t class_id = H5Tget_class(type_id); + if (class_id == H5T_NO_CLASS) { + throw DataTypeException("Failed to get class of type"); + } + + return class_id; +} + +htri_t h5t_equal(hid_t type1_id, hid_t type2_id) { + htri_t equal = H5Tequal(type1_id, type2_id); + if (equal < 0) { + throw DataTypeException("Failed to compare two datatypes"); + } + + return equal; +} + +htri_t h5t_is_variable_str(hid_t type_id) { + htri_t is_variable = H5Tis_variable_str(type_id); + if (is_variable < 0) { + HDF5ErrMapper::ToException( + "Failed to check if string is variable length"); + } + return is_variable; +} + +herr_t h5t_set_fields(hid_t type_id, + size_t spos, + size_t epos, + size_t esize, + size_t mpos, + size_t msize) { + herr_t err = H5Tset_fields(type_id, spos, epos, esize, mpos, msize); + if (err < 0) { + HDF5ErrMapper::ToException( + "Failed to create custom floating point data type"); + } + return err; } + +herr_t h5t_set_ebias(hid_t type_id, size_t ebias) { + herr_t err = H5Tset_ebias(type_id, ebias); + if (err < 0) { + HDF5ErrMapper::ToException( + "Failed to exponent bias of floating point data type"); + } + + return err; +} + +hid_t h5t_create(H5T_class_t type, size_t size) { + hid_t type_id = H5Tcreate(type, size); + if (type_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException("Failed to datatype"); + } + + return type_id; +} + +herr_t h5t_insert(hid_t parent_id, const char* name, size_t offset, hid_t member_id) { + herr_t err = H5Tinsert(parent_id, name, offset, member_id); + if (err < 0) { + HDF5ErrMapper::ToException("Failed to not add new member to datatype"); + } + + return err; +} + +herr_t h5t_commit2(hid_t loc_id, + const char* name, + hid_t type_id, + hid_t lcpl_id, + hid_t tcpl_id, + hid_t tapl_id) { + herr_t err = H5Tcommit2(loc_id, name, type_id, lcpl_id, tcpl_id, tapl_id); + if (err < 0) { + HDF5ErrMapper::ToException("Failed to commit datatype"); + } + + return err; } + +herr_t h5t_close(hid_t type_id) { + auto err = H5Tclose(type_id); + if (err < 0) { + HDF5ErrMapper::ToException("Failed to close datatype"); + } + + return err; +} + +hid_t h5t_enum_create(hid_t base_id) { + hid_t type_id = H5Tenum_create(base_id); + if (type_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException("Failed to create new enum datatype"); + } + return type_id; +} + +herr_t h5t_enum_insert(hid_t type, const char* name, const void* value) { + herr_t err = H5Tenum_insert(type, name, value); + if (err < 0) { + HDF5ErrMapper::ToException( + "Failed to add new member to this enum datatype"); + } + return err; +} + +hid_t h5t_open2(hid_t loc_id, const char* name, hid_t tapl_id) { + hid_t datatype_id = H5Topen2(loc_id, name, tapl_id); + if (datatype_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to open the datatype \"") + name + "\":"); + } + + return datatype_id; +} + + +} // namespace detail +} // namespace HighFive