hops
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  return std::move(obj);
47 }
48 
49 inline Object Reference::get_ref(const Object& location) const {
50  hid_t res;
51 #if (H5Rdereference_vers == 2)
52  if ((res = H5Rdereference(location.getId(), H5P_DEFAULT, H5R_OBJECT, &href)) < 0) {
53  HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
54  }
55 #else
56  if ((res = H5Rdereference(location.getId(), H5R_OBJECT, &href)) < 0) {
57  HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
58  }
59 #endif
60  return Object(res);
61 }
62 
63 } // namespace HighFive
64 
65 #endif // H5REFERENCE_MISC_HPP
HighFive::details::get_name
std::string get_name(T fct)
Definition: H5Utils.hpp:252
HighFive::Reference::dereference
T dereference(const Object &location) const
Retrieve the Object being referenced by the Reference.
Definition: H5Reference_misc.hpp:38
HighFive::Reference::create_ref
void create_ref(hobj_ref_t *refptr) const
Create the low-level reference and store it at refptr.
Definition: H5Reference_misc.hpp:26
HighFive::Reference::Reference
Reference()=default
Create an empty Reference to be initialized later.
HighFive::Object::getType
ObjectType getType() const
Gets the fundamental type of the object (dataset, group, etc)
Definition: H5Object_misc.hpp:78
HighFive::Object
Definition: H5Object.hpp:36
HighFive::Object::getId
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
HighFive::ObjectType
ObjectType
Enum of the types of objects (H5O api)
Definition: H5Object.hpp:25
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
HighFive::Reference::getType
ObjectType getType(const Object &location) const
Get only the type of the referenced Object.
Definition: H5Reference_misc.hpp:33
H5Utils.hpp
HighFive
Definition: H5_definitions.hpp:15