Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
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, typename std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
118 BasicBitBuffer<typename T::allocator_type> serialize(const T& data, ARGS&&... arguments)
119 {
120  return serialize(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
121 }
122 
148 template <typename T, typename... ARGS, typename std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
150 {
151  return serializeToBytes(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
152 }
153 
174 template <typename T, typename ALLOC, typename... ARGS>
175 View<T> deserialize(const BasicBitBuffer<ALLOC>& buffer, T& data, ARGS&&... arguments)
176 {
177  BitStreamReader reader(buffer);
178 
179  return detail::read(reader, data, std::forward<ARGS>(arguments)...);
180 }
181 
202 template <typename T, typename... ARGS>
203 View<T> deserializeFromBytes(Span<const uint8_t> buffer, T& data, ARGS&&... arguments)
204 {
205  BitStreamReader reader(buffer);
206 
207  return detail::read(reader, data, std::forward<ARGS>(arguments)...);
208 }
209 
235 template <typename T, typename ALLOC>
236 BasicBitBuffer<ALLOC> serialize(const View<T>& view, const ALLOC& allocator)
237 {
238  detail::validate(view, "");
239  const BitSize bitSize = detail::initializeOffsets(view, 0);
240  BasicBitBuffer<ALLOC> buffer(bitSize, allocator);
241  BitStreamWriter writer(buffer);
242  detail::write(writer, view);
243 
244  return buffer;
245 }
246 
272 template <typename T, typename ALLOC>
273 Vector<uint8_t, ALLOC> serializeToBytes(const View<T>& view, const ALLOC& allocator)
274 {
275  detail::validate(view, "");
276  const BitSize bitSize = detail::initializeOffsets(view, 0);
277  Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
278  BitStreamWriter writer(buffer);
279  detail::write(writer, view);
280 
281  return buffer;
282 }
283 
307 template <typename T>
309 {
310  return serialize(view, typename T::allocator_type());
311 }
312 
334 template <typename T>
336 {
337  return serializeToBytes(view, typename T::allocator_type());
338 }
339 
364 template <typename T, typename ALLOC>
365 BasicBitBuffer<ALLOC> serialize(const DataView<T>& dataView, const ALLOC& allocator)
366 {
367  // there is no need to set offsets or call validation here, DataView is already consistent
368  size_t bitSize = detail::bitSizeOf(dataView, 0);
369  BasicBitBuffer<ALLOC> buffer(bitSize, allocator);
370  BitStreamWriter writer(buffer);
371 
372  detail::write(writer, dataView);
373  return buffer;
374 }
375 
400 template <typename T, typename ALLOC>
401 Vector<uint8_t, ALLOC> serializeToBytes(const DataView<T>& dataView, const ALLOC& allocator)
402 {
403  // there is no need to set offsets or call validation here, DataView is already consistent
404  size_t bitSize = detail::bitSizeOf(dataView, 0);
405  Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
406  BitStreamWriter writer(buffer);
407 
408  detail::write(writer, dataView);
409  return buffer;
410 }
411 
434 template <typename T>
436 {
437  return serialize(dataView, typename T::allocator_type());
438 }
439 
461 template <typename T>
463 {
464  return serializeToBytes(dataView, typename T::allocator_type());
465 }
466 
488 template <typename T, typename ALLOC, typename... ARGS>
490  const BasicBitBuffer<ALLOC>& buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
491 {
492  BitStreamReader reader(buffer);
493  T data{allocator};
494 
495  return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
496 }
497 
519 template <typename T, typename... ARGS>
521  Span<const uint8_t> buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
522 {
523  BitStreamReader reader(buffer);
524  T data{allocator};
525 
526  return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
527 }
528 
549 template <typename T, typename ALLOC, typename... ARGS,
550  std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
551 DataView<T> deserialize(const BasicBitBuffer<ALLOC>& buffer, ARGS&&... arguments)
552 {
553  return deserialize<T>(buffer, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
554 }
555 
574 template <typename T, typename... ARGS, std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
576 {
577  return deserializeFromBytes<T>(buffer, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
578 }
579 
597 template <typename T>
598 void serializeToFile(const View<T>& view, std::string_view fileName)
599 {
600  const auto bitBuffer = serialize(view);
601  writeBufferToFile(bitBuffer, fileName);
602 }
603 
621 template <typename T>
622 void serializeToFile(const DataView<T>& dataView, std::string_view fileName)
623 {
624  const auto bitBuffer = serialize(dataView);
625  writeBufferToFile(bitBuffer, fileName);
626 }
627 
645 template <typename T, typename... ARGS>
646 void serializeToFile(const T& data, std::string_view fileName, ARGS&&... arguments)
647 {
648  const View<T> view(data, std::forward<ARGS>(arguments)...);
649  serializeToFile(view, fileName);
650 }
651 
674 template <typename T, typename... ARGS>
675 View<T> deserializeFromFile(std::string_view fileName, T& data, ARGS&&... arguments)
676 {
677  const BitBuffer bitBuffer = readBufferFromFile(fileName);
678  return deserialize(bitBuffer, data, std::forward<ARGS>(arguments)...);
679 }
680 
703 template <typename T, typename... ARGS>
704 DataView<T> deserializeFromFile(std::string_view fileName, ARGS&&... arguments)
705 {
706  const BitBuffer bitBuffer = readBufferFromFile(fileName);
707  return deserialize<T>(bitBuffer, std::forward<ARGS>(arguments)...);
708 }
709 
710 } // namespace zserio
711 
712 #endif // ZSERIO_SERIALIZE_UTIL_H_INC
View< T > deserializeFromFile(std::string_view fileName, T &data, ARGS &&... arguments)
View< T > deserializeFromBytes(Span< const uint8_t > buffer, T &data, ARGS &&... arguments)
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
Vector< uint8_t, ALLOC > serializeToBytes(const T &data, const ALLOC &allocator, ARGS &&... arguments)
Definition: SerializeUtil.h:87
View< T > deserialize(const BasicBitBuffer< ALLOC > &buffer, T &data, ARGS &&... arguments)
BitBuffer readBufferFromFile(std::string_view fileName)
Definition: FileUtil.cpp:25
constexpr bool is_first_allocator_v
Definition: Traits.h:114