HighFive 2.10.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
eigen.hpp
Go to the documentation of this file.
1#pragma once
2#ifdef H5_USE_EIGEN
3
5#include "H5Exception.hpp"
6
7#include <Eigen/Eigen>
8
9
10namespace HighFive {
11namespace details {
12
13template <typename T, int M, int N>
14struct inspector<Eigen::Matrix<T, M, N>> {
15 using type = Eigen::Matrix<T, M, N>;
16 using value_type = T;
17 using base_type = typename inspector<value_type>::base_type;
18 using hdf5_type = base_type;
19
20 static constexpr size_t ndim = 2;
21 static constexpr size_t recursive_ndim = ndim + inspector<value_type>::recursive_ndim;
22 static constexpr bool is_trivially_copyable = std::is_trivially_copyable<value_type>::value &&
23 inspector<value_type>::is_trivially_copyable;
24
25
26 static void assert_not_buggy(Eigen::Index nrows, Eigen::Index ncols) {
27 if (nrows > 1 && ncols > 1) {
28 throw std::runtime_error(
29 "HighFive has been broken for Eigen::Matrix. Please check "
30 "https://github.com/BlueBrain/HighFive/issues/532.");
31 }
32 }
33
34 static std::vector<size_t> getDimensions(const type& val) {
35 assert_not_buggy(val.rows(), val.cols());
36
37 std::vector<size_t> sizes{static_cast<size_t>(val.rows()), static_cast<size_t>(val.cols())};
38 auto s = inspector<value_type>::getDimensions(val.data()[0]);
39 sizes.insert(sizes.end(), s.begin(), s.end());
40 return sizes;
41 }
42
43 static size_t getSizeVal(const type& val) {
44 return compute_total_size(getDimensions(val));
45 }
46
47 static size_t getSize(const std::vector<size_t>& dims) {
48 return compute_total_size(dims);
49 }
50
51 static void prepare(type& val, const std::vector<size_t>& dims) {
52 if (dims[0] != static_cast<size_t>(val.rows()) ||
53 dims[1] != static_cast<size_t>(val.cols())) {
54 val.resize(static_cast<typename type::Index>(dims[0]),
55 static_cast<typename type::Index>(dims[1]));
56 }
57
58 assert_not_buggy(val.rows(), val.cols());
59 }
60
61 static hdf5_type* data(type& val) {
62 assert_not_buggy(val.rows(), val.cols());
63 return inspector<value_type>::data(*val.data());
64 }
65
66 static const hdf5_type* data(const type& val) {
67 assert_not_buggy(val.rows(), val.cols());
68 return inspector<value_type>::data(*val.data());
69 }
70
71 static void serialize(const type& val, hdf5_type* m) {
72 assert_not_buggy(val.rows(), val.cols());
73 std::memcpy(m, val.data(), static_cast<size_t>(val.size()) * sizeof(hdf5_type));
74 }
75
76 static void unserialize(const hdf5_type* vec_align,
77 const std::vector<size_t>& dims,
78 type& val) {
79 assert_not_buggy(val.rows(), val.cols());
80 if (dims.size() < 2) {
81 std::ostringstream os;
82 os << "Impossible to pair DataSet with " << dims.size()
83 << " dimensions into an eigen-matrix.";
84 throw DataSpaceException(os.str());
85 }
86 std::memcpy(val.data(), vec_align, compute_total_size(dims) * sizeof(hdf5_type));
87 }
88};
89
90} // namespace details
91} // namespace HighFive
92
93#endif
size_t getSize(const File &file, const std::string &path)
Get the size of an existing DataSet in an open HDF5 file.
Definition H5Easy_public.hpp:82
Definition H5_definitions.hpp:22
size_t compute_total_size(const std::vector< size_t > &dims)
Definition H5Inspector_decl.hpp:10