From 60a3b06ca862a065c1bd870ce6cc24c9991a5042 Mon Sep 17 00:00:00 2001 From: Quark-X10 <1952584+Quark-X10@users.noreply.github.com> Date: Tue, 11 Jul 2023 19:03:47 +0200 Subject: [PATCH] Read commited datatype (#796) Adds the ability to open committed data types (including compound datatypes) along with documentation and tests. --------- Co-authored-by: sbouteille Co-authored-by: Nicolas Cornu --- include/highfive/H5DataType.hpp | 2 ++ include/highfive/bits/H5Node_traits.hpp | 8 ++++++++ include/highfive/bits/H5Node_traits_misc.hpp | 13 ++++++++++++ tests/unit/tests_high_five_base.cpp | 21 ++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/include/highfive/H5DataType.hpp b/include/highfive/H5DataType.hpp index 2c901ba44..8403618e2 100644 --- a/include/highfive/H5DataType.hpp +++ b/include/highfive/H5DataType.hpp @@ -113,6 +113,8 @@ class DataType: public Object { friend class File; friend class DataSet; friend class CompoundType; + template + friend class NodeTraits; }; diff --git a/include/highfive/bits/H5Node_traits.hpp b/include/highfive/bits/H5Node_traits.hpp index b35cc2a87..d53d3f048 100644 --- a/include/highfive/bits/H5Node_traits.hpp +++ b/include/highfive/bits/H5Node_traits.hpp @@ -129,6 +129,14 @@ class NodeTraits { /// \return the group object Group getGroup(const std::string& group_name) const; + /// + /// \brief open a commited datatype with the name type_name + /// \param type_name + /// \return the datatype object + DataType getDataType( + const std::string& type_name, + const DataTypeAccessProps& accessProps = DataTypeAccessProps::Default()) const; + /// /// \brief return the number of leaf objects of the node / group /// \return number of leaf objects diff --git a/include/highfive/bits/H5Node_traits_misc.hpp b/include/highfive/bits/H5Node_traits_misc.hpp index f823155b0..2f75ff311 100644 --- a/include/highfive/bits/H5Node_traits_misc.hpp +++ b/include/highfive/bits/H5Node_traits_misc.hpp @@ -174,6 +174,19 @@ inline Group NodeTraits::getGroup(const std::string& group_name) const return detail::make_group(hid); } +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); +} + template inline size_t NodeTraits::getNumberObjects() const { hsize_t res; diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index d869a2cb0..287f54595 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -2899,6 +2899,27 @@ TEST_CASE("HighFiveEnum") { } } +TEST_CASE("HighFiveReadType") { + const std::string file_name("readtype_test.h5"); + const std::string datatype_name1("my_type"); + const std::string datatype_name2("position"); + + File file(file_name, File::ReadWrite | File::Create | File::Truncate); + + CompoundType t1 = create_compound_csl1(); + t1.commit(file, datatype_name1); + + CompoundType t2 = file.getDataType(datatype_name1); + + auto t3 = create_enum_position(); + t3.commit(file, datatype_name2); + + DataType t4 = file.getDataType(datatype_name2); + + CHECK(t2 == t1); + CHECK(t4 == t3); +} + TEST_CASE("HighFiveFixedString") { const std::string file_name("array_atomic_types.h5"); const std::string group_1("group1");