HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Attribute_misc.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Ali Can Demiralp <ali.demiralp@rwth-aachen.de>
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#pragma once
10
11#include <algorithm>
12#include <functional>
13#include <numeric>
14#include <sstream>
15#include <string>
16
17#include <H5Apublic.h>
18#include <H5Ppublic.h>
19
20#include "../H5DataSpace.hpp"
21#include "H5Converter_misc.hpp"
22#include "H5ReadWrite_misc.hpp"
23#include "H5Utils.hpp"
24
25namespace HighFive {
26
27inline std::string Attribute::getName() const {
28 return details::get_name(
29 [&](char* buffer, size_t length) { return H5Aget_name(_hid, length, buffer); });
30}
31
32inline size_t Attribute::getStorageSize() const {
33 return static_cast<size_t>(H5Aget_storage_size(_hid));
34}
35
37 DataType res;
38 res._hid = H5Aget_type(_hid);
39 return res;
40}
41
43 DataSpace space;
44 if ((space._hid = H5Aget_space(_hid)) < 0) {
45 HDF5ErrMapper::ToException<AttributeException>("Unable to get DataSpace out of Attribute");
46 }
47 return space;
48}
49
51 return getSpace();
52}
53
54template <typename T>
55inline T Attribute::read() const {
56 T array;
57 read(array);
58 return array;
59}
60
61template <typename T>
62inline void Attribute::read(T& array) const {
63 const DataSpace& mem_space = getMemSpace();
64 const details::BufferInfo<T> buffer_info(
66 [this]() -> std::string { return this->getName(); },
67 details::BufferInfo<T>::read);
68
69 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
70 std::ostringstream ss;
71 ss << "Impossible to read DataSet of dimensions " << mem_space.getNumberDimensions()
72 << " into arrays of dimensions " << buffer_info.n_dimensions;
73 throw DataSpaceException(ss.str());
74 }
75 auto dims = mem_space.getDimensions();
76
77 if (mem_space.getElementCount() == 0) {
78 auto effective_dims = details::squeezeDimensions(dims,
79 details::inspector<T>::recursive_ndim);
80
81 details::inspector<T>::prepare(array, effective_dims);
82 return;
83 }
84
85 auto r = details::data_converter::get_reader<T>(dims, array);
86 read(r.get_pointer(), buffer_info.data_type);
87 // re-arrange results
88 r.unserialize();
89 auto t = create_datatype<typename details::inspector<T>::base_type>();
90 auto c = t.getClass();
91 if (c == DataTypeClass::VarLen || t.isVariableStr()) {
92#if H5_VERSION_GE(1, 12, 0)
93 // This one have been created in 1.12.0
94 (void) H5Treclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.get_pointer());
95#else
96 // This one is deprecated since 1.12.0
97 (void) H5Dvlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.get_pointer());
98#endif
99 }
100}
101
102template <typename T>
103inline void Attribute::read(T* array, const DataType& dtype) const {
104 static_assert(!std::is_const<T>::value,
105 "read() requires a non-const structure to read data into");
106 using element_type = typename details::inspector<T>::base_type;
107 // Auto-detect mem datatype if not provided
108 const DataType& mem_datatype = dtype.empty() ? create_and_check_datatype<element_type>()
109 : dtype;
110
111 if (H5Aread(getId(), mem_datatype.getId(), static_cast<void*>(array)) < 0) {
112 HDF5ErrMapper::ToException<AttributeException>("Error during HDF5 Read: ");
113 }
114}
115
116template <typename T>
117inline void Attribute::write(const T& buffer) {
118 const DataSpace& mem_space = getMemSpace();
119
120 if (mem_space.getElementCount() == 0) {
121 return;
122 }
123
124 const details::BufferInfo<T> buffer_info(
125 getDataType(),
126 [this]() -> std::string { return this->getName(); },
127 details::BufferInfo<T>::write);
128
129 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
130 std::ostringstream ss;
131 ss << "Impossible to write buffer of dimensions " << buffer_info.n_dimensions
132 << " into dataset of dimensions " << mem_space.getNumberDimensions();
133 throw DataSpaceException(ss.str());
134 }
135 auto w = details::data_converter::serialize<T>(buffer);
136 write_raw(w.get_pointer(), buffer_info.data_type);
137}
138
139template <typename T>
140inline void Attribute::write_raw(const T* buffer, const DataType& dtype) {
141 using element_type = typename details::inspector<T>::base_type;
142 const auto& mem_datatype = dtype.empty() ? create_and_check_datatype<element_type>() : dtype;
143
144 if (H5Awrite(getId(), mem_datatype.getId(), buffer) < 0) {
145 HDF5ErrMapper::ToException<DataSetException>("Error during HDF5 Write: ");
146 }
147}
148
149} // namespace HighFive
void write_raw(const T *buffer, const DataType &dtype={})
Write a buffer to this attribute.
Definition H5Attribute_misc.hpp:140
DataSpace getSpace() const
getSpace
Definition H5Attribute_misc.hpp:42
DataType getDataType() const
getDataType
Definition H5Attribute_misc.hpp:36
std::string getName() const
return the name of the current attribute
Definition H5Attribute_misc.hpp:27
void write(const T &buffer)
Definition H5Attribute_misc.hpp:117
T read() const
Return the attribute.
Definition H5Attribute_misc.hpp:55
DataSpace getMemSpace() const
getMemSpace
Definition H5Attribute_misc.hpp:50
size_t getStorageSize() const
Definition H5Attribute_misc.hpp:32
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:112
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:25
size_t getNumberDimensions() const
getNumberDimensions
Definition H5Dataspace_misc.hpp:93
size_t getElementCount() const
getElementCount
Definition H5Dataspace_misc.hpp:112
std::vector< size_t > getDimensions() const
getDimensions
Definition H5Dataspace_misc.hpp:102
HDF5 Data Type.
Definition H5DataType.hpp:54
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:35
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:65
hid_t _hid
Definition H5Object.hpp:105
Definition H5_definitions.hpp:15