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 "../H5Attribute.hpp"
26 #include "../H5DataSpace.hpp"
27 #include "../H5DataType.hpp"
28 
29 #include "H5Converter_misc.hpp"
30 #include "H5Utils.hpp"
31 
32 namespace HighFive {
33 
35 
36 inline size_t Attribute::getStorageSize() const {
37  return 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  typedef typename std::remove_const<T>::type type_no_const;
60 
61  type_no_const& nocv_array = const_cast<type_no_const&>(array);
62 
63  const size_t dim_array = details::array_dims<type_no_const>::value;
64  DataSpace space = getSpace();
65  DataSpace mem_space = getMemSpace();
66 
67  if (!details::checkDimensions(mem_space, dim_array)) {
68  std::ostringstream ss;
69  ss << "Impossible to read attribute of dimensions "
70  << mem_space.getNumberDimensions() << " into arrays of dimensions "
71  << dim_array;
72  throw DataSpaceException(ss.str());
73  }
74 
75  // Create mem datatype
77  array_datatype;
78 
79  // Apply pre read convertions
80  details::data_converter<type_no_const> converter(nocv_array, mem_space);
81 
82  if (H5Aread(getId(), array_datatype.getId(),
83  static_cast<void*>(converter.transform_read(nocv_array))) < 0) {
84  HDF5ErrMapper::ToException<AttributeException>(
85  "Error during HDF5 Read: ");
86  }
87 
88  // re-arrange results
89  converter.process_result(nocv_array);
90 }
91 
92 template <typename T>
93 inline void Attribute::write(const T& buffer) {
94  typedef typename std::remove_const<T>::type type_no_const;
95 
96  type_no_const& nocv_buffer = const_cast<type_no_const&>(buffer);
97 
98  const size_t dim_buffer = details::array_dims<type_no_const>::value;
99  DataSpace space = getSpace();
100  DataSpace mem_space = getMemSpace();
101 
102  if (!details::checkDimensions(mem_space, dim_buffer)) {
103  std::ostringstream ss;
104  ss << "Impossible to write buffer of dimensions " << dim_buffer
105  << " into attribute of dimensions "
106  << mem_space.getNumberDimensions();
107  throw DataSpaceException(ss.str());
108  }
109 
111  array_datatype;
112 
113  // Apply pre write convertions
114  details::data_converter<type_no_const> converter(nocv_buffer, mem_space);
115 
116  if (H5Awrite(getId(), array_datatype.getId(),
117  static_cast<const void*>(
118  converter.transform_write(nocv_buffer))) < 0) {
119  HDF5ErrMapper::ToException<DataSetException>(
120  "Error during HDF5 Write: ");
121  }
122 }
123 }
124 
125 #endif // H5ATTRIBUTE_MISC_HPP
DataType getDataType() const
getDataType
size_t getNumberDimensions() const
getNumberDimensions
Exception specific to HighFive DataSpace interface.
Definition: H5Exception.hpp:98
hid_t getId() const
getId
Scalar * transform_write(Scalar &datamem)
void read(T &array) const
DataSpace getSpace() const
getSpace
Scalar * transform_read(Scalar &datamem)
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
DataSpace getMemSpace() const
getMemSpace
HDF5 Data Type.
Definition: H5DataType.hpp:21
void write(const T &buffer)
bool checkDimensions(const DataSpace &mem_space, size_t input_dims)
dimension checks
size_t getStorageSize() const


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06