6 #ifndef ECHELON_HDF5_STORAGE_LAYER_HPP 7 #define ECHELON_HDF5_STORAGE_LAYER_HPP 9 #include <echelon/hdf5/customization_hooks.hpp> 10 #include <echelon/hdf5/type_traits.hpp> 12 #include <echelon/hdf5/container_adaption.hpp> 14 #include <echelon/hdf5/precursor/property_list.hpp> 15 #include <echelon/hdf5/precursor/dataspace.hpp> 16 #include <echelon/hdf5/precursor/type.hpp> 25 #include <type_traits> 28 #include <boost/format.hpp> 36 class inconsistent_selection_size_exception :
public std::exception
39 explicit inconsistent_selection_size_exception(hssize_t mem_selection_size_, hssize_t file_selection_size_)
41 what_ = str(boost::format(
"The number of selected elements in the memory space (%1%) and the file space (%2%) differs.") % mem_selection_size_ % file_selection_size_);
44 const char* what() const noexcept
override 53 template <
typename Sink,
typename C>
54 inline void write_impl(Sink& sink,
const hdf5::precursor::type& datatype,
55 const hdf5::precursor::dataspace& memspace,
56 const hdf5::precursor::dataspace& filespace,
const C& container,
59 static_assert(is_readable_container<C>(),
60 "C does not fulfill the ReadableContainer requirements");
62 using value_type =
typename std::decay<decltype(*data_adl(container))>::type;
64 static_assert(is_hdf5_type<value_type>::value,
"trivially storable types must be HDF5 types.");
66 sink.write(datatype, memspace, filespace, hdf5::precursor::default_property_list,
70 template <
typename Sink,
typename C>
71 void write(Sink& sink,
const hdf5::precursor::type& datatype,
72 const hdf5::precursor::dataspace& memspace,
const hdf5::precursor::dataspace& filespace,
75 template <
typename Sink,
typename C>
76 inline void write_impl(Sink& sink,
const hdf5::precursor::type& datatype,
77 const hdf5::precursor::dataspace& memspace,
78 const hdf5::precursor::dataspace& filespace,
const C& container,
81 static_assert(is_readable_container<C>(),
82 "C does not fulfill the ReadableContainer requirements");
85 typename std::decay<decltype(lower_type_internal(*data_adl(container), {}))>::type;
87 std::size_t buffer_size = memspace.select_npoints();
89 std::vector<lowered_type> lowered_buffer(buffer_size);
91 for (std::size_t i = 0; i < buffer_size; ++i)
93 lowered_buffer[i] = lower_type_internal(data_adl(container)[i], sink);
96 write(sink, datatype, memspace, filespace, lowered_buffer);
99 template <
typename Sink,
typename C>
100 inline void write(Sink& sink,
const hdf5::precursor::type& datatype,
101 const hdf5::precursor::dataspace& memspace,
102 const hdf5::precursor::dataspace& filespace,
const C& container)
104 static_assert(is_readable_container<C>(),
105 "C does not fulfill the ReadableContainer requirements");
107 using value_type =
typename std::decay<decltype(*data_adl(container))>::type;
109 write_impl(sink, datatype, memspace, filespace, container,
110 std::integral_constant<
bool, is_trivially_storable<value_type>()>{});
113 template <
typename Source,
typename C>
114 void read(
const Source& source,
const hdf5::precursor::type& datatype,
115 const hdf5::precursor::dataspace& memspace,
const hdf5::precursor::dataspace& filespace,
118 template <
typename Source,
typename C>
119 inline void read_impl(
const Source& source,
const hdf5::precursor::type& datatype,
120 const hdf5::precursor::dataspace& memspace,
121 const hdf5::precursor::dataspace& filespace, C& container, std::true_type)
123 static_assert(is_container<C>(),
"C does not fulfill the Container requirements");
125 using value_type =
typename std::decay<decltype(*data_adl(container))>::type;
127 static_assert(is_hdf5_type<value_type>::value,
"trivially storable types must be HDF5 types.");
129 auto memory_selection_size = memspace.select_npoints();
130 auto file_selection_size = filespace.select_npoints();
132 if(memory_selection_size != file_selection_size)
133 throw inconsistent_selection_size_exception(memory_selection_size, file_selection_size);
135 source.read(datatype, memspace, filespace, hdf5::precursor::default_property_list,
136 data_adl(container));
139 template <
typename Source,
typename C>
140 inline void read_impl(
const Source& source,
const hdf5::precursor::type& datatype,
141 const hdf5::precursor::dataspace& memspace,
142 const hdf5::precursor::dataspace& filespace, C& container, std::false_type)
144 static_assert(is_container<C>(),
"C does not fulfill the Container requirements");
146 std::vector<hsize_t> mem_shape = memspace.get_simple_extent_dims();
149 typename std::decay<decltype(lower_type_internal(*data_adl(container), {}))>::type;
151 auto memory_selection_size = memspace.select_npoints();
152 auto file_selection_size = filespace.select_npoints();
154 if(memory_selection_size != file_selection_size)
155 throw inconsistent_selection_size_exception(memory_selection_size, file_selection_size);
157 std::vector<lowered_type> lowered_buffer(memory_selection_size);
159 read(source, datatype, memspace, filespace, lowered_buffer);
161 for (decltype(memory_selection_size) i = 0; i < memory_selection_size; ++i)
163 data_adl(container)[i] = raise_type_internal(lowered_buffer[i], source);
166 if (H5Tis_variable_str(datatype.id()) || H5Tget_class(datatype.id()) == H5T_VLEN)
168 H5Dvlen_reclaim(datatype.id(), memspace.id(), H5P_DEFAULT, lowered_buffer.data());
172 template <
typename Source,
typename C>
173 inline void read(
const Source& source,
const hdf5::precursor::type& datatype,
174 const hdf5::precursor::dataspace& memspace,
175 const hdf5::precursor::dataspace& filespace, C& container)
177 static_assert(is_container<C>(),
"C does not fulfill the Container requirements");
179 using value_type =
typename std::decay<decltype(*data_adl(container))>::type;
181 read_impl(source, datatype, memspace, filespace, container,
182 std::integral_constant<
bool, is_trivially_storable<value_type>()>{});
185 template <
typename Sink,
typename T>
186 void write(Sink& sink,
const T& value);
188 template <
typename Sink,
typename T>
189 inline void write_impl(Sink& sink,
const T& value, std::true_type)
191 static_assert(is_hdf5_type<T>::value,
"trivially storable types must be HDF5 types.");
196 template <
typename Sink,
typename T>
197 inline void write_impl(Sink& sink,
const T& value, std::false_type)
199 auto lowered_value = lower_type_internal(value, sink);
201 write(sink, lowered_value);
204 template <
typename Sink,
typename T>
205 inline void write(Sink& sink,
const T& value)
207 write_impl(sink, value, std::integral_constant<
bool, is_trivially_storable<T>()>{});
210 template <
typename Sink,
typename T,
int N>
211 inline void write(Sink& sink,
const T (&value)[N])
213 using value_type =
typename std::decay<T>::type;
215 static_assert(is_trivially_storable<value_type>(),
216 "Storage of arrays of non-trivially storable types is not implemented.");
218 const T* decayed_array = value;
220 sink.write(&decayed_array);
223 template <
typename Source,
typename T>
224 inline void read(Source& source, T& value);
226 template <
typename Source,
typename T>
227 inline void read_impl(Source& source, T& value, std::true_type)
229 static_assert(is_hdf5_type<T>::value,
"trivially storable types must be HDF5 types.");
234 template <
typename Source,
typename T>
235 inline void read_impl(Source& source, T& value, std::false_type)
237 using lowered_value_type =
typename std::decay<decltype(lower_type_internal(value, {}))>::type;
239 lowered_value_type lowered_value;
241 read(source, lowered_value);
243 value = raise_type_internal(lowered_value, source);
245 auto datatype = source.datatype();
247 if (H5Tis_variable_str(datatype.id()) || H5Tget_class(datatype.id()) == H5T_VLEN)
250 H5Dvlen_reclaim(datatype.id(), hdf5::precursor::dataspace().id(), H5P_DEFAULT,
255 template <
typename Source,
typename T>
256 inline void read(Source& source, T& value)
258 using value_type =
typename std::decay<T>::type;
260 read_impl(source, value, std::integral_constant<
bool, is_trivially_storable<value_type>()>{});
263 template <
typename Source,
typename T,
int N>
264 inline void read(Source& source, T (&value)[N])
266 using value_type =
typename std::decay<T>::type;
268 static_assert(is_trivially_storable<value_type>(),
269 "Storage of arrays of non-trivially storable types is not implemented.");
271 T* decayed_array = value;
273 source.read(&decayed_array);
echelon's core namespace
Definition: attribute.cpp:10