Skip to content

Commit

Permalink
Add a function to get property list from object (#629)
Browse files Browse the repository at this point in the history
* Add a function to get property list from object

* Format

* Check format

* Rewrite

* Rewrite

* Revert "Check format"

This reverts commit aa381aa.

* Format

* Avoid nullptr

* Fix protected object

* Add small doc

* put get_plist in details (don't use it)
  • Loading branch information
alkino committed Nov 3, 2022
1 parent c603bd9 commit 1cf20ee
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 42 deletions.
18 changes: 10 additions & 8 deletions include/highfive/H5Attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <vector>

#include "H5Apublic.h"

#include "H5DataSpace.hpp"
#include "H5DataType.hpp"
#include "H5Object.hpp"
Expand Down Expand Up @@ -51,8 +53,7 @@ class Attribute: public Object, public PathTraits<Attribute> {
///
DataSpace getMemSpace() const;

///
/// Return the attribute
/// \brief Return the attribute
template <typename T>
T read() const;

Expand All @@ -66,9 +67,7 @@ class Attribute: public Object, public PathTraits<Attribute> {
template <typename T>
void read(T& array) const;

///
/// Read the attribute into a buffer
///
/// \brief Read the attribute into a buffer
template <typename T>
void read(T* array, const DataType& dtype = DataType()) const;

Expand All @@ -82,12 +81,15 @@ class Attribute: public Object, public PathTraits<Attribute> {
template <typename T>
void write(const T& buffer);

///
/// Write a buffer to this attribute
///
/// \brief Write a buffer to this attribute
template <typename T>
void write_raw(const T* buffer, const DataType& dtype = DataType());

/// \brief Get the list of properties for creation of this attribute
AttributeCreateProps getCreatePropertyList() const {
return details::get_plist<AttributeCreateProps>(*this, H5Aget_create_plist);
}

// No empty attributes
Attribute() = delete;

Expand Down
10 changes: 10 additions & 0 deletions include/highfive/H5DataSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ class DataSet: public Object,
return getSpace().getElementCount();
}

/// \brief Get the list of properties for creation of this dataset
DataSetCreateProps getCreatePropertyList() const {
return details::get_plist<DataSetCreateProps>(*this, H5Dget_create_plist);
}

/// \brief Get the list of properties for accession of this dataset
DataSetAccessProps getAccessPropertyList() const {
return details::get_plist<DataSetAccessProps>(*this, H5Dget_access_plist);
}

H5_DEPRECATED("Default constructor creates unsafe uninitialized objects")
DataSet() = default;

Expand Down
7 changes: 7 additions & 0 deletions include/highfive/H5DataType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "H5Object.hpp"
#include "bits/H5Utils.hpp"

#include "H5PropertyList.hpp"

namespace HighFive {


Expand Down Expand Up @@ -82,6 +84,11 @@ class DataType: public Object {
/// \brief Returns whether the type is a Reference
bool isReference() const;

/// \brief Get the list of properties for creation of this DataType
DataTypeCreateProps getCreatePropertyList() const {
return details::get_plist<DataTypeCreateProps>(*this, H5Tget_create_plist);
}

protected:
using Object::Object;

Expand Down
13 changes: 10 additions & 3 deletions include/highfive/H5File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "H5FileDriver.hpp"
#include "H5Object.hpp"
#include "H5PropertyList.hpp"
#include "bits/H5Annotate_traits.hpp"
#include "bits/H5Node_traits.hpp"

Expand Down Expand Up @@ -100,9 +101,15 @@ class File: public Object, public NodeTraits<File>, public AnnotateTraits<File>
///
void flush();

protected:
hid_t getAccessPList() const;
hid_t getCreatePList() const;
/// \brief Get the list of properties for creation of this file
FileCreateProps getCreatePropertyList() const {
return details::get_plist<FileCreateProps>(*this, H5Fget_create_plist);
}

/// \brief Get the list of properties for accession of this file
FileAccessProps getAccessPropertyList() const {
return details::get_plist<FileAccessProps>(*this, H5Fget_access_plist);
}

private:
using Object::Object;
Expand Down
9 changes: 7 additions & 2 deletions include/highfive/H5Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Group: public Object,

std::pair<unsigned int, unsigned int> getEstimatedLinkInfo() const;

/// \brief Get the list of properties for creation of this group
GroupCreateProps getCreatePropertyList() const {
return details::get_plist<GroupCreateProps>(*this, H5Gget_create_plist);
}

protected:
using Object::Object;

Expand All @@ -49,8 +54,8 @@ inline std::pair<unsigned int, unsigned int> Group::getEstimatedLinkInfo() const
unsigned int est_num_entries;
unsigned int est_name_len;

auto gid_gcpl = H5Gget_create_plist(getId());
if (H5Pget_est_link_info(gid_gcpl, &est_num_entries, &est_name_len) < 0) {
auto gcpl = getCreatePropertyList();
if (H5Pget_est_link_info(gcpl.getId(), &est_num_entries, &est_name_len) < 0) {
HDF5ErrMapper::ToException<GroupException>(
std::string("Unable to access group link size property"));
}
Expand Down
17 changes: 16 additions & 1 deletion include/highfive/H5PropertyList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ enum class PropertyType : int {
LINK_ACCESS,
};

namespace details {
template <typename T, typename U>
T get_plist(const U& obj, hid_t (*f)(hid_t)) {
auto hid = f(obj.getId());
if (hid < 0) {
HDF5ErrMapper::ToException<PropertyException>(std::string("Unable to get property list"));
}
T t{};
t._hid = hid;
return t;
}
} // namespace details

///
/// \brief Base Class for Property lists, providing global default
Expand All @@ -55,8 +67,11 @@ class PropertyListBase: public Object {
static const PropertyListBase plist{};
return plist;
}
};

private:
template <typename T, typename U>
friend T details::get_plist(const U&, hid_t (*f)(hid_t));
};

///
/// \brief HDF5 property Lists
Expand Down
36 changes: 8 additions & 28 deletions include/highfive/bits/H5File_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ inline const std::string& File::getName() const noexcept {

inline hsize_t File::getMetadataBlockSize() const {
hsize_t size;
auto fid_fapl = H5Fget_access_plist(getId());
if (H5Pget_meta_block_size(fid_fapl, &size) < 0) {
auto fapl = getAccessPropertyList();
if (H5Pget_meta_block_size(fapl.getId(), &size) < 0) {
HDF5ErrMapper::ToException<FileException>(
std::string("Unable to access file metadata block size"));
}
Expand All @@ -105,8 +105,8 @@ inline hsize_t File::getMetadataBlockSize() const {
inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
H5F_libver_t low;
H5F_libver_t high;
auto fid_fapl = getAccessPList();
if (H5Pget_libver_bounds(fid_fapl, &low, &high) < 0) {
auto fapl = getAccessPropertyList();
if (H5Pget_libver_bounds(fapl.getId(), &low, &high) < 0) {
HDF5ErrMapper::ToException<FileException>(
std::string("Unable to access file version bounds"));
}
Expand All @@ -115,57 +115,37 @@ inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {

#if H5_VERSION_GE(1, 10, 1)
inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
auto fcpl = getCreatePList();
auto fcpl = getCreatePropertyList();

H5F_fspace_strategy_t strategy;
hbool_t persist;
hsize_t threshold;

if (H5Pget_file_space_strategy(fcpl, &strategy, &persist, &threshold) < 0) {
if (H5Pget_file_space_strategy(fcpl.getId(), &strategy, &persist, &threshold) < 0) {
HDF5ErrMapper::ToException<FileException>(std::string("Unable to get file space strategy"));
}

H5Pclose(fcpl);
return strategy;
}

inline hsize_t File::getFileSpacePageSize() const {
auto fcpl = getCreatePList();
auto fcpl = getCreatePropertyList();
hsize_t page_size;

if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
HDF5ErrMapper::ToException<FileException>(
std::string("Cannot obtain page size as paged allocation is not used."));
}

if (H5Pget_file_space_page_size(fcpl, &page_size) < 0) {
if (H5Pget_file_space_page_size(fcpl.getId(), &page_size) < 0) {
HDF5ErrMapper::ToException<FileException>(
std::string("Unable to get file space page size"));
}

H5Pclose(fcpl);
return page_size;
}
#endif

inline hid_t File::getAccessPList() const {
auto fapl = H5Fget_access_plist(getId());
if (fapl < 0) {
HDF5ErrMapper::ToException<FileException>(std::string("Unable to get access plist."));
}

return fapl;
}

inline hid_t File::getCreatePList() const {
auto fapl = H5Fget_create_plist(getId());
if (fapl < 0) {
HDF5ErrMapper::ToException<FileException>(std::string("Unable to get create plist."));
}

return fapl;
}

inline void File::flush() {
if (H5Fflush(_hid, H5F_SCOPE_GLOBAL) < 0) {
HDF5ErrMapper::ToException<FileException>(std::string("Unable to flush file " + getName()));
Expand Down

0 comments on commit 1cf20ee

Please sign in to comment.