diff --git a/include/highfive/bits/H5Attribute_misc.hpp b/include/highfive/bits/H5Attribute_misc.hpp index 62f6ebd82..042c63014 100644 --- a/include/highfive/bits/H5Attribute_misc.hpp +++ b/include/highfive/bits/H5Attribute_misc.hpp @@ -120,6 +120,7 @@ inline void Attribute::read_raw(T* array) const { template inline void Attribute::write(const T& buffer) { const DataSpace& mem_space = getMemSpace(); + auto dims = mem_space.getDimensions(); if (mem_space.getElementCount() == 0) { return; @@ -138,7 +139,7 @@ inline void Attribute::write(const T& buffer) { << " into dataset of dimensions " << mem_space.getNumberDimensions(); throw DataSpaceException(ss.str()); } - auto w = details::data_converter::serialize(buffer, file_datatype); + auto w = details::data_converter::serialize(buffer, dims, file_datatype); write_raw(w.getPointer(), buffer_info.data_type); } diff --git a/include/highfive/bits/H5Converter_misc.hpp b/include/highfive/bits/H5Converter_misc.hpp index ed387702f..2f84a7bec 100644 --- a/include/highfive/bits/H5Converter_misc.hpp +++ b/include/highfive/bits/H5Converter_misc.hpp @@ -344,23 +344,27 @@ struct Writer::type>: public ShallowCopyBuffe using super = ShallowCopyBuffer; public: - explicit Writer(const T& val, const DataType& /* file_datatype */) + explicit Writer(const T& val, + const std::vector& /* dims */, + const DataType& /* file_datatype */) : super(val){}; }; template struct Writer::type>: public DeepCopyBuffer { - explicit Writer(const T& val, const DataType& /* file_datatype */) - : DeepCopyBuffer(inspector::getDimensions(val)) { - inspector::serialize(val, this->begin()); + explicit Writer(const T& val, + const std::vector& _dims, + const DataType& /* file_datatype */) + : DeepCopyBuffer(_dims) { + inspector::serialize(val, _dims, this->begin()); } }; template struct Writer::type>: public StringBuffer { - explicit Writer(const T& val, const DataType& _file_datatype) - : StringBuffer(inspector::getDimensions(val), _file_datatype) { - inspector::serialize(val, this->begin()); + explicit Writer(const T& val, const std::vector& _dims, const DataType& _file_datatype) + : StringBuffer(_dims, _file_datatype) { + inspector::serialize(val, _dims, this->begin()); } }; @@ -402,8 +406,9 @@ struct Reader::type>: public StringBuffer static Writer serialize(const typename inspector::type& val, + const std::vector& dims, const DataType& file_datatype) { - return Writer(val, file_datatype); + return Writer(val, dims, file_datatype); } template diff --git a/include/highfive/bits/H5Inspector_misc.hpp b/include/highfive/bits/H5Inspector_misc.hpp index 49606005f..a1f1e0a3a 100644 --- a/include/highfive/bits/H5Inspector_misc.hpp +++ b/include/highfive/bits/H5Inspector_misc.hpp @@ -149,7 +149,7 @@ inspector { // Return a point of the first value of val static const hdf5_type* data(const type& val) // Take a val and serialize it inside 'out' - static void serialize(const type& val, hdf5_type* out) + static void serialize(const type& val, const std::vector& dims, hdf5_type* out) // Return an array of dimensions of the space needed for writing val static std::vector getDimensions(const type& val) } @@ -191,7 +191,7 @@ struct type_helper { return &val; } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& /* dims*/, hdf5_type* m) { static_assert(is_trivially_copyable, "The type is not trivially copyable"); *m = val; } @@ -233,7 +233,7 @@ struct inspector: type_helper { val = vec[0] != 0 ? true : false; } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& /* dims*/, hdf5_type* m) { *m = val ? 1 : 0; } }; @@ -251,7 +251,7 @@ struct inspector: type_helper { } template - static void serialize(const type& val, It m) { + static void serialize(const type& val, const std::vector& /* dims*/, It m) { (*m).assign(val.data(), val.size(), StringPadding::NullTerminated); } @@ -276,7 +276,7 @@ struct inspector: type_helper { throw DataSpaceException("A Reference cannot be written directly."); } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& /* dims*/, hdf5_type* m) { hobj_ref_t ref; val.create_ref(&ref); *m = ref; @@ -339,11 +339,12 @@ struct inspector> { } template - static void serialize(const type& val, It m) { + static void serialize(const type& val, const std::vector& dims, It m) { if (!val.empty()) { size_t subsize = inspector::getSizeVal(val[0]); + auto subdims = std::vector(dims.begin() + 1, dims.end()); for (auto&& e: val) { - inspector::serialize(e, m); + inspector::serialize(e, subdims, m); m += subsize; } } @@ -401,7 +402,7 @@ struct inspector> { throw DataSpaceException("A std::vector cannot be written directly."); } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& /* dims*/, hdf5_type* m) { for (size_t i = 0; i < val.size(); ++i) { m[i] = val[i] ? 1 : 0; } @@ -468,10 +469,11 @@ struct inspector> { } template - static void serialize(const type& val, It m) { + static void serialize(const type& val, const std::vector& dims, It m) { size_t subsize = inspector::getSizeVal(val[0]); + auto subdims = std::vector(dims.begin() + 1, dims.end()); for (auto& e: val) { - inspector::serialize(e, m); + inspector::serialize(e, subdims, m); m += subsize; } } @@ -519,7 +521,9 @@ struct inspector { /* it works because there is only T[][][] currently we will fix it one day */ - static void serialize(const type& /* val */, hdf5_type* /* m */) { + static void serialize(const type& /* val */, + const std::vector& /* dims*/, + hdf5_type* /* m */) { throw DataSpaceException("Not possible to serialize a T*"); } }; @@ -575,10 +579,11 @@ struct inspector { /* it works because there is only T[][][] currently we will fix it one day */ - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& dims, hdf5_type* m) { size_t subsize = inspector::getSizeVal(val[0]); + auto subdims = std::vector(dims.begin() + 1, dims.end()); for (size_t i = 0; i < N; ++i) { - inspector::serialize(val[i], m + i * subsize); + inspector::serialize(val[i], subdims, m + i * subsize); } } }; diff --git a/include/highfive/bits/H5Slice_traits_misc.hpp b/include/highfive/bits/H5Slice_traits_misc.hpp index 27c103ae2..2ae6640b0 100644 --- a/include/highfive/bits/H5Slice_traits_misc.hpp +++ b/include/highfive/bits/H5Slice_traits_misc.hpp @@ -242,6 +242,7 @@ template inline void SliceTraits::write(const T& buffer, const DataTransferProps& xfer_props) { const auto& slice = static_cast(*this); const DataSpace& mem_space = slice.getMemSpace(); + auto dims = mem_space.getDimensions(); auto file_datatype = slice.getDataType(); @@ -257,7 +258,7 @@ inline void SliceTraits::write(const T& buffer, const DataTransferProp << " into dataset with n = " << buffer_info.n_dimensions << " dimensions."; throw DataSpaceException(ss.str()); } - auto w = details::data_converter::serialize(buffer, file_datatype); + auto w = details::data_converter::serialize(buffer, dims, file_datatype); write_raw(w.getPointer(), buffer_info.data_type, xfer_props); } diff --git a/include/highfive/boost.hpp b/include/highfive/boost.hpp index a4364faf3..e9b89675d 100644 --- a/include/highfive/boost.hpp +++ b/include/highfive/boost.hpp @@ -68,11 +68,12 @@ struct inspector> { } template - static void serialize(const type& val, It m) { + static void serialize(const type& val, const std::vector& dims, It m) { size_t size = val.num_elements(); size_t subsize = inspector::getSizeVal(*val.origin()); + auto subdims = std::vector(dims.begin() + ndim, dims.end()); for (size_t i = 0; i < size; ++i) { - inspector::serialize(*(val.origin() + i), m + i * subsize); + inspector::serialize(*(val.origin() + i), subdims, m + i * subsize); } } @@ -133,11 +134,12 @@ struct inspector> { return inspector::data(val(0, 0)); } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& dims, hdf5_type* m) { size_t size = val.size1() * val.size2(); size_t subsize = inspector::getSizeVal(val(0, 0)); + auto subdims = std::vector(dims.begin() + ndim, dims.end()); for (size_t i = 0; i < size; ++i) { - inspector::serialize(*(&val(0, 0) + i), m + i * subsize); + inspector::serialize(*(&val(0, 0) + i), subdims, m + i * subsize); } } diff --git a/include/highfive/eigen.hpp b/include/highfive/eigen.hpp index f91dab24c..2aee101fc 100644 --- a/include/highfive/eigen.hpp +++ b/include/highfive/eigen.hpp @@ -67,7 +67,7 @@ struct inspector> { return inspector::data(*val.data()); } - static void serialize(const type& val, hdf5_type* m) { + static void serialize(const type& val, const std::vector& /* dims */, hdf5_type* m) { assert_not_buggy(val.rows(), val.cols()); std::memcpy(m, val.data(), static_cast(val.size()) * sizeof(hdf5_type)); }