h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Attribute_misc.hpp
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 H5GT_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 "H5Utils.hpp"
27
28namespace h5gt {
29
30inline std::string Attribute::getName() const {
31 return details::get_name([&](char *buffer, hsize_t length) {
32 return H5Aget_name(_hid, length, buffer);
33 });
34}
35
36inline size_t Attribute::getStorageSize() const {
37 return static_cast<size_t>(H5Aget_storage_size(_hid));
38}
39
41 DataType res;
42 res._hid = H5Aget_type(_hid);
43 return res;
44}
45
47 DataSpace space;
48 if ((space._hid = H5Aget_space(_hid)) < 0) {
49 HDF5ErrMapper::ToException<AttributeException>(
50 "Unable to get DataSpace out of Attribute");
51 }
52 return space;
53}
54
55inline DataSpace Attribute::getMemSpace() const { return getSpace(); }
56
57inline File Attribute::getFile() const {
58 hid_t fileId = H5Iget_file_id(_hid);
59 if (!H5Iis_valid(fileId)){
60 HDF5ErrMapper::ToException<AttributeException>(
61 std::string("File ID is invalid. Probably the object doesn't belong to any file"));
62 }
63 return File::FromId(fileId, false);
64}
65
66
67template <typename T>
68inline void Attribute::read(T& array) const {
69 static_assert(!std::is_const<typename std::remove_reference<T>::type>::value,
70 "read() requires a non-const array to read into");
71 using element_type = typename details::inspector<T>::base_type;
72 const size_t dim_array = details::inspector<T>::recursive_ndim;
73 DataSpace space = getSpace();
74 DataSpace mem_space = getMemSpace();
75
76 if (!details::checkDimensions(mem_space, dim_array)) {
77 std::ostringstream ss;
78 ss << "Impossible to read attribute of dimensions "
79 << mem_space.getNumberDimensions() << " into arrays of dimensions "
80 << dim_array;
81 throw DataSpaceException(ss.str());
82 }
83
84 const DataType mem_datatype = create_and_check_datatype<element_type>();
85
86 // Apply pre read conversions
87 details::data_converter<T> converter(mem_space, mem_datatype);
88
89 if (H5Aread(getId(false), mem_datatype.getId(false),
90 static_cast<void*>(converter.transform_read(array))) < 0) {
91 HDF5ErrMapper::ToException<AttributeException>(
92 "Error during HDF5 Read: ");
93 }
94
95 // re-arrange results
96 converter.process_result(array);
97}
98
99template <typename T>
100inline void Attribute::read(T* array, const DataType& dtype) const {
101 static_assert(!std::is_const<T>::value,
102 "read() requires a non-const structure to read data into");
103 const auto& mem_datatype = dtype.empty() ? create_and_check_datatype<T>() : dtype;
104
105 if (H5Aread(getId(false), mem_datatype.getId(false),
106 static_cast<void*>(array)) < 0) {
107 HDF5ErrMapper::ToException<AttributeException>(
108 "Error during HDF5 Read: ");
109 }
110}
111
112template <typename T>
113inline void Attribute::write(const T& buffer) {
114 using element_type = typename details::inspector<T>::base_type;
115 const size_t dim_buffer = details::inspector<T>::recursive_ndim;
116 DataSpace space = getSpace();
117 DataSpace mem_space = getMemSpace();
118
119 if (!details::checkDimensions(mem_space, dim_buffer)) {
120 std::ostringstream ss;
121 ss << "Impossible to write buffer of dimensions " << dim_buffer
122 << " into attribute of dimensions "
123 << mem_space.getNumberDimensions();
124 throw DataSpaceException(ss.str());
125 }
126
127 const DataType mem_datatype = create_and_check_datatype<element_type>();
128 details::data_converter<T> converter(mem_space, mem_datatype);
129
130 if (H5Awrite(getId(false), mem_datatype.getId(false),
131 static_cast<const void*>(converter.transform_write(buffer))) < 0) {
132 HDF5ErrMapper::ToException<DataSetException>(
133 "Error during HDF5 Write: ");
134 }
135}
136
137template <typename T>
138inline void Attribute::write_raw(const T* buffer, const DataType& dtype) {
139 const DataType mem_datatype = dtype.empty() ? create_and_check_datatype<T>() : dtype;
140
141 if (H5Awrite(getId(false), mem_datatype.getId(false), buffer) < 0) {
142 HDF5ErrMapper::ToException<DataSetException>(
143 "Error during HDF5 Write: ");
144 }
145}
146
147} // namespace h5gt
148
149#endif // H5ATTRIBUTE_MISC_HPP
void write(const T &buffer)
Definition H5Attribute_misc.hpp:113
DataType getDataType() const
getDataType
Definition H5Attribute_misc.hpp:40
void write_raw(const T *buffer, const DataType &dtype=DataType())
Definition H5Attribute_misc.hpp:138
void read(T &array) const
Definition H5Attribute_misc.hpp:68
DataSpace getMemSpace() const
getMemSpace
Definition H5Attribute_misc.hpp:55
std::string getName() const
return the name of the current attribute
Definition H5Attribute_misc.hpp:30
size_t getStorageSize() const
getStorageSize
Definition H5Attribute_misc.hpp:36
DataSpace getSpace() const
getSpace
Definition H5Attribute_misc.hpp:46
Exception specific to h5gt 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:48
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:28
File class.
Definition H5File.hpp:25
hid_t getId(const bool &increaseRefCount=false) const noexcept
getId
Definition H5Object_misc.hpp:172
Definition H5Converter_misc.hpp:124
Definition H5Utils.hpp:49