Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap all used H5T functions. #883

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions include/highfive/H5DataType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "bits/string_padding.hpp"
#include "H5PropertyList.hpp"

#include "bits/h5_wrapper.hpp"
#include "bits/h5t_wrapper.hpp"

namespace HighFive {


Expand Down Expand Up @@ -235,21 +238,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<size_t>(result);
size_t n_members = static_cast<size_t>(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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/highfive/bits/H5Attribute_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ 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());
Expand Down
50 changes: 19 additions & 31 deletions include/highfive/bits/H5DataType_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#endif

#include <H5Ppublic.h>
#include <H5Tpublic.h>

#ifdef H5_USE_HALF_FLOAT
#include <half.hpp>
Expand All @@ -38,35 +37,31 @@ 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 {
return detail::h5t_get_size(_hid);
}

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 {
return !(*this == other);
}

inline bool DataType::isVariableStr() const {
auto var_value = H5Tis_variable_str(_hid);
if (var_value < 0) {
HDF5ErrMapper::ToException<DataTypeException>("Unable to define datatype size to variable");
}
return static_cast<bool>(var_value);
return detail::h5t_is_variable_str(_hid) > 0;
}

inline bool DataType::isFixedLenStr() const {
return getClass() == DataTypeClass::String && !isVariableStr();
}

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 {
Expand Down Expand Up @@ -183,11 +178,11 @@ template <>
inline AtomicType<float16_t>::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

Expand Down Expand Up @@ -316,8 +311,8 @@ inline AtomicType<Reference>::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));
Expand All @@ -327,11 +322,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);
Expand Down Expand Up @@ -391,43 +386,36 @@ inline void CompoundType::create(size_t size) {
}

// Create the HDF5 type
if ((_hid = H5Tcreate(H5T_COMPOUND, size)) < 0) {
HDF5ErrMapper::ToException<DataTypeException>("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<DataTypeException>("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 <typename T>
inline void EnumType<T>::create() {
// Create the HDF5 type
if ((_hid = H5Tenum_create(AtomicType<typename std::underlying_type<T>::type>{}.getId())) < 0) {
HDF5ErrMapper::ToException<DataTypeException>("Could not create new enum datatype");
}
_hid = detail::h5t_enum_create(AtomicType<typename std::underlying_type<T>::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<DataTypeException>(
"Could not add new member to this enum datatype");
}
detail::h5t_enum_insert(_hid, member.name.c_str(), &(member.value));
}
}

template <typename T>
inline void EnumType<T>::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 {
Expand Down
11 changes: 3 additions & 8 deletions include/highfive/bits/H5Node_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,9 @@ inline Group NodeTraits<Derivate>::getGroup(const std::string& group_name) const
template <typename Derivate>
inline DataType NodeTraits<Derivate>::getDataType(const std::string& type_name,
const DataTypeAccessProps& accessProps) const {
const auto hid = H5Topen2(static_cast<const Derivate*>(this)->getId(),
type_name.c_str(),
accessProps.getId());
if (hid < 0) {
HDF5ErrMapper::ToException<DataTypeException>(
std::string("Unable to open the datatype \"") + type_name + "\":");
}
return DataType(hid);
return DataType(detail::h5t_open2(static_cast<const Derivate*>(this)->getId(),
type_name.c_str(),
accessProps.getId()));
}

template <typename Derivate>
Expand Down
3 changes: 2 additions & 1 deletion include/highfive/bits/H5Slice_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ inline void SliceTraits<Derivate>::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());
Expand Down
12 changes: 12 additions & 0 deletions include/highfive/bits/h5_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <H5public.h>

namespace HighFive {
namespace detail {
inline void h5_free_memory(void* mem) {
if (H5free_memory(mem) < 0) {
throw DataTypeException("Could not free memory allocated by HDF5");

Check warning on line 8 in include/highfive/bits/h5_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5_wrapper.hpp#L8

Added line #L8 was not covered by tests
}
}
} // namespace detail
} // namespace HighFive
Loading