hops
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 "H5Utils.hpp"
27 
28 namespace HighFive {
29 
31  return details::get_name([&](char *buffer, hsize_t length) {
32  return H5Aget_name(_hid, length, buffer);
33  });
34 }
35 
36 inline 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 
55 inline DataSpace Attribute::getMemSpace() const { return getSpace(); }
56 
57 template <typename T>
58 inline void Attribute::read(T& array) const {
59  static_assert(!std::is_const<typename std::remove_reference<T>::type>::value,
60  "read() requires a non-const array to read into");
61  using element_type = typename details::type_of_array<T>::type;
62  const size_t dim_array = details::array_dims<T>::value;
63  DataSpace space = getSpace();
64  DataSpace mem_space = getMemSpace();
65 
66  if (!details::checkDimensions(mem_space, dim_array)) {
67  std::ostringstream ss;
68  ss << "Impossible to read attribute of dimensions "
69  << mem_space.getNumberDimensions() << " into arrays of dimensions "
70  << dim_array;
71  throw DataSpaceException(ss.str());
72  }
73 
74  const DataType mem_datatype = create_and_check_datatype<element_type>();
75 
76  // Apply pre read conversions
77  details::data_converter<T> converter(mem_space);
78 
79  if (H5Aread(getId(), mem_datatype.getId(),
80  static_cast<void*>(converter.transform_read(array))) < 0) {
81  HDF5ErrMapper::ToException<AttributeException>(
82  "Error during HDF5 Read: ");
83  }
84 
85  // re-arrange results
86  converter.process_result(array);
87 }
88 
89 template <typename T>
90 inline void Attribute::read(T* array) const {
91  static_assert(!std::is_const<T>::value,
92  "read() requires a non-const structure to read data into");
93  using element_type = typename details::type_of_array<T>::type;
94  DataSpace mem_space = getMemSpace();
95 
96  const DataType mem_datatype = create_and_check_datatype<element_type>();
97 
98  if (H5Aread(getId(), mem_datatype.getId(),
99  static_cast<void*>(array)) < 0) {
100  HDF5ErrMapper::ToException<AttributeException>(
101  "Error during HDF5 Read: ");
102  }
103 }
104 
105 template <typename T>
106 inline void Attribute::write(const T& buffer) {
107  using element_type = typename details::type_of_array<T>::type;
108  const size_t dim_buffer = details::array_dims<T>::value;
109  DataSpace space = getSpace();
110  DataSpace mem_space = getMemSpace();
111 
112  if (!details::checkDimensions(mem_space, dim_buffer)) {
113  std::ostringstream ss;
114  ss << "Impossible to write buffer of dimensions " << dim_buffer
115  << " into attribute of dimensions "
116  << mem_space.getNumberDimensions();
117  throw DataSpaceException(ss.str());
118  }
119 
120  const DataType mem_datatype = create_and_check_datatype<element_type>();
121  details::data_converter<T> converter(mem_space);
122 
123  if (H5Awrite(getId(), mem_datatype.getId(),
124  static_cast<const void*>(converter.transform_write(buffer))) < 0) {
125  HDF5ErrMapper::ToException<DataSetException>(
126  "Error during HDF5 Write: ");
127  }
128 }
129 
130 template <typename T>
131 inline void Attribute::write_raw(const T& buffer) {
132  using element_type = typename details::type_of_array<T>::type;
133  DataSpace space = getSpace();
134  DataSpace mem_space = getMemSpace();
135  const DataType mem_datatype = create_and_check_datatype<element_type>();
136  details::data_converter<T> converter(mem_space);
137 
138  if (H5Awrite(getId(), mem_datatype.getId(),
139  static_cast<const void*>(converter.transform_write(buffer))) < 0) {
140  HDF5ErrMapper::ToException<DataSetException>(
141  "Error during HDF5 Write: ");
142  }
143 }
144 
145 } // namespace HighFive
146 
147 #endif // H5ATTRIBUTE_MISC_HPP
HighFive::Attribute::getSpace
DataSpace getSpace() const
getSpace
Definition: H5Attribute_misc.hpp:46
HighFive::details::get_name
std::string get_name(T fct)
Definition: H5Utils.hpp:252
HighFive::DataType
HDF5 Data Type.
Definition: H5DataType.hpp:42
HighFive::Attribute::read
void read(T &array) const
Definition: H5Attribute_misc.hpp:58
HighFive::details::array_dims
Definition: H5Utils.hpp:44
HighFive::Attribute::getMemSpace
DataSpace getMemSpace() const
getMemSpace
Definition: H5Attribute_misc.hpp:55
HighFive::details::data_converter::transform_write
const Scalar * transform_write(const Scalar &datamem) const noexcept
Definition: H5Converter_misc.hpp:137
HighFive::Object::getId
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
HighFive::details::data_converter::process_result
void process_result(Scalar &) const noexcept
Definition: H5Converter_misc.hpp:141
HighFive::Attribute::write_raw
void write_raw(const T &buffer)
Definition: H5Attribute_misc.hpp:131
HighFive::details::type_of_array::type
unqualified_t< T > type
Definition: H5Utils.hpp:137
HighFive::details::data_converter::transform_read
Scalar * transform_read(Scalar &datamem) const noexcept
Definition: H5Converter_misc.hpp:133
HighFive::Attribute::getStorageSize
size_t getStorageSize() const
Definition: H5Attribute_misc.hpp:36
HighFive::Object::_hid
hid_t _hid
Definition: H5Object.hpp:81
HighFive::Attribute::getDataType
DataType getDataType() const
getDataType
Definition: H5Attribute_misc.hpp:40
HighFive::DataSpace
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
HighFive::Attribute::getName
std::string getName() const
return the name of the current attribute
Definition: H5Attribute_misc.hpp:30
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
H5Converter_misc.hpp
HighFive::details::data_converter
Definition: H5_definitions.hpp:57
H5Utils.hpp
HighFive::details::checkDimensions
bool checkDimensions(const DataSpace &mem_space, size_t input_dims)
dimension checks
Definition: H5Dataspace_misc.hpp:227
HighFive::DataSpace::getNumberDimensions
size_t getNumberDimensions() const
getNumberDimensions
Definition: H5Dataspace_misc.hpp:90
HighFive
Definition: H5_definitions.hpp:15
HighFive::DataSpaceException
Exception specific to HighFive DataSpace interface.
Definition: H5Exception.hpp:99
HighFive::Attribute::write
void write(const T &buffer)
Definition: H5Attribute_misc.hpp:106