Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:08

0001 #pragma once
0002 
0003 #include "H5Inspector_decl.hpp"
0004 #include "../H5Exception.hpp"
0005 
0006 #include <cstdlib>
0007 #include <vector>
0008 #include <type_traits>
0009 
0010 namespace HighFive {
0011 namespace details {
0012 
0013 
0014 // Anything with the same API as `std::span` can implemented by inheriting from
0015 // this class.
0016 template <class Span>
0017 struct inspector_stl_span {
0018     using type = Span;
0019     using value_type = unqualified_t<typename Span::value_type>;
0020     using base_type = typename inspector<value_type>::base_type;
0021     using hdf5_type = typename inspector<value_type>::hdf5_type;
0022 
0023     static constexpr size_t ndim = 1;
0024     static constexpr size_t min_ndim = ndim + inspector<value_type>::min_ndim;
0025     static constexpr size_t max_ndim = ndim + inspector<value_type>::max_ndim;
0026 
0027     static constexpr bool is_trivially_copyable = std::is_trivially_copyable<value_type>::value &&
0028                                                   inspector<value_type>::is_trivially_nestable;
0029     static constexpr bool is_trivially_nestable = false;
0030 
0031 
0032     static size_t getRank(const type& val) {
0033         if (!val.empty()) {
0034             return ndim + inspector<value_type>::getRank(val[0]);
0035         } else {
0036             return min_ndim;
0037         }
0038     }
0039 
0040     static std::vector<size_t> getDimensions(const type& val) {
0041         auto rank = getRank(val);
0042         std::vector<size_t> sizes(rank, 1ul);
0043         sizes[0] = val.size();
0044         if (!val.empty()) {
0045             auto s = inspector<value_type>::getDimensions(val[0]);
0046             assert(s.size() + ndim == sizes.size());
0047             for (size_t i = 0; i < s.size(); ++i) {
0048                 sizes[i + ndim] = s[i];
0049             }
0050         }
0051         return sizes;
0052     }
0053 
0054     static void prepare(type& val, const std::vector<size_t>& expected_dims) {
0055         auto actual_dims = getDimensions(val);
0056         if (actual_dims.size() != expected_dims.size()) {
0057             throw DataSpaceException("Mismatching rank.");
0058         }
0059 
0060         for (size_t i = 0; i < actual_dims.size(); ++i) {
0061             if (actual_dims[i] != expected_dims[i]) {
0062                 throw DataSpaceException("Mismatching dimensions.");
0063             }
0064         }
0065     }
0066 
0067     static hdf5_type* data(type& val) {
0068         return val.empty() ? nullptr : inspector<value_type>::data(val[0]);
0069     }
0070 
0071     static const hdf5_type* data(const type& val) {
0072         return val.empty() ? nullptr : inspector<value_type>::data(val[0]);
0073     }
0074 
0075     template <class It>
0076     static void serialize(const type& val, const std::vector<size_t>& dims, It m) {
0077         if (!val.empty()) {
0078             auto subdims = std::vector<size_t>(dims.begin() + ndim, dims.end());
0079             size_t subsize = compute_total_size(subdims);
0080             for (const auto& e: val) {
0081                 inspector<value_type>::serialize(e, subdims, m);
0082                 m += subsize;
0083             }
0084         }
0085     }
0086 
0087     template <class It>
0088     static void unserialize(const It& vec_align, const std::vector<size_t>& dims, type& val) {
0089         std::vector<size_t> subdims(dims.begin() + ndim, dims.end());
0090         size_t subsize = compute_total_size(subdims);
0091         for (size_t i = 0; i < dims[0]; ++i) {
0092             inspector<value_type>::unserialize(vec_align + i * subsize, subdims, val[i]);
0093         }
0094     }
0095 };
0096 
0097 }  // namespace details
0098 }  // namespace HighFive