HighFive 2.10.0
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 <H5Ppublic.h>
18
19#include "../H5DataSpace.hpp"
20#include "H5Converter_misc.hpp"
21#include "H5ReadWrite_misc.hpp"
22#include "H5Utils.hpp"
23#include "h5a_wrapper.hpp"
24#include "h5d_wrapper.hpp"
25
26namespace HighFive {
27
28inline std::string Attribute::getName() const {
29 return details::get_name(
30 [&](char* buffer, size_t length) { return detail::h5a_get_name(_hid, length, buffer); });
31}
32
33inline size_t Attribute::getStorageSize() const {
34 return static_cast<size_t>(detail::h5a_get_storage_size(_hid));
35}
36
38 DataType res;
39 res._hid = detail::h5a_get_type(_hid);
40 return res;
41}
42
44 DataSpace space;
45 space._hid = detail::h5a_get_space(_hid);
46 return space;
47}
48
50 return getSpace();
51}
52
53template <typename T>
54inline T Attribute::read() const {
55 T array;
56 read(array);
57 return array;
58}
59
60template <typename T>
61inline void Attribute::read(T& array) const {
62 const DataSpace& mem_space = getMemSpace();
63 auto file_datatype = getDataType();
64 const details::BufferInfo<T> buffer_info(
65 file_datatype,
66 [this]() -> std::string { return this->getName(); },
67 details::BufferInfo<T>::Operation::read);
68
69 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
70 std::ostringstream ss;
71 ss << "Impossible to read Attribute 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, file_datatype);
86 read_raw(r.getPointer(), buffer_info.data_type);
87 // re-arrange results
88 r.unserialize(array);
89
90 auto t = buffer_info.data_type;
91 auto c = t.getClass();
92
93 if (c == DataTypeClass::VarLen || t.isVariableStr()) {
94#if H5_VERSION_GE(1, 12, 0)
95 // This one have been created in 1.12.0
96 (void) detail::h5t_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer());
97#else
98 // This one is deprecated since 1.12.0
99 (void) detail::h5d_vlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer());
100#endif
101 }
102}
103
104template <typename T>
105inline void Attribute::read(T* array, const DataType& mem_datatype) const {
106 read_raw(array, mem_datatype);
107}
108
109template <typename T>
110inline void Attribute::read_raw(T* array, const DataType& mem_datatype) const {
111 static_assert(!std::is_const<T>::value,
112 "read() requires a non-const structure to read data into");
113
114 detail::h5a_read(getId(), mem_datatype.getId(), static_cast<void*>(array));
115}
116
117template <typename T>
118inline void Attribute::read(T* array) const {
119 read_raw(array);
120}
121
122template <typename T>
123inline void Attribute::read_raw(T* array) const {
124 using element_type = typename details::inspector<T>::base_type;
125 const DataType& mem_datatype = create_and_check_datatype<element_type>();
126
127 read_raw(array, mem_datatype);
128}
129
130template <typename T>
131inline void Attribute::write(const T& buffer) {
132 const DataSpace& mem_space = getMemSpace();
133
134 if (mem_space.getElementCount() == 0) {
135 return;
136 }
137
138 auto file_datatype = getDataType();
139
140 const details::BufferInfo<T> buffer_info(
141 file_datatype,
142 [this]() -> std::string { return this->getName(); },
143 details::BufferInfo<T>::Operation::write);
144
145 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
146 std::ostringstream ss;
147 ss << "Impossible to write buffer of dimensions " << buffer_info.n_dimensions
148 << " into dataset of dimensions " << mem_space.getNumberDimensions();
149 throw DataSpaceException(ss.str());
150 }
151 auto w = details::data_converter::serialize<T>(buffer, file_datatype);
152 write_raw(w.getPointer(), buffer_info.data_type);
153}
154
155template <typename T>
156inline void Attribute::write_raw(const T* buffer, const DataType& mem_datatype) {
157 detail::h5a_write(getId(), mem_datatype.getId(), buffer);
158}
159
160template <typename T>
161inline void Attribute::write_raw(const T* buffer) {
162 using element_type = typename details::inspector<T>::base_type;
163 const auto& mem_datatype = create_and_check_datatype<element_type>();
164
165 write_raw(buffer, mem_datatype);
166}
167
168} // namespace HighFive
DataSpace getSpace() const
Get the DataSpace of the current Attribute.
Definition H5Attribute_misc.hpp:43
DataType getDataType() const
Get the DataType of the Attribute.
Definition H5Attribute_misc.hpp:37
void read_raw(T *array, const DataType &mem_datatype) const
Read the attribute into a pre-allocated buffer.
Definition H5Attribute_misc.hpp:110
std::string getName() const
Get the name of the current Attribute.
Definition H5Attribute_misc.hpp:28
T read() const
Get the value of the Attribute.
Definition H5Attribute_misc.hpp:54
DataSpace getMemSpace() const
Get the DataSpace of the current Attribute.
Definition H5Attribute_misc.hpp:49
void write(const T &value)
Write the value into the Attribute.
Definition H5Attribute_misc.hpp:131
void write_raw(const T *buffer, const DataType &mem_datatype)
Write from a raw pointer.
Definition H5Attribute_misc.hpp:156
size_t getStorageSize() const
The number of bytes required to store the attribute in the HDF5 file.
Definition H5Attribute_misc.hpp:33
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:112
Class representing the space (dimensions) of a DataSet.
Definition H5DataSpace.hpp:39
size_t getNumberDimensions() const
Returns the number of dimensions of a DataSpace.
Definition H5Dataspace_misc.hpp:100
size_t getElementCount() const
Return the number of elements in this DataSpace.
Definition H5Dataspace_misc.hpp:112
std::vector< size_t > getDimensions() const
Returns the size of the dataset in each dimension.
Definition H5Dataspace_misc.hpp:104
HDF5 Data Type.
Definition H5DataType.hpp:61
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:69
hid_t _hid
Definition H5Object.hpp:105
Definition H5_definitions.hpp:22