17template <
class ElementType,
class Extents,
class LayoutPolicy,
class AccessorPolicy>
18struct inspector<std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>> {
19 using type = std::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>;
20 using value_type =
typename type::element_type;
21 using base_type =
typename inspector<value_type>::base_type;
22 using hdf5_type = base_type;
23 using extents_type =
typename type::extents_type;
24 using accessor_type =
typename type::accessor_type;
26 static constexpr size_t ndim = type::rank();
27 static constexpr size_t min_ndim = ndim + inspector<value_type>::min_ndim;
28 static constexpr size_t max_ndim = ndim + inspector<value_type>::max_ndim;
30 static constexpr bool is_trivially_copyable =
31 std::is_trivially_copyable<value_type>::value &&
32 inspector<value_type>::is_trivially_nestable &&
33 (std::is_same_v<std::default_accessor<value_type>, accessor_type>
34#ifdef __cpp_lib_aligned_accessor
35 || std::is_same_v<std::aligned_accessor<value_type>, accessor_type>
38 (std::is_same_v<typename type::layout_type, std::layout_right> ||
39 (std::is_same_v<typename type::layout_type, std::layout_left> && ndim == 1));
40 static constexpr bool is_trivially_nestable =
false;
43 using index_type =
typename extents_type::index_type;
46 static auto get_first_element(
const type& val) {
47 std::array<index_type, ndim> indices{};
52 static auto data_impl(T& val) ->
decltype(inspector<value_type>::data(*val.data_handle())) {
53 if (!is_trivially_copyable) {
54 throw DataSetException(
"Invalid use of `inspector<std::mdspan<...>>::data`.");
61 return inspector<value_type>::data(*val.data_handle());
65 static size_t getRank(
const type& val) {
69 return ndim + inspector<value_type>::getRank(get_first_element(val));
72 static std::vector<size_t> getDimensions(
const type& val) {
73 std::vector<size_t> sizes;
75 for (
size_t r = 0; r < ndim; ++r) {
76 sizes.push_back(val.extent(r));
79 auto s = inspector<value_type>::getDimensions(get_first_element(val));
80 sizes.insert(sizes.end(), s.begin(), s.end());
85 static void prepare(type& val,
const std::vector<size_t>& dims) {
86 if (dims.size() < ndim) {
87 std::ostringstream os;
88 os <<
"Impossible to pair DataSet with " << dims.size()
89 <<
" dimensions into an mdspan with rank " << ndim <<
".";
90 throw DataSpaceException(os.str());
94 for (
size_t r = 0; r < ndim; ++r) {
95 if (dims[r] != val.extent(r)) {
96 std::ostringstream os;
97 os <<
"Mismatching dimensions for mdspan: expected " << val.extent(r)
98 <<
" for dimension " << r <<
", but got " << dims[r] <<
".";
99 throw DataSpaceException(os.str());
104 static hdf5_type* data(type& val) {
105 return data_impl(val);
108 static const hdf5_type* data(
const type& val) {
109 return data_impl(val);
112 static void serialize(
const type& val,
const std::vector<size_t>& dims, hdf5_type* m) {
113 auto subdims = std::vector<size_t>(dims.begin() + ndim, dims.end());
116 std::array<index_type, ndim> indices{};
117 auto iterate = [&](
auto& self,
size_t dim) ->
void {
120 inspector<value_type>::serialize(val[indices], subdims, m);
124 const auto n =
static_cast<index_type
>(val.extent(dim));
125 for (indices[dim] = 0; indices[dim] < n; ++indices[dim]) {
134 static void unserialize(
const hdf5_type* vec_align,
135 const std::vector<size_t>& dims,
137 if (dims.size() < ndim) {
138 std::ostringstream os;
139 os <<
"Impossible to pair DataSet with " << dims.size()
140 <<
" dimensions into an mdspan with rank " << ndim <<
".";
141 throw DataSpaceException(os.str());
144 auto subdims = std::vector<size_t>(dims.begin() + ndim, dims.end());
147 std::array<index_type, ndim> indices{};
148 auto iterate = [&](
auto& self,
size_t dim) ->
void {
151 inspector<value_type>::unserialize(vec_align, subdims, val[indices]);
152 vec_align += subsize;
155 const auto n =
static_cast<index_type
>(dims[dim]);
156 for (indices[dim] = 0; indices[dim] < n; ++indices[dim]) {
Definition assert_compatible_spaces.hpp:15
size_t compute_total_size(const std::vector< size_t > &dims)
Definition compute_total_size.hpp:10