HighFive  2.2.2
HighFive - Header-only C++ HDF5 interface
H5Reference_misc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2020, EPFL - Blue Brain Project
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 
10 #ifndef H5REFERENCE_MISC_HPP
11 #define H5REFERENCE_MISC_HPP
12 
13 #include <string>
14 #include <H5Ppublic.h>
15 
16 #include "H5Utils.hpp"
17 
18 namespace HighFive {
19 
20 inline Reference::Reference(const Object& location, const Object& object)
21  : parent_id(location.getId()) {
22  obj_name = details::get_name([&](char *buffer, hsize_t length) {
23  return H5Iget_name(object.getId(), buffer, length); });
24 }
25 
26 inline void Reference::create_ref(hobj_ref_t* refptr) const {
27  if (H5Rcreate(refptr, parent_id, obj_name.c_str(), H5R_OBJECT, -1) < 0) {
28  HDF5ErrMapper::ToException<ReferenceException>(
29  std::string("Unable to create the reference for \"") + obj_name + "\":");
30  }
31 }
32 
33 inline ObjectType Reference::getType(const Object& location) const {
34  return get_ref(location).getType();
35 }
36 
37 template <typename T>
38 inline T Reference::dereference(const Object& location) const {
39  static_assert(std::is_same<DataSet, T>::value || std::is_same<Group, T>::value,
40  "We can only (de)reference HighFive::Group or HighFive:DataSet");
41  auto obj = get_ref(location) ;
42  if (obj.getType() != T::type) {
43  HDF5ErrMapper::ToException<ReferenceException>(
44  "Trying to dereference the wrong type");
45  }
46 #if defined __GNUC__ && __GNUC__ < 9
47  return std::move(obj);
48 #else
49  return obj;
50 #endif
51 }
52 
53 inline Object Reference::get_ref(const Object& location) const {
54  hid_t res;
55 #if (H5Rdereference_vers == 2)
56  if ((res = H5Rdereference(location.getId(), H5P_DEFAULT, H5R_OBJECT, &href)) < 0) {
57  HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
58  }
59 #else
60  if ((res = H5Rdereference(location.getId(), H5R_OBJECT, &href)) < 0) {
61  HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
62  }
63 #endif
64  return Object(res);
65 }
66 
67 } // namespace HighFive
68 
69 #endif // H5REFERENCE_MISC_HPP
Definition: H5Object.hpp:36
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
ObjectType getType() const
Gets the fundamental type of the object (dataset, group, etc)
Definition: H5Object_misc.hpp:78
T dereference(const Object &location) const
Retrieve the Object being referenced by the Reference.
Definition: H5Reference_misc.hpp:38
Reference()=default
Create an empty Reference to be initialized later.
ObjectType getType(const Object &location) const
Get only the type of the referenced Object.
Definition: H5Reference_misc.hpp:33
void create_ref(hobj_ref_t *refptr) const
Create the low-level reference and store it at refptr.
Definition: H5Reference_misc.hpp:26
Definition: H5_definitions.hpp:15
ObjectType
Enum of the types of objects (H5O api)
Definition: H5Object.hpp:25