diff --git a/include/highfive/bits/H5Attribute_misc.hpp b/include/highfive/bits/H5Attribute_misc.hpp index 33295d40e..887660c9f 100644 --- a/include/highfive/bits/H5Attribute_misc.hpp +++ b/include/highfive/bits/H5Attribute_misc.hpp @@ -120,6 +120,7 @@ inline void Attribute::read(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..c0802c6b4 100644 --- a/include/highfive/bits/H5Converter_misc.hpp +++ b/include/highfive/bits/H5Converter_misc.hpp @@ -344,23 +344,23 @@ 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 +402,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 1613f87c3..4ccd802d9 100644 --- a/include/highfive/bits/H5Inspector_misc.hpp +++ b/include/highfive/bits/H5Inspector_misc.hpp @@ -148,7 +148,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) } @@ -190,7 +190,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; } @@ -232,7 +232,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; } }; @@ -250,7 +250,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); } @@ -275,7 +275,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; @@ -328,7 +328,7 @@ struct inspector> { return val.data(); } - 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) { std::memcpy(m + i * N, val[i], N); } @@ -393,11 +393,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; } } @@ -455,7 +456,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; } @@ -522,10 +523,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; } } @@ -573,7 +575,7 @@ 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*"); } }; @@ -610,10 +612,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 dd7e45b1d..054e558f1 100644 --- a/include/highfive/bits/H5Slice_traits_misc.hpp +++ b/include/highfive/bits/H5Slice_traits_misc.hpp @@ -241,6 +241,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(); @@ -256,7 +257,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 8992159a2..3bb276e5e 100644 --- a/include/highfive/boost.hpp +++ b/include/highfive/boost.hpp @@ -71,11 +71,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); } } @@ -136,11 +137,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 c47095dde..72019f0ca 100644 --- a/include/highfive/eigen.hpp +++ b/include/highfive/eigen.hpp @@ -68,7 +68,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)); }