diff --git a/include/highfive/boost.hpp b/include/highfive/boost.hpp index 33c1458df..64a67c67b 100644 --- a/include/highfive/boost.hpp +++ b/include/highfive/boost.hpp @@ -1,176 +1,4 @@ #pragma once -#include "bits/H5Inspector_decl.hpp" -#include "H5Exception.hpp" - -#include -#include - -namespace HighFive { -namespace details { - -template -struct inspector> { - using type = boost::multi_array; - using value_type = T; - using base_type = typename inspector::base_type; - using hdf5_type = typename inspector::hdf5_type; - - static constexpr size_t ndim = Dims; - static constexpr size_t min_ndim = ndim + inspector::min_ndim; - static constexpr size_t max_ndim = ndim + inspector::max_ndim; - - static constexpr bool is_trivially_copyable = std::is_trivially_copyable::value && - inspector::is_trivially_nestable; - static constexpr bool is_trivially_nestable = false; - - - static size_t getRank(const type& val) { - return ndim + inspector::getRank(val.data()[0]); - } - - static std::vector getDimensions(const type& val) { - auto rank = getRank(val); - std::vector sizes(rank, 1ul); - for (size_t i = 0; i < ndim; ++i) { - sizes[i] = val.shape()[i]; - } - if (val.size() != 0) { - auto s = inspector::getDimensions(val.data()[0]); - sizes.resize(ndim + s.size()); - for (size_t i = 0; i < s.size(); ++i) { - sizes[ndim + i] = s[i]; - } - } - return sizes; - } - - static void prepare(type& val, const std::vector& dims) { - if (dims.size() < ndim) { - std::ostringstream os; - os << "Only '" << dims.size() << "' given but boost::multi_array is of size '" << ndim - << "'."; - throw DataSpaceException(os.str()); - } - boost::array ext; - std::copy(dims.begin(), dims.begin() + ndim, ext.begin()); - val.resize(ext); - std::vector next_dims(dims.begin() + Dims, dims.end()); - std::size_t size = std::accumulate(dims.begin(), - dims.begin() + Dims, - std::size_t{1}, - std::multiplies()); - for (size_t i = 0; i < size; ++i) { - inspector::prepare(*(val.origin() + i), next_dims); - } - } - - static void assert_c_order(const type& val) { - if (!(val.storage_order() == boost::c_storage_order())) { - throw DataTypeException("Only C storage order is supported for 'boost::multi_array'."); - } - } - - static hdf5_type* data(type& val) { - assert_c_order(val); - return inspector::data(*val.data()); - } - - static const hdf5_type* data(const type& val) { - assert_c_order(val); - return inspector::data(*val.data()); - } - - template - static void serialize(const type& val, const std::vector& dims, It m) { - assert_c_order(val); - size_t size = val.num_elements(); - auto subdims = std::vector(dims.begin() + ndim, dims.end()); - size_t subsize = compute_total_size(subdims); - for (size_t i = 0; i < size; ++i) { - inspector::serialize(*(val.origin() + i), subdims, m + i * subsize); - } - } - - template - static void unserialize(It vec_align, const std::vector& dims, type& val) { - assert_c_order(val); - std::vector next_dims(dims.begin() + ndim, dims.end()); - size_t subsize = compute_total_size(next_dims); - for (size_t i = 0; i < val.num_elements(); ++i) { - inspector::unserialize(vec_align + i * subsize, - next_dims, - *(val.origin() + i)); - } - } -}; - -template -struct inspector> { - using type = boost::numeric::ublas::matrix; - using value_type = unqualified_t; - using base_type = typename inspector::base_type; - using hdf5_type = typename inspector::hdf5_type; - - static constexpr size_t ndim = 2; - static constexpr size_t min_ndim = ndim + inspector::min_ndim; - static constexpr size_t max_ndim = ndim + inspector::max_ndim; - - static constexpr bool is_trivially_copyable = std::is_trivially_copyable::value && - inspector::is_trivially_copyable; - static constexpr bool is_trivially_nestable = false; - - static size_t getRank(const type& val) { - return ndim + inspector::getRank(val(0, 0)); - } - - static std::vector getDimensions(const type& val) { - std::vector sizes{val.size1(), val.size2()}; - auto s = inspector::getDimensions(val(0, 0)); - sizes.insert(sizes.end(), s.begin(), s.end()); - return sizes; - } - - static void prepare(type& val, const std::vector& dims) { - if (dims.size() < ndim) { - std::ostringstream os; - os << "Impossible to pair DataSet with " << dims.size() << " dimensions into a " << ndim - << " boost::numeric::ublas::matrix"; - throw DataSpaceException(os.str()); - } - val.resize(dims[0], dims[1], false); - } - - static hdf5_type* data(type& val) { - return inspector::data(val(0, 0)); - } - - static const hdf5_type* data(const type& val) { - return inspector::data(val(0, 0)); - } - - static void serialize(const type& val, const std::vector& dims, hdf5_type* m) { - size_t size = val.size1() * val.size2(); - auto subdims = std::vector(dims.begin() + ndim, dims.end()); - size_t subsize = compute_total_size(subdims); - for (size_t i = 0; i < size; ++i) { - inspector::serialize(*(&val(0, 0) + i), subdims, m + i * subsize); - } - } - - static void unserialize(const hdf5_type* vec_align, - const std::vector& dims, - type& val) { - std::vector next_dims(dims.begin() + ndim, dims.end()); - size_t subsize = compute_total_size(next_dims); - size_t size = val.size1() * val.size2(); - for (size_t i = 0; i < size; ++i) { - inspector::unserialize(vec_align + i * subsize, - next_dims, - *(&val(0, 0) + i)); - } - } -}; - -} // namespace details -} // namespace HighFive +#include "boost_ublas.hpp" +#include "boost_multi_array.hpp" diff --git a/include/highfive/boost_multi_array.hpp b/include/highfive/boost_multi_array.hpp new file mode 100644 index 000000000..5a07d973c --- /dev/null +++ b/include/highfive/boost_multi_array.hpp @@ -0,0 +1,108 @@ +#pragma once + +#include "bits/H5Inspector_decl.hpp" +#include "H5Exception.hpp" + +#include + +namespace HighFive { +namespace details { + +template +struct inspector> { + using type = boost::multi_array; + using value_type = T; + using base_type = typename inspector::base_type; + using hdf5_type = typename inspector::hdf5_type; + + static constexpr size_t ndim = Dims; + static constexpr size_t min_ndim = ndim + inspector::min_ndim; + static constexpr size_t max_ndim = ndim + inspector::max_ndim; + + static constexpr bool is_trivially_copyable = std::is_trivially_copyable::value && + inspector::is_trivially_nestable; + static constexpr bool is_trivially_nestable = false; + + + static size_t getRank(const type& val) { + return ndim + inspector::getRank(val.data()[0]); + } + + static std::vector getDimensions(const type& val) { + auto rank = getRank(val); + std::vector sizes(rank, 1ul); + for (size_t i = 0; i < ndim; ++i) { + sizes[i] = val.shape()[i]; + } + if (val.size() != 0) { + auto s = inspector::getDimensions(val.data()[0]); + sizes.resize(ndim + s.size()); + for (size_t i = 0; i < s.size(); ++i) { + sizes[ndim + i] = s[i]; + } + } + return sizes; + } + + static void prepare(type& val, const std::vector& dims) { + if (dims.size() < ndim) { + std::ostringstream os; + os << "Only '" << dims.size() << "' given but boost::multi_array is of size '" << ndim + << "'."; + throw DataSpaceException(os.str()); + } + boost::array ext; + std::copy(dims.begin(), dims.begin() + ndim, ext.begin()); + val.resize(ext); + std::vector next_dims(dims.begin() + Dims, dims.end()); + std::size_t size = std::accumulate(dims.begin(), + dims.begin() + Dims, + std::size_t{1}, + std::multiplies()); + for (size_t i = 0; i < size; ++i) { + inspector::prepare(*(val.origin() + i), next_dims); + } + } + + static void assert_c_order(const type& val) { + if (!(val.storage_order() == boost::c_storage_order())) { + throw DataTypeException("Only C storage order is supported for 'boost::multi_array'."); + } + } + + static hdf5_type* data(type& val) { + assert_c_order(val); + return inspector::data(*val.data()); + } + + static const hdf5_type* data(const type& val) { + assert_c_order(val); + return inspector::data(*val.data()); + } + + template + static void serialize(const type& val, const std::vector& dims, It m) { + assert_c_order(val); + size_t size = val.num_elements(); + auto subdims = std::vector(dims.begin() + ndim, dims.end()); + size_t subsize = compute_total_size(subdims); + for (size_t i = 0; i < size; ++i) { + inspector::serialize(*(val.origin() + i), subdims, m + i * subsize); + } + } + + template + static void unserialize(It vec_align, const std::vector& dims, type& val) { + assert_c_order(val); + std::vector next_dims(dims.begin() + ndim, dims.end()); + size_t subsize = compute_total_size(next_dims); + for (size_t i = 0; i < val.num_elements(); ++i) { + inspector::unserialize(vec_align + i * subsize, + next_dims, + *(val.origin() + i)); + } + } +}; + +} // namespace details +} // namespace HighFive diff --git a/include/highfive/boost_ublas.hpp b/include/highfive/boost_ublas.hpp new file mode 100644 index 000000000..e3b5b717d --- /dev/null +++ b/include/highfive/boost_ublas.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include "bits/H5Inspector_decl.hpp" +#include "H5Exception.hpp" + +#include + +namespace HighFive { +namespace details { + +template +struct inspector> { + using type = boost::numeric::ublas::matrix; + using value_type = unqualified_t; + using base_type = typename inspector::base_type; + using hdf5_type = typename inspector::hdf5_type; + + static constexpr size_t ndim = 2; + static constexpr size_t min_ndim = ndim + inspector::min_ndim; + static constexpr size_t max_ndim = ndim + inspector::max_ndim; + + static constexpr bool is_trivially_copyable = std::is_trivially_copyable::value && + inspector::is_trivially_copyable; + static constexpr bool is_trivially_nestable = false; + + static size_t getRank(const type& val) { + return ndim + inspector::getRank(val(0, 0)); + } + + static std::vector getDimensions(const type& val) { + std::vector sizes{val.size1(), val.size2()}; + auto s = inspector::getDimensions(val(0, 0)); + sizes.insert(sizes.end(), s.begin(), s.end()); + return sizes; + } + + static void prepare(type& val, const std::vector& dims) { + if (dims.size() < ndim) { + std::ostringstream os; + os << "Impossible to pair DataSet with " << dims.size() << " dimensions into a " << ndim + << " boost::numeric::ublas::matrix"; + throw DataSpaceException(os.str()); + } + val.resize(dims[0], dims[1], false); + } + + static hdf5_type* data(type& val) { + return inspector::data(val(0, 0)); + } + + static const hdf5_type* data(const type& val) { + return inspector::data(val(0, 0)); + } + + static void serialize(const type& val, const std::vector& dims, hdf5_type* m) { + size_t size = val.size1() * val.size2(); + auto subdims = std::vector(dims.begin() + ndim, dims.end()); + size_t subsize = compute_total_size(subdims); + for (size_t i = 0; i < size; ++i) { + inspector::serialize(*(&val(0, 0) + i), subdims, m + i * subsize); + } + } + + static void unserialize(const hdf5_type* vec_align, + const std::vector& dims, + type& val) { + std::vector next_dims(dims.begin() + ndim, dims.end()); + size_t subsize = compute_total_size(next_dims); + size_t size = val.size1() * val.size2(); + for (size_t i = 0; i < size; ++i) { + inspector::unserialize(vec_align + i * subsize, + next_dims, + *(&val(0, 0) + i)); + } + } +}; + +} // namespace details +} // namespace HighFive diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 323b63ab8..3f9a8d532 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -51,7 +51,7 @@ foreach(PUBLIC_HEADER ${public_headers}) continue() endif() - if(PUBLIC_HEADER STREQUAL "highfive/boost.hpp" AND NOT HIGHFIVE_TEST_BOOST) + if(PUBLIC_HEADER MATCHES "highfive/boost.*.hpp" AND NOT HIGHFIVE_TEST_BOOST) continue() endif()