HighFive 2.3.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#ifndef H5ATTRIBUTE_MISC_HPP
10#define H5ATTRIBUTE_MISC_HPP
11
12#include <algorithm>
13#include <functional>
14#include <numeric>
15#include <sstream>
16#include <string>
17
18#ifdef H5_USE_BOOST
19#include <boost/multi_array.hpp>
20#endif
21
22#include <H5Apublic.h>
23#include <H5Ppublic.h>
24
25#include "H5Converter_misc.hpp"
26#include "H5ReadWrite_misc.hpp"
27#include "H5Utils.hpp"
28
29namespace HighFive {
30
31inline std::string Attribute::getName() const {
32 return details::get_name([&](char *buffer, hsize_t length) {
33 return H5Aget_name(_hid, static_cast<size_t>(length), buffer);
34 });
35}
36
37inline size_t Attribute::getStorageSize() const {
38 return static_cast<size_t>(H5Aget_storage_size(_hid));
39}
40
42 DataType res;
43 res._hid = H5Aget_type(_hid);
44 return res;
45}
46
48 DataSpace space;
49 if ((space._hid = H5Aget_space(_hid)) < 0) {
50 HDF5ErrMapper::ToException<AttributeException>(
51 "Unable to get DataSpace out of Attribute");
52 }
53 return space;
54}
55
56inline DataSpace Attribute::getMemSpace() const { return getSpace(); }
57
58template <typename T>
59inline void Attribute::read(T& array) const {
60 const DataSpace& mem_space = getMemSpace();
61 const details::BufferInfo<T> buffer_info(getDataType(),
62 [this]() -> std::string { return this->getName(); });
63
64 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
65 std::ostringstream ss;
66 ss << "Impossible to read DataSet of dimensions "
67 << mem_space.getNumberDimensions() << " into arrays of dimensions "
68 << buffer_info.n_dimensions;
69 throw DataSpaceException(ss.str());
70 }
71 details::data_converter<T> converter(mem_space);
72 read(converter.transform_read(array), buffer_info.data_type);
73 // re-arrange results
74 converter.process_result(array);
75}
76
77template <typename T>
78inline void Attribute::read(T* array, const DataType& dtype) const {
79 static_assert(!std::is_const<T>::value,
80 "read() requires a non-const structure to read data into");
81 using element_type = typename details::inspector<T>::base_type;
82 // Auto-detect mem datatype if not provided
83 const DataType& mem_datatype =
84 dtype.empty() ? create_and_check_datatype<element_type>() : dtype;
85
86 if (H5Aread(getId(), mem_datatype.getId(),
87 static_cast<void*>(array)) < 0) {
88 HDF5ErrMapper::ToException<AttributeException>(
89 "Error during HDF5 Read: ");
90 }
91}
92
93template <typename T>
94inline void Attribute::write(const T& buffer) {
95 const DataSpace& mem_space = getMemSpace();
96 const details::BufferInfo<T> buffer_info(getDataType(),
97 [this]() -> std::string { return this->getName(); });
98
99 if (!details::checkDimensions(mem_space, buffer_info.n_dimensions)) {
100 std::ostringstream ss;
101 ss << "Impossible to write buffer of dimensions " << buffer_info.n_dimensions
102 << " into dataset of dimensions " << mem_space.getNumberDimensions();
103 throw DataSpaceException(ss.str());
104 }
105 details::data_converter<T> converter(mem_space);
106 write_raw(converter.transform_write(buffer), buffer_info.data_type);
107}
108
109template <typename T>
110inline void Attribute::write_raw(const T* buffer, const DataType& dtype) {
111 using element_type = typename details::inspector<T>::base_type;
112 const auto& mem_datatype =
113 dtype.empty() ? create_and_check_datatype<element_type>() : dtype;
114
115 if (H5Awrite(getId(), mem_datatype.getId(), buffer) < 0) {
116 HDF5ErrMapper::ToException<DataSetException>(
117 "Error during HDF5 Write: ");
118 }
119}
120
121} // namespace HighFive
122
123#endif // H5ATTRIBUTE_MISC_HPP
void write_raw(const T *buffer, const DataType &dtype=DataType())
Definition H5Attribute_misc.hpp:110
DataSpace getSpace() const
getSpace
Definition H5Attribute_misc.hpp:47
DataType getDataType() const
getDataType
Definition H5Attribute_misc.hpp:41
std::string getName() const
return the name of the current attribute
Definition H5Attribute_misc.hpp:31
void write(const T &buffer)
Definition H5Attribute_misc.hpp:94
DataSpace getMemSpace() const
getMemSpace
Definition H5Attribute_misc.hpp:56
size_t getStorageSize() const
Definition H5Attribute_misc.hpp:37
void read(T &array) const
Definition H5Attribute_misc.hpp:59
Exception specific to HighFive DataSpace interface.
Definition H5Exception.hpp:99
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:37
size_t getNumberDimensions() const
getNumberDimensions
Definition H5Dataspace_misc.hpp:94
HDF5 Data Type.
Definition H5DataType.hpp:42
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:28
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:55
hid_t _hid
Definition H5Object.hpp:87
Definition H5_definitions.hpp:15