h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Reference_misc.hpp
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
18namespace h5gt {
19
20inline Reference::Reference(const Object& location, const Object& object)
21 : parent_id(location.getId(false)) {
22 obj_name = details::get_name([&](char *buffer, hsize_t length) {
23 return H5Iget_name(object.getId(false), buffer, length); });
24}
25
26inline 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
33inline ObjectType Reference::getType(const Object& location) const {
34 return get_ref(location).getObjectType();
35}
36
37template <typename T>
38inline 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 h5gt::Group or h5gt:DataSet");
41 auto obj = get_ref(location) ;
42 if (obj.getObjectType() != 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
53inline Object Reference::get_ref(const Object& location) const {
54 hid_t res;
55#if (H5Rdereference_vers == 2)
56 if ((res = H5Rdereference(location.getId(false), H5P_DEFAULT, H5R_OBJECT, &href)) < 0) {
57 HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
58 }
59#else
60 if ((res = H5Rdereference(location.getId(false), H5R_OBJECT, &href)) < 0) {
61 HDF5ErrMapper::ToException<ReferenceException>("Unable to dereference.");
62 }
63#endif
64 return Object(res);
65}
66
67} // namespace h5gt
68
69#endif // H5REFERENCE_MISC_HPP
Definition H5Object.hpp:55
ObjectType getObjectType() const
Gets the fundamental type of the object (dataset, group, etc)
Definition H5Object_misc.hpp:195
hid_t getId(const bool &increaseRefCount=false) const noexcept
getId
Definition H5Object_misc.hpp:172
void create_ref(hobj_ref_t *refptr) const
Create the low-level reference and store it at refptr.
Definition H5Reference_misc.hpp:26
ObjectType getType(const Object &location) const
Get only the type of the referenced Object.
Definition H5Reference_misc.hpp:33
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.