File indexing completed on 2025-04-19 08:55:35
0001 #pragma once
0002 #ifdef H5_USE_EIGEN
0003
0004 #include "bits/H5Inspector_decl.hpp"
0005 #include "H5Exception.hpp"
0006
0007 #include <Eigen/Eigen>
0008
0009
0010 namespace HighFive {
0011 namespace details {
0012
0013 template <typename T, int M, int N>
0014 struct inspector<Eigen::Matrix<T, M, N>> {
0015 using type = Eigen::Matrix<T, M, N>;
0016 using value_type = T;
0017 using base_type = typename inspector<value_type>::base_type;
0018 using hdf5_type = base_type;
0019
0020 static constexpr size_t ndim = 2;
0021 static constexpr size_t recursive_ndim = ndim + inspector<value_type>::recursive_ndim;
0022 static constexpr bool is_trivially_copyable = std::is_trivially_copyable<value_type>::value &&
0023 inspector<value_type>::is_trivially_copyable;
0024
0025
0026 static void assert_not_buggy(Eigen::Index nrows, Eigen::Index ncols) {
0027 if (nrows > 1 && ncols > 1) {
0028 throw std::runtime_error(
0029 "HighFive has been broken for Eigen::Matrix. Please check "
0030 "https://github.com/BlueBrain/HighFive/issues/532.");
0031 }
0032 }
0033
0034 static std::vector<size_t> getDimensions(const type& val) {
0035 assert_not_buggy(val.rows(), val.cols());
0036
0037 std::vector<size_t> sizes{static_cast<size_t>(val.rows()), static_cast<size_t>(val.cols())};
0038 auto s = inspector<value_type>::getDimensions(val.data()[0]);
0039 sizes.insert(sizes.end(), s.begin(), s.end());
0040 return sizes;
0041 }
0042
0043 static size_t getSizeVal(const type& val) {
0044 return compute_total_size(getDimensions(val));
0045 }
0046
0047 static size_t getSize(const std::vector<size_t>& dims) {
0048 return compute_total_size(dims);
0049 }
0050
0051 static void prepare(type& val, const std::vector<size_t>& dims) {
0052 if (dims[0] != static_cast<size_t>(val.rows()) ||
0053 dims[1] != static_cast<size_t>(val.cols())) {
0054 val.resize(static_cast<typename type::Index>(dims[0]),
0055 static_cast<typename type::Index>(dims[1]));
0056 }
0057
0058 assert_not_buggy(val.rows(), val.cols());
0059 }
0060
0061 static hdf5_type* data(type& val) {
0062 assert_not_buggy(val.rows(), val.cols());
0063 return inspector<value_type>::data(*val.data());
0064 }
0065
0066 static const hdf5_type* data(const type& val) {
0067 assert_not_buggy(val.rows(), val.cols());
0068 return inspector<value_type>::data(*val.data());
0069 }
0070
0071 static void serialize(const type& val, hdf5_type* m) {
0072 assert_not_buggy(val.rows(), val.cols());
0073 std::memcpy(m, val.data(), static_cast<size_t>(val.size()) * sizeof(hdf5_type));
0074 }
0075
0076 static void unserialize(const hdf5_type* vec_align,
0077 const std::vector<size_t>& dims,
0078 type& val) {
0079 assert_not_buggy(val.rows(), val.cols());
0080 if (dims.size() < 2) {
0081 std::ostringstream os;
0082 os << "Impossible to pair DataSet with " << dims.size()
0083 << " dimensions into an eigen-matrix.";
0084 throw DataSpaceException(os.str());
0085 }
0086 std::memcpy(val.data(), vec_align, compute_total_size(dims) * sizeof(hdf5_type));
0087 }
0088 };
0089
0090 }
0091 }
0092
0093 #endif