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

Make ~Object protected. #1009

Merged
merged 1 commit into from
May 23, 2024
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
26 changes: 3 additions & 23 deletions include/highfive/H5Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,12 @@ enum class ObjectType {
Other // Internal/custom object type
};

namespace detail {
/// \brief Internal hack to create an `Object` from an ID.
///
/// WARNING: Creating an Object from an ID has implications w.r.t. the lifetime of the object
/// that got passed via its ID. Using this method careless opens up the suite of issues
/// related to C-style resource management, including the analog of double free, dangling
/// pointers, etc.
///
/// NOTE: This is not part of the API and only serves to work around a compiler issue in GCC which
/// prevents us from using `friend`s instead. This function should only be used for internal
/// purposes. The problematic construct is:
///
/// template<class Derived>
/// friend class SomeCRTP<Derived>;
///
/// \private
Object make_object(hid_t hid);
} // namespace detail


class Object {
public:
// move constructor, reuse hid
Object(Object&& other) noexcept;

// decrease reference counter
~Object();

///
/// \brief isValid
/// \return true if current Object is a valid HDF5Object
Expand Down Expand Up @@ -99,13 +77,15 @@ class Object {
// Init with an low-level object id
explicit Object(hid_t);

// decrease reference counter
~Object();

// Copy-Assignment operator
Object& operator=(const Object& other);

hid_t _hid;

private:
friend Object detail::make_object(hid_t);
friend class Reference;
friend class CompoundType;

Expand Down
3 changes: 0 additions & 3 deletions include/highfive/bits/H5Node_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ class NodeTraits {
// It makes behavior consistent among versions and by default transforms
// errors to exceptions
bool _exist(const std::string& node_name, bool raise_errors = true) const;

// Opens an arbitrary object to obtain info
Object _open(const std::string& node_name) const;
};


Expand Down
16 changes: 6 additions & 10 deletions include/highfive/bits/H5Node_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ inline LinkType NodeTraits<Derivate>::getLinkType(const std::string& node_name)

template <typename Derivate>
inline ObjectType NodeTraits<Derivate>::getObjectType(const std::string& node_name) const {
return _open(node_name).getType();
const auto id = detail::h5o_open(static_cast<const Derivate*>(this)->getId(),
node_name.c_str(),
H5P_DEFAULT);
auto object_type = _convert_object_type(detail::h5i_get_type(id));
detail::h5o_close(id);
return object_type;
}


Expand Down Expand Up @@ -314,13 +319,4 @@ inline void NodeTraits<Derivate>::createHardLink(const std::string& link_name,
}


template <typename Derivate>
inline Object NodeTraits<Derivate>::_open(const std::string& node_name) const {
const auto id = detail::h5o_open(static_cast<const Derivate*>(this)->getId(),
node_name.c_str(),
H5P_DEFAULT);
return detail::make_object(id);
}


} // namespace HighFive
6 changes: 0 additions & 6 deletions include/highfive/bits/H5Object_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include "h5i_wrapper.hpp"

namespace HighFive {
namespace detail {
inline Object make_object(hid_t hid) {
return Object(hid);
}
} // namespace detail


inline Object::Object()
: _hid(H5I_INVALID_HID) {}
Expand Down
9 changes: 9 additions & 0 deletions include/highfive/bits/h5o_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@
return hid;
}

inline herr_t h5o_close(hid_t id) {
herr_t err = H5Oclose(id);
if (err < 0) {
HDF5ErrMapper::ToException<ObjectException>("Unable to close object.");

Check warning on line 21 in include/highfive/bits/h5o_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5o_wrapper.hpp#L21

Added line #L21 was not covered by tests
}

return err;
}

} // namespace detail
} // namespace HighFive
Loading