Skip to content

Commit

Permalink
Add named ctors for scalar and null dataspaces. (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc authored Dec 18, 2023
1 parent 9e5cb5b commit 5e1a92f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/highfive/H5DataSpace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ class DataSpace: public Object {
/// \since 1.3
explicit DataSpace(DataspaceType space_type);

/// \brief Create a scalar DataSpace.
///
/// \code{.cpp}
/// auto dataspace = DataSpace::Scalar();
/// \endcode
///
/// \since 2.9
static DataSpace Scalar();

/// \brief Create a null DataSpace.
///
/// \code{.cpp}
/// auto dataspace = DataSpace::Null();
/// \endcode
///
/// \since 2.9
static DataSpace Null();

/// \brief Create a copy of the DataSpace which will have different id.
///
/// \code{.cpp}
Expand Down
8 changes: 8 additions & 0 deletions include/highfive/bits/H5Dataspace_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ inline DataSpace::DataSpace(const IT begin, const IT end) {
_hid = detail::h5s_create_simple(int(real_dims.size()), real_dims.data(), nullptr);
}

inline DataSpace DataSpace::Scalar() {
return DataSpace(DataSpace::dataspace_scalar);
}

inline DataSpace DataSpace::Null() {
return DataSpace(DataSpace::dataspace_null);
}

inline DataSpace::DataSpace(const std::vector<size_t>& dims, const std::vector<size_t>& maxdims) {
if (dims.size() != maxdims.size()) {
throw DataSpaceException("dims and maxdims must be the same length.");
Expand Down
9 changes: 9 additions & 0 deletions include/highfive/bits/h5s_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ inline hssize_t h5s_get_simple_extent_npoints(hid_t space_id) {
return nelements;
}

inline H5S_class_t h5s_get_simple_extent_type(hid_t space_id) {
H5S_class_t cls = H5Sget_simple_extent_type(space_id);
if (cls == H5S_NO_CLASS) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to get class of simple dataspace.");
}

return cls;
}


} // namespace detail
} // namespace HighFive
14 changes: 14 additions & 0 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,25 @@ TEST_CASE("DataSpace::getElementCount") {
SECTION("null") {
auto space = DataSpace(DataSpace::dataspace_null);
CHECK(space.getElementCount() == 0);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_NULL);
}

SECTION("null named ctor") {
auto space = DataSpace::Null();
CHECK(space.getElementCount() == 0);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_NULL);
}

SECTION("scalar") {
auto space = DataSpace(DataSpace::dataspace_scalar);
CHECK(space.getElementCount() == 1);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_SCALAR);
}

SECTION("scalar named ctor") {
auto space = DataSpace::Scalar();
CHECK(space.getElementCount() == 1);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_SCALAR);
}

SECTION("simple, empty (1D)") {
Expand Down

0 comments on commit 5e1a92f

Please sign in to comment.