Zserio C++17 runtime library  1.1.0
Built for Zserio 2.18.1
SerializeUtil.h
Go to the documentation of this file.
1 
8 #ifndef ZSERIO_SERIALIZE_UTIL_H_INC
9 #define ZSERIO_SERIALIZE_UTIL_H_INC
10 
11 #include <string_view>
12 #include <type_traits>
13 #include <utility>
14 
15 #include "zserio/BitBuffer.h"
16 #include "zserio/BitSize.h"
17 #include "zserio/BitStreamReader.h"
18 #include "zserio/BitStreamWriter.h"
19 #include "zserio/DataView.h"
20 #include "zserio/FileUtil.h"
21 #include "zserio/Traits.h"
22 #include "zserio/View.h"
23 
24 namespace zserio
25 {
26 
52 template <typename T, typename ALLOC, typename... ARGS, std::enable_if_t<is_allocator_v<ALLOC>, int> = 0>
53 BasicBitBuffer<ALLOC> serialize(const T& data, const ALLOC& allocator, ARGS&&... arguments)
54 {
55  const View<T> view(data, std::forward<ARGS>(arguments)...);
56 
57  return serialize(view, allocator);
58 }
59 
86 template <typename T, typename ALLOC, typename... ARGS, std::enable_if_t<is_allocator_v<ALLOC>, int> = 0>
87 Vector<uint8_t, ALLOC> serializeToBytes(const T& data, const ALLOC& allocator, ARGS&&... arguments)
88 {
89  const View<T> view(data, std::forward<ARGS>(arguments)...);
90 
91  return serializeToBytes(view, allocator);
92 }
93 
117 template <typename T, typename... ARGS,
118  typename std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>,
119  int> = 0>
120 BasicBitBuffer<typename T::allocator_type> serialize(const T& data, ARGS&&... arguments)
121 {
122  return serialize(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
123 }
124 
150 template <typename T, typename... ARGS,
151  typename std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>,
152  int> = 0>
154 {
155  return serializeToBytes(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
156 }
157 
180 template <typename T, typename ALLOC, typename... ARGS>
182  const BasicBitBuffer<ALLOC>& buffer, ArrayPreallocation maxArrayPrealloc, T& data, ARGS&&... arguments)
183 {
184  BitStreamReader reader(buffer, maxArrayPrealloc);
185 
186  return detail::read(reader, data, std::forward<ARGS>(arguments)...);
187 }
188 
209 template <typename T, typename ALLOC, typename... ARGS>
210 View<T> deserialize(const BasicBitBuffer<ALLOC>& buffer, T& data, ARGS&&... arguments)
211 {
212  return deserialize<T>(buffer, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
213 }
214 
237 template <typename T, typename... ARGS>
239  Span<const uint8_t> buffer, ArrayPreallocation maxArrayPrealloc, T& data, ARGS&&... arguments)
240 {
241  BitStreamReader reader(buffer, maxArrayPrealloc);
242 
243  return detail::read(reader, data, std::forward<ARGS>(arguments)...);
244 }
245 
266 template <typename T, typename... ARGS>
267 View<T> deserializeFromBytes(Span<const uint8_t> buffer, T& data, ARGS&&... arguments)
268 {
269  return deserializeFromBytes(buffer, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
270 }
271 
297 template <typename T, typename ALLOC>
298 BasicBitBuffer<ALLOC> serialize(const View<T>& view, const ALLOC& allocator)
299 {
300  detail::validate(view, "");
301  const BitSize bitSize = detail::initializeOffsets(view, 0);
302  BasicBitBuffer<ALLOC> buffer(bitSize, allocator);
303  BitStreamWriter writer(buffer);
304  detail::write(writer, view);
305 
306  return buffer;
307 }
308 
334 template <typename T, typename ALLOC>
335 Vector<uint8_t, ALLOC> serializeToBytes(const View<T>& view, const ALLOC& allocator)
336 {
337  detail::validate(view, "");
338  const BitSize bitSize = detail::initializeOffsets(view, 0);
339  Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
340  BitStreamWriter writer(buffer);
341  detail::write(writer, view);
342 
343  return buffer;
344 }
345 
369 template <typename T>
371 {
372  return serialize(view, typename T::allocator_type());
373 }
374 
396 template <typename T>
398 {
399  return serializeToBytes(view, typename T::allocator_type());
400 }
401 
426 template <typename T, typename ALLOC>
427 BasicBitBuffer<ALLOC> serialize(const DataView<T>& dataView, const ALLOC& allocator)
428 {
429  // there is no need to set offsets or call validation here, DataView is already consistent
430  size_t bitSize = detail::bitSizeOf(dataView, 0);
431  BasicBitBuffer<ALLOC> buffer(bitSize, allocator);
432  BitStreamWriter writer(buffer);
433 
434  detail::write(writer, dataView);
435  return buffer;
436 }
437 
462 template <typename T, typename ALLOC>
463 Vector<uint8_t, ALLOC> serializeToBytes(const DataView<T>& dataView, const ALLOC& allocator)
464 {
465  // there is no need to set offsets or call validation here, DataView is already consistent
466  size_t bitSize = detail::bitSizeOf(dataView, 0);
467  Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
468  BitStreamWriter writer(buffer);
469 
470  detail::write(writer, dataView);
471  return buffer;
472 }
473 
496 template <typename T>
498 {
499  return serialize(dataView, typename T::allocator_type());
500 }
501 
523 template <typename T>
525 {
526  return serializeToBytes(dataView, typename T::allocator_type());
527 }
528 
552 template <typename T, typename ALLOC, typename... ARGS>
554  const typename T::allocator_type& allocator, ARGS&&... arguments)
555 {
556  BitStreamReader reader(buffer, maxArrayPreallocation);
557  T data{allocator};
558 
559  return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
560 }
561 
583 template <typename T, typename ALLOC, typename... ARGS>
585  const BasicBitBuffer<ALLOC>& buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
586 {
587  return deserialize<T>(buffer, ArrayPreallocation(), allocator, std::forward<ARGS>(arguments)...);
588 }
589 
612 template <typename T, typename... ARGS>
614  const typename T::allocator_type& allocator, ARGS&&... arguments)
615 {
616  BitStreamReader reader(buffer, maxArrayPreallocation);
617  T data{allocator};
618 
619  return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
620 }
621 
643 template <typename T, typename... ARGS>
645  Span<const uint8_t> buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
646 {
647  return deserializeFromBytes<T>(buffer, ArrayPreallocation(), allocator, std::forward<ARGS>(arguments)...);
648 }
649 
671 template <typename T, typename ALLOC, typename... ARGS,
672  std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
674  const BasicBitBuffer<ALLOC>& buffer, ArrayPreallocation maxArrayAlloc, ARGS&&... arguments)
675 {
676  return deserialize<T>(
677  buffer, maxArrayAlloc, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
678 }
679 
700 template <typename T, typename ALLOC, typename... ARGS,
701  std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>, int> = 0>
702 DataView<T> deserialize(const BasicBitBuffer<ALLOC>& buffer, ARGS&&... arguments)
703 {
704  return deserialize<T>(buffer, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
705 }
706 
726 template <typename T, typename... ARGS, std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
728  Span<const uint8_t> buffer, ArrayPreallocation maxArrayPrealloc, ARGS&&... arguments)
729 {
730  return deserializeFromBytes<T>(
731  buffer, maxArrayPrealloc, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
732 }
733 
752 template <typename T, typename... ARGS,
753  std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>, int> = 0>
755 {
756  return deserializeFromBytes<T>(buffer, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
757 }
758 
776 template <typename T>
777 void serializeToFile(const View<T>& view, std::string_view fileName)
778 {
779  const auto bitBuffer = serialize(view);
780  writeBufferToFile(bitBuffer, fileName);
781 }
782 
800 template <typename T>
801 void serializeToFile(const DataView<T>& dataView, std::string_view fileName)
802 {
803  const auto bitBuffer = serialize(dataView);
804  writeBufferToFile(bitBuffer, fileName);
805 }
806 
824 template <typename T, typename... ARGS>
825 void serializeToFile(const T& data, std::string_view fileName, ARGS&&... arguments)
826 {
827  const View<T> view(data, std::forward<ARGS>(arguments)...);
828  serializeToFile(view, fileName);
829 }
830 
855 template <typename T, typename... ARGS>
857  std::string_view fileName, ArrayPreallocation maxArrayPrealloc, T& data, ARGS&&... arguments)
858 {
859  const BitBuffer bitBuffer = readBufferFromFile(fileName);
860  return deserialize(bitBuffer, maxArrayPrealloc, data, std::forward<ARGS>(arguments)...);
861 }
862 
885 template <typename T, typename... ARGS>
886 View<T> deserializeFromFile(std::string_view fileName, T& data, ARGS&&... arguments)
887 {
888  return deserializeFromFile(fileName, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
889 }
890 
915 template <typename T, typename... ARGS>
917  std::string_view fileName, ArrayPreallocation maxArrayPrealloc, ARGS&&... arguments)
918 {
919  const BitBuffer bitBuffer = readBufferFromFile(fileName);
920  return deserialize<T>(bitBuffer, maxArrayPrealloc, std::forward<ARGS>(arguments)...);
921 }
922 
945 template <typename T, typename... ARGS>
946 DataView<T> deserializeFromFile(std::string_view fileName, ARGS&&... arguments)
947 {
948  return deserializeFromFile<T>(fileName, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
949 }
950 
951 } // namespace zserio
952 
953 #endif // ZSERIO_SERIALIZE_UTIL_H_INC
unsigned int BitSize
Definition: BitSize.h:8
std::vector< T, ALLOC > Vector
Definition: Vector.h:13
void serializeToFile(const View< T > &view, std::string_view fileName)
BasicBitBuffer< ALLOC > serialize(const T &data, const ALLOC &allocator, ARGS &&... arguments)
Definition: SerializeUtil.h:53
void writeBufferToFile(const uint8_t *buffer, size_t bitSize, BitsTag, std::string_view fileName)
Definition: FileUtil.cpp:10
View< T > deserializeFromBytes(Span< const uint8_t > buffer, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
View< T > deserializeFromFile(std::string_view fileName, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
Vector< uint8_t, ALLOC > serializeToBytes(const T &data, const ALLOC &allocator, ARGS &&... arguments)
Definition: SerializeUtil.h:87
BitBuffer readBufferFromFile(std::string_view fileName)
Definition: FileUtil.cpp:25
constexpr bool is_first_allocator_v
Definition: Traits.h:122
View< T > deserialize(const BasicBitBuffer< ALLOC > &buffer, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
constexpr bool is_first_array_preallocation_v
Definition: Traits.h:154