hops
H5Utils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
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 H5UTILS_HPP
10 #define H5UTILS_HPP
11 
12 // internal utilities functions
13 #include <algorithm>
14 #include <array>
15 #include <cstddef> // __GLIBCXX__
16 #include <exception>
17 #include <string>
18 #include <type_traits>
19 #include <vector>
20 
21 #ifdef H5_USE_BOOST
22 # include <boost/multi_array.hpp>
23 # include <boost/numeric/ublas/matrix.hpp>
24 #endif
25 #ifdef H5_USE_EIGEN
26 # include <Eigen/Eigen>
27 #endif
28 
29 #include <H5public.h>
30 
31 #include "../H5Exception.hpp"
32 
33 namespace HighFive {
34 
35 // If ever used, recognize dimensions of FixedLenStringArray
36 template <std::size_t N>
37 class FixedLenStringArray;
38 
39 
40 namespace details {
41 
42 // determine at compile time number of dimensions of in memory datasets
43 template <typename T>
44 struct array_dims {
45  static constexpr size_t value = 0;
46 };
47 
48 template <std::size_t N>
50  static constexpr size_t value = 1;
51 };
52 
53 template <typename T>
54 struct array_dims<std::vector<T> > {
55  static constexpr size_t value = 1 + array_dims<T>::value;
56 };
57 
58 template <typename T>
59 struct array_dims<T*> {
60  static constexpr size_t value = 1 + array_dims<T>::value;
61 };
62 
63 template <typename T, std::size_t N>
64 struct array_dims<T[N]> {
65  static constexpr size_t value = 1 + array_dims<T>::value;
66 };
67 
68 // Only supporting 1D arrays at the moment
69 template<typename T, std::size_t N>
70 struct array_dims<std::array<T,N>> {
71  static constexpr size_t value = 1 + array_dims<T>::value;
72 };
73 
74 #ifdef H5_USE_BOOST
75 template <typename T, std::size_t Dims>
76 struct array_dims<boost::multi_array<T, Dims> > {
77  static constexpr size_t value = Dims;
78 };
79 
80 template <typename T>
81 struct array_dims<boost::numeric::ublas::matrix<T> > {
82  static constexpr size_t value = 2;
83 };
84 #endif
85 
86 #ifdef H5_USE_EIGEN
87 template<typename T, int M, int N>
88 struct array_dims<Eigen::Matrix<T, M, N>> {
89  static constexpr size_t value = 2;
90 };
91 
92 template<typename T, int M, int N>
93 struct array_dims<std::vector<Eigen::Matrix<T, M, N>>> {
94  static constexpr size_t value = 2;
95 };
96 #endif
97 
98 // determine recursively the size of each dimension of a N dimension vector
99 template <typename T>
100 inline void get_dim_vector_rec(const T& /*vec*/, std::vector<size_t>& /*dims*/) {}
101 
102 template <typename T>
103 inline void get_dim_vector_rec(const std::vector<T>& vec, std::vector<size_t>& dims) {
104  dims.push_back(vec.size());
105  get_dim_vector_rec(vec[0], dims);
106 }
107 
108 template <typename T>
109 inline std::vector<size_t> get_dim_vector(const std::vector<T>& vec) {
110  std::vector<size_t> dims;
111  get_dim_vector_rec(vec, dims);
112  return dims;
113 }
114 
115 // determine recursively the size of each dimension of a N dimension vector
116 template <typename T, std::size_t N>
117 inline void get_dim_vector_rec(const T(&vec)[N], std::vector<size_t>& dims) {
118  dims.push_back(N);
119  get_dim_vector_rec(vec[0], dims);
120 }
121 
122 template <typename T, std::size_t N>
123 inline std::vector<size_t> get_dim_vector(const T(&vec)[N]) {
124  std::vector<size_t> dims;
125  get_dim_vector_rec(vec, dims);
126  return dims;
127 }
128 
129 
130 template <typename T>
131 using unqualified_t = typename std::remove_const<typename std::remove_reference<T>::type
132  >::type;
133 
134 // determine at compile time recursively the basic type of the data
135 template <typename T>
138 };
139 
140 template <typename T>
141 struct type_of_array<std::vector<T>> {
142  typedef typename type_of_array<T>::type type;
143 };
144 
145 template <typename T, std::size_t N>
146 struct type_of_array<std::array<T, N>> {
147  typedef typename type_of_array<T>::type type;
148 };
149 
150 #ifdef H5_USE_BOOST
151 template <typename T, std::size_t Dims>
152 struct type_of_array<boost::multi_array<T, Dims>> {
153  typedef typename type_of_array<T>::type type;
154 };
155 
156 template <typename T>
157 struct type_of_array<boost::numeric::ublas::matrix<T>> {
158  typedef typename type_of_array<T>::type type;
159 };
160 #endif
161 
162 #ifdef H5_USE_EIGEN
163 template<typename T, int M, int N>
164 struct type_of_array<Eigen::Matrix<T, M, N>> {
165  typedef T type;
166 };
167 #endif
168 
169 template <typename T>
170 struct type_of_array<T*> {
171  typedef typename type_of_array<T>::type type;
172 };
173 
174 template <typename T, std::size_t N>
175 struct type_of_array<T[N]> {
176  typedef typename type_of_array<T>::type type;
177 };
178 
179 
180 // Find the type of an eventual char array, otherwise void
181 template <typename>
183  typedef void type;
184 };
185 
186 template <typename T>
187 struct type_char_array<T*> {
188  typedef typename std::conditional<
189  std::is_same<unqualified_t<T>, char>::value,
190  char*,
191  typename type_char_array<T>::type
193 };
194 
195 template <typename T, std::size_t N>
196 struct type_char_array<T[N]> {
197  typedef typename std::conditional<
198  std::is_same<unqualified_t<T>, char>::value,
199  char[N],
200  typename type_char_array<T>::type
202 };
203 
204 
205 // check if the type is a container ( only vector supported for now )
206 template <typename>
207 struct is_container {
208  static const bool value = false;
209 };
210 
211 template <typename T>
212 struct is_container<std::vector<T> > {
213  static const bool value = true;
214 };
215 
216 // check if the type is a basic C-Array
217 template <typename>
218 struct is_c_array {
219  static const bool value = false;
220 };
221 
222 template <typename T>
223 struct is_c_array<T*> {
224  static const bool value = true;
225 };
226 
227 template <typename T, std::size_t N>
228 struct is_c_array<T[N]> {
229  static const bool value = true;
230 };
231 
232 
233 // converter function for hsize_t -> size_t when hsize_t != size_t
234 template <typename Size>
235 inline std::vector<std::size_t> to_vector_size_t(const std::vector<Size>& vec) {
236  static_assert(std::is_same<Size, std::size_t>::value == false,
237  " hsize_t != size_t mandatory here");
238  std::vector<size_t> res(vec.size());
239  std::transform(vec.cbegin(), vec.cend(), res.begin(), [](Size e) {
240  return static_cast<size_t>(e);
241  });
242  return res;
243 }
244 
245 // converter function for hsize_t -> size_t when size_t == hsize_t
246 inline std::vector<std::size_t> to_vector_size_t(const std::vector<std::size_t>& vec) {
247  return vec;
248 }
249 
250 // read name from a H5 object using the specified function
251 template<typename T>
252 inline std::string get_name(T fct) {
253  const size_t maxLength = 255;
254  char buffer[maxLength + 1];
255  ssize_t retcode = fct(buffer, static_cast<hsize_t>(maxLength) + 1);
256  if (retcode < 0) {
257  HDF5ErrMapper::ToException<GroupException>("Error accessing object name");
258  }
259  const size_t length = static_cast<std::size_t>(retcode);
260  if (length <= maxLength) {
261  return std::string(buffer, length);
262  }
263  std::vector<char> bigBuffer(length + 1, 0);
264  fct(bigBuffer.data(), static_cast<hsize_t>(length) + 1);
265  return std::string(bigBuffer.data(), length);
266 }
267 
268 } // namespace details
269 } // namespace HighFive
270 
271 #endif // H5UTILS_HPP
HighFive::details::get_name
std::string get_name(T fct)
Definition: H5Utils.hpp:252
HighFive::details::type_of_array
Definition: H5Utils.hpp:136
HighFive::details::is_container
Definition: H5Utils.hpp:207
HighFive::details::type_of_array< T * >::type
type_of_array< T >::type type
Definition: H5Utils.hpp:171
HighFive::details::type_char_array< T * >::type
std::conditional< std::is_same< unqualified_t< T >, char >::value, char *, typename type_char_array< T >::type >::type type
Definition: H5Utils.hpp:192
HighFive::details::array_dims
Definition: H5Utils.hpp:44
HighFive::details::array_dims::value
static constexpr size_t value
Definition: H5Utils.hpp:45
HighFive::details::is_c_array
Definition: H5Utils.hpp:218
HighFive::details::get_dim_vector
std::vector< size_t > get_dim_vector(const std::vector< T > &vec)
Definition: H5Utils.hpp:109
HighFive::details::is_container::value
static const bool value
Definition: H5Utils.hpp:208
HighFive::details::type_of_array< std::array< T, N > >::type
type_of_array< T >::type type
Definition: H5Utils.hpp:147
HighFive::FixedLenStringArray
A structure representing a set of fixed-length strings.
Definition: H5_definitions.hpp:42
HighFive::details::to_vector_size_t
std::vector< std::size_t > to_vector_size_t(const std::vector< Size > &vec)
Definition: H5Utils.hpp:235
HighFive::details::is_c_array::value
static const bool value
Definition: H5Utils.hpp:219
HighFive::details::type_char_array::type
void type
Definition: H5Utils.hpp:183
HighFive::details::type_of_array< std::vector< T > >::type
type_of_array< T >::type type
Definition: H5Utils.hpp:142
HighFive::details::type_of_array::type
unqualified_t< T > type
Definition: H5Utils.hpp:137
HighFive::details::type_char_array< T[N]>::type
std::conditional< std::is_same< unqualified_t< T >, char >::value, char[N], typename type_char_array< T >::type >::type type
Definition: H5Utils.hpp:201
HighFive::details::type_of_array< T[N]>::type
type_of_array< T >::type type
Definition: H5Utils.hpp:176
HighFive::details::type_char_array
Definition: H5Utils.hpp:182
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
HighFive::details::unqualified_t
typename std::remove_const< typename std::remove_reference< T >::type >::type unqualified_t
Definition: H5Utils.hpp:132
HighFive::details::get_dim_vector_rec
void get_dim_vector_rec(const T &, std::vector< size_t > &)
Definition: H5Utils.hpp:100
HighFive
Definition: H5_definitions.hpp:15