HighFive 2.10.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Converter_misc.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Blue Brain Project
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#pragma once
10
11#include <type_traits>
12
13#include "H5Inspector_misc.hpp"
14#include "../H5DataType.hpp"
15
16namespace HighFive {
17namespace details {
18
19template <class T>
20struct is_std_string {
21 static constexpr bool value =
22 std::is_same<typename inspector<T>::base_type, std::string>::value;
23};
24
25template <class T, class V = void>
26struct enable_shallow_copy
27 : public std::enable_if<!is_std_string<T>::value && inspector<T>::is_trivially_copyable, V> {};
28
29template <class T, class V = void>
30struct enable_deep_copy
31 : public std::enable_if<!is_std_string<T>::value && !inspector<T>::is_trivially_copyable, V> {};
32
33template <class T, class V = void>
34struct enable_string_copy: public std::enable_if<is_std_string<T>::value, V> {};
35
36
37template <typename T, bool IsReadOnly>
38struct ShallowCopyBuffer {
39 using type = unqualified_t<T>;
40 using hdf5_type =
41 typename std::conditional<IsReadOnly,
42 typename std::add_const<typename inspector<T>::hdf5_type>::type,
43 typename inspector<T>::hdf5_type>::type;
44
45 ShallowCopyBuffer() = delete;
46
47 explicit ShallowCopyBuffer(typename std::conditional<IsReadOnly, const T&, T&>::type val)
48 : ptr(inspector<T>::data(val)){};
49
50 hdf5_type* getPointer() const {
51 return ptr;
52 }
53
54 hdf5_type* begin() const {
55 return getPointer();
56 }
57
58 void unserialize(T& /* val */) const {
59 /* nothing to do. */
60 }
61
62 private:
63 hdf5_type* ptr;
64};
65
66template <class T>
67struct DeepCopyBuffer {
68 using type = unqualified_t<T>;
69 using hdf5_type = typename inspector<type>::hdf5_type;
70
71 explicit DeepCopyBuffer(const std::vector<size_t>& _dims)
72 : buffer(inspector<T>::getSize(_dims))
73 , dims(_dims) {}
74
75 hdf5_type* getPointer() {
76 return buffer.data();
77 }
78
79 hdf5_type const* getPointer() const {
80 return buffer.data();
81 }
82
83 hdf5_type* begin() {
84 return getPointer();
85 }
86
87 hdf5_type const* begin() const {
88 return getPointer();
89 }
90
91 void unserialize(T& val) const {
92 inspector<type>::unserialize(buffer.data(), dims, val);
93 }
94
95 private:
96 std::vector<hdf5_type> buffer;
97 std::vector<size_t> dims;
98};
99
100enum class BufferMode { Read, Write };
101
102
106inline size_t char_buffer_length(char const* const str, size_t max_string_size) {
107 for (size_t i = 0; i < max_string_size; ++i) {
108 if (str[i] == '\0') {
109 return i;
110 }
111 }
112
113 return max_string_size;
114}
115
116
164template <typename T, BufferMode buffer_mode>
165struct StringBuffer {
166 using type = unqualified_t<T>;
167 using hdf5_type = typename inspector<type>::hdf5_type;
168
169 class StringView {
170 public:
171 StringView(StringBuffer<T, buffer_mode>& _buffer, size_t _i)
172 : buffer(_buffer)
173 , i(_i) {}
174
182 void assign(char const* data, size_t length, StringPadding pad) {
183 if (buffer.isVariableLengthString()) {
185 buffer.variable_length_pointers[i] = data;
186 } else {
187 buffer.variable_length_buffer[i] = std::string(data, length);
188 buffer.variable_length_pointers[i] = buffer.variable_length_buffer[i].data();
189 }
190 } else if (buffer.isFixedLengthString()) {
191 // If the buffer is fixed-length and null-terminated, then
192 // `buffer.string_length` doesn't include the null-character.
193 if (length > buffer.string_max_length) {
194 throw std::invalid_argument("String length too big.");
195 }
196
197 memcpy(&buffer.fixed_length_buffer[i * buffer.string_size], data, length);
198 }
199 }
200
201 private:
202 StringBuffer<T, buffer_mode>& buffer;
203 size_t i;
204 };
205
206
207 class StringConstView {
208 public:
209 StringConstView(const StringBuffer<T, buffer_mode>& _buffer, size_t _i)
210 : buffer(_buffer)
211 , i(_i) {}
212
216 char const* data() const {
217 if (buffer.isVariableLengthString()) {
218 return buffer.variable_length_pointers[i];
219 } else {
220 return &buffer.fixed_length_buffer[i * buffer.string_size];
221 }
222 }
223
230 size_t length() const {
231 if (buffer.isNullTerminated()) {
232 return char_buffer_length(data(), buffer.string_size);
233 } else {
234 return buffer.string_max_length;
235 }
236 }
237
238 private:
239 const StringBuffer<T, buffer_mode>& buffer;
240 size_t i;
241 };
242
243
244 class Iterator {
245 public:
246 Iterator(StringBuffer<T, buffer_mode>& _buffer, size_t _pos)
247 : buffer(_buffer)
248 , pos(_pos) {}
249
250 Iterator operator+(size_t n_strings) const {
251 return Iterator(buffer, pos + n_strings);
252 }
253
254 void operator+=(size_t n_strings) {
255 pos += n_strings;
256 }
257
258 StringView operator*() {
259 return StringView(buffer, pos);
260 }
261
262 StringConstView operator*() const {
263 return StringConstView(buffer, pos);
264 }
265
266 private:
267 StringBuffer<T, buffer_mode>& buffer;
268 size_t pos;
269 };
270
271 StringBuffer(std::vector<size_t> _dims, const DataType& _file_datatype)
272 : file_datatype(_file_datatype.asStringType())
273 , padding(file_datatype.getPadding())
274 , string_size(file_datatype.isVariableStr() ? size_t(-1) : file_datatype.getSize())
275 , string_max_length(string_size - size_t(isNullTerminated()))
276 , dims(_dims) {
277 if (string_size == 0 && isNullTerminated()) {
278 throw DataTypeException(
279 "Fixed-length, null-terminated need at least one byte to store the "
280 "null-character.");
281 }
282
283 auto n_strings = compute_total_size(dims);
284 if (isVariableLengthString()) {
285 variable_length_buffer.resize(n_strings);
286 variable_length_pointers.resize(n_strings);
287 } else {
288 char pad = padding == StringPadding::SpacePadded ? ' ' : '\0';
289 fixed_length_buffer.assign(n_strings * string_size, pad);
290 }
291 }
292
293 bool isVariableLengthString() const {
294 return file_datatype.isVariableStr();
295 }
296
297 bool isFixedLengthString() const {
298 return file_datatype.isFixedLenStr();
299 }
300
301 bool isNullTerminated() const {
302 return file_datatype.getPadding() == StringPadding::NullTerminated;
303 }
304
305
306 void* getPointer() {
307 if (file_datatype.isVariableStr()) {
308 return variable_length_pointers.data();
309 } else {
310 return fixed_length_buffer.data();
311 }
312 }
313
314 Iterator begin() {
315 return Iterator(*this, 0ul);
316 }
317
318 void unserialize(T& val) {
319 inspector<type>::unserialize(begin(), dims, val);
320 }
321
322 private:
323 StringType file_datatype;
324 StringPadding padding;
325 // Size of buffer required to store the string.
326 // Meaningful for fixed length strings only.
327 size_t string_size;
328 // Maximum length of string.
329 size_t string_max_length;
330 std::vector<size_t> dims;
331
332 std::vector<char> fixed_length_buffer;
333 std::vector<std::string> variable_length_buffer;
334 std::vector<
335 typename std::conditional<buffer_mode == BufferMode::Write, const char, char>::type*>
336 variable_length_pointers;
337};
338
339
340template <typename T, typename Enable = void>
341struct Writer;
342
343template <typename T>
344struct Writer<T, typename enable_shallow_copy<T>::type>: public ShallowCopyBuffer<T, true> {
345 private:
346 using super = ShallowCopyBuffer<T, true>;
347
348 public:
349 explicit Writer(const T& val, const DataType& /* file_datatype */)
350 : super(val){};
351};
352
353template <typename T>
354struct Writer<T, typename enable_deep_copy<T>::type>: public DeepCopyBuffer<T> {
355 explicit Writer(const T& val, const DataType& /* file_datatype */)
356 : DeepCopyBuffer<T>(inspector<T>::getDimensions(val)) {
357 inspector<T>::serialize(val, this->begin());
358 }
359};
360
361template <typename T>
362struct Writer<T, typename enable_string_copy<T>::type>: public StringBuffer<T, BufferMode::Write> {
363 explicit Writer(const T& val, const DataType& _file_datatype)
364 : StringBuffer<T, BufferMode::Write>(inspector<T>::getDimensions(val), _file_datatype) {
365 inspector<T>::serialize(val, this->begin());
366 }
367};
368
369template <typename T, typename Enable = void>
370struct Reader;
371
372template <typename T>
373struct Reader<T, typename enable_shallow_copy<T>::type>: public ShallowCopyBuffer<T, false> {
374 private:
375 using super = ShallowCopyBuffer<T, false>;
376 using type = typename super::type;
377
378 public:
379 Reader(const std::vector<size_t>&, type& val, const DataType& /* file_datatype */)
380 : super(val) {}
381};
382
383template <typename T>
384struct Reader<T, typename enable_deep_copy<T>::type>: public DeepCopyBuffer<T> {
385 private:
386 using super = DeepCopyBuffer<T>;
387 using type = typename super::type;
388
389 public:
390 Reader(const std::vector<size_t>& _dims, type&, const DataType& /* file_datatype */)
391 : super(_dims) {}
392};
393
394
395template <typename T>
396struct Reader<T, typename enable_string_copy<T>::type>: public StringBuffer<T, BufferMode::Write> {
397 public:
398 explicit Reader(const std::vector<size_t>& _dims,
399 const T& /* val */,
400 const DataType& _file_datatype)
401 : StringBuffer<T, BufferMode::Write>(_dims, _file_datatype) {}
402};
403
404struct data_converter {
405 template <typename T>
406 static Writer<T> serialize(const typename inspector<T>::type& val,
407 const DataType& file_datatype) {
408 return Writer<T>(val, file_datatype);
409 }
410
411 template <typename T>
412 static Reader<T> get_reader(const std::vector<size_t>& dims,
413 T& val,
414 const DataType& file_datatype) {
415 // TODO Use bufferinfo for recursive_ndim
416 auto effective_dims = details::squeezeDimensions(dims, inspector<T>::recursive_ndim);
417 inspector<T>::prepare(val, effective_dims);
418 return Reader<T>(effective_dims, val, file_datatype);
419 }
420};
421
422} // namespace details
423} // namespace HighFive
size_t getSize(const File &file, const std::string &path)
Definition H5Easy_public.hpp:82
Definition H5_definitions.hpp:22
StringPadding
Definition string_padding.hpp:7
size_t compute_total_size(const std::vector< size_t > &dims)
Definition H5Inspector_decl.hpp:10