HighFive  2.2.2
HighFive - Header-only C++ HDF5 interface
H5Easy_xtensor.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 H5EASY_BITS_XTENSOR_HPP
10 #define H5EASY_BITS_XTENSOR_HPP
11 
12 #include "../H5Easy.hpp"
13 #include "H5Easy_misc.hpp"
14 #include "H5Easy_scalar.hpp"
15 
16 #ifdef H5_USE_XTENSOR
17 
18 namespace H5Easy {
19 
20 namespace detail {
21 
22 template <class T>
23 struct is_xtensor : std::false_type {};
24 template <class T>
25 struct is_xtensor<xt::xarray<T>> : std::true_type {};
26 template <class T, size_t N>
27 struct is_xtensor<xt::xtensor<T, N>> : std::true_type {};
28 
29 template <typename T>
30 struct io_impl<T, typename std::enable_if<is_xtensor<T>::value>::type> {
31 
32  inline static std::vector<size_t> shape(const T& data) {
33  return std::vector<size_t>(data.shape().cbegin(), data.shape().cend());
34  }
35 
36  inline static DataSet dump(File& file,
37  const std::string& path,
38  const T& data,
39  const DumpOptions& options) {
40  using value_type = typename std::decay_t<T>::value_type;
41  DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
42  dataset.write_raw(data.data());
43  if (options.flush()) {
44  file.flush();
45  }
46  return dataset;
47  }
48 
49  inline static T load(const File& file, const std::string& path) {
50  DataSet dataset = file.getDataSet(path);
51  std::vector<size_t> dims = dataset.getDimensions();
52  T data = T::from_shape(dims);
53  dataset.read(data.data());
54  return data;
55  }
56 
57  inline static Attribute dumpAttribute(File& file,
58  const std::string& path,
59  const std::string& key,
60  const T& data,
61  const DumpOptions& options) {
62  using value_type = typename std::decay_t<T>::value_type;
63  Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
64  attribute.write_raw(data.data());
65  if (options.flush()) {
66  file.flush();
67  }
68  return attribute;
69  }
70 
71  inline static T loadAttribute(const File& file,
72  const std::string& path,
73  const std::string& key) {
74  DataSet dataset = file.getDataSet(path);
75  Attribute attribute = dataset.getAttribute(key);
76  DataSpace dataspace = attribute.getSpace();
77  std::vector<size_t> dims = dataspace.getDimensions();
78  T data = T::from_shape(dims);
79  attribute.read(data.data());
80  return data;
81  }
82 };
83 
84 } // namespace detail
85 } // namespace H5Easy
86 
87 #endif // H5_USE_XTENSOR
88 #endif // H5EASY_BITS_XTENSOR_HPP
Definition: H5Easy.hpp:51
DataSet dump(File &file, const std::string &path, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) DataSet in an open HDF5 file.
Definition: H5Easy_public.hpp:115
T loadAttribute(const File &file, const std::string &path, const std::string &key)
Load a Attribute in an open HDF5 file to an object (templated).
Definition: H5Easy_public.hpp:185
Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) Attribute in an open HDF5 file.
Definition: H5Easy_public.hpp:167
T load(const File &file, const std::string &path, const std::vector< size_t > &idx)
Load entry "(i,j)" from a rank-two DataSet in an open HDF5 file to a scalar.
Definition: H5Easy_public.hpp:157