Zserio C++ runtime library  1.4.0
Built for Zserio 2.18.1
SerializeUtil.h
Go to the documentation of this file.
1 
10 #ifndef ZSERIO_SERIALIZE_UTIL_H_INC
11 #define ZSERIO_SERIALIZE_UTIL_H_INC
12 
13 #include "zserio/BitStreamReader.h"
14 #include "zserio/BitStreamWriter.h"
15 #include "zserio/FileUtil.h"
16 #include "zserio/Traits.h"
17 #include "zserio/Types.h"
18 #include "zserio/Vector.h"
19 
20 namespace zserio
21 {
22 
23 namespace detail
24 {
25 
26 template <typename T>
27 void initializeChildrenImpl(std::true_type, T& object)
28 {
29  object.initializeChildren();
30 }
31 
32 template <typename T>
33 void initializeChildrenImpl(std::false_type, T&)
34 {}
35 
36 template <typename T>
37 void initializeChildren(T& object)
38 {
39  initializeChildrenImpl(has_initialize_children<T>(), object);
40 }
41 
42 template <typename T, typename... ARGS>
43 void initializeImpl(std::true_type, T& object, ARGS&&... arguments)
44 {
45  object.initialize(std::forward<ARGS>(arguments)...);
46 }
47 
48 template <typename T>
49 void initializeImpl(std::false_type, T& object)
50 {
51  initializeChildren(object);
52 }
53 
54 template <typename T, typename... ARGS>
55 void initialize(T& object, ARGS&&... arguments)
56 {
57  initializeImpl(has_initialize<T>(), object, std::forward<ARGS>(arguments)...);
58 }
59 
60 template <typename T, typename = void>
61 struct allocator_chooser
62 {
63  using type = std::allocator<uint8_t>;
64 };
65 
66 template <typename T>
67 struct allocator_chooser<T, detail::void_t<typename T::allocator_type>>
68 {
69  using type = typename T::allocator_type;
70 };
71 
72 // This implementation needs to be in detail because old MSVC compiler 2015 has problems with calling overload.
73 template <typename T, typename ALLOC, typename... ARGS>
74 BasicBitBuffer<ALLOC> serialize(T& object, const ALLOC& allocator, ARGS&&... arguments)
75 {
76  detail::initialize(object, std::forward<ARGS>(arguments)...);
77  BasicBitBuffer<ALLOC> bitBuffer(object.initializeOffsets(), allocator);
78  BitStreamWriter writer(bitBuffer);
79  object.write(writer);
80  return bitBuffer;
81 }
82 
83 } // namespace detail
84 
110 template <typename T, typename ALLOC, typename... ARGS,
111  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
112 BasicBitBuffer<ALLOC> serialize(T& object, const ALLOC& allocator, ARGS&&... arguments)
113 {
114  return detail::serialize(object, allocator, std::forward<ARGS>(arguments)...);
115 }
116 
138 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename... ARGS,
139  typename std::enable_if<!std::is_enum<T>::value &&
140  !is_first_allocator<typename std::decay<ARGS>::type...>::value,
141  int>::type = 0>
142 BasicBitBuffer<ALLOC> serialize(T& object, ARGS&&... arguments)
143 {
144  return detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
145 }
146 
165 template <typename T, typename ALLOC = std::allocator<uint8_t>,
166  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
167 BasicBitBuffer<ALLOC> serialize(T enumValue, const ALLOC& allocator = ALLOC())
168 {
169  BasicBitBuffer<ALLOC> bitBuffer(zserio::bitSizeOf(enumValue), allocator);
170  BitStreamWriter writer(bitBuffer);
171  zserio::write(writer, enumValue);
172  return bitBuffer;
173 }
174 
194 template <typename T, typename ALLOC, typename... ARGS>
195 typename std::enable_if<!std::is_enum<T>::value && !is_first_array_preallocation<ARGS...>::value, T>::type
196 deserialize(const BasicBitBuffer<ALLOC>& bitBuffer, ARGS&&... arguments)
197 {
198  BitStreamReader reader(bitBuffer);
199  return T(reader, std::forward<ARGS>(arguments)...);
200 }
201 
223 template <typename T, typename ALLOC, typename... ARGS>
224 typename std::enable_if<!std::is_enum<T>::value, T>::type deserialize(
225  const BasicBitBuffer<ALLOC>& bitBuffer, ArrayPreallocation maxArrayPrealloc, ARGS&&... arguments)
226 {
227  BitStreamReader reader(bitBuffer, maxArrayPrealloc);
228  return T(reader, std::forward<ARGS>(arguments)...);
229 }
230 
249 template <typename T, typename ALLOC>
250 typename std::enable_if<std::is_enum<T>::value, T>::type deserialize(const BasicBitBuffer<ALLOC>& bitBuffer)
251 {
252  BitStreamReader reader(bitBuffer);
253  return zserio::read<T>(reader);
254 }
255 
281 template <typename T, typename ALLOC, typename... ARGS,
282  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
283 vector<uint8_t, ALLOC> serializeToBytes(T& object, const ALLOC& allocator, ARGS&&... arguments)
284 {
285  const BasicBitBuffer<ALLOC> bitBuffer =
286  detail::serialize(object, allocator, std::forward<ARGS>(arguments)...);
287 
288  return bitBuffer.getBytes();
289 }
290 
315 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename... ARGS,
316  typename std::enable_if<!std::is_enum<T>::value &&
317  !is_first_allocator<typename std::decay<ARGS>::type...>::value,
318  int>::type = 0>
319 vector<uint8_t, ALLOC> serializeToBytes(T& object, ARGS&&... arguments)
320 {
321  const BasicBitBuffer<ALLOC> bitBuffer =
322  detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
323 
324  return bitBuffer.getBytes();
325 }
326 
345 template <typename T, typename ALLOC = std::allocator<uint8_t>,
346  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
347 vector<uint8_t, ALLOC> serializeToBytes(T enumValue, const ALLOC& allocator = ALLOC())
348 {
349  const BasicBitBuffer<ALLOC> bitBuffer = serialize(enumValue, allocator);
350 
351  return bitBuffer.getBytes();
352 }
353 
377 template <typename T, typename... ARGS>
378 typename std::enable_if<!std::is_enum<T>::value && !is_first_array_preallocation<ARGS...>::value, T>::type
379 deserializeFromBytes(Span<const uint8_t> buffer, ARGS&&... arguments)
380 {
381  BitStreamReader reader(buffer);
382  return T(reader, std::forward<ARGS>(arguments)...);
383 }
384 
409 template <typename T, typename... ARGS>
410 typename std::enable_if<!std::is_enum<T>::value, T>::type deserializeFromBytes(
411  Span<const uint8_t> buffer, ArrayPreallocation maxArrayPrealloc, ARGS&&... arguments)
412 {
413  BitStreamReader reader(buffer, maxArrayPrealloc);
414  return T(reader, std::forward<ARGS>(arguments)...);
415 }
416 
435 template <typename T>
436 typename std::enable_if<std::is_enum<T>::value, T>::type deserializeFromBytes(Span<const uint8_t> buffer)
437 {
438  BitStreamReader reader(buffer);
439  return zserio::read<T>(reader);
440 }
441 
458 template <typename T, typename... ARGS>
459 void serializeToFile(T& object, const std::string& fileName, ARGS&&... arguments)
460 {
461  const auto bitBuffer = serialize(object, std::forward<ARGS>(arguments)...);
462  writeBufferToFile(bitBuffer, fileName);
463 }
464 
487 template <typename T, typename... ARGS>
488 typename std::enable_if<!is_first_array_preallocation<ARGS...>::value, T>::type deserializeFromFile(
489  const std::string& fileName, ARGS&&... arguments)
490 {
491  const BitBuffer bitBuffer = readBufferFromFile(fileName);
492  return deserialize<T>(bitBuffer, std::forward<ARGS>(arguments)...);
493 }
494 
518 template <typename T, typename... ARGS>
519 T deserializeFromFile(const std::string& fileName, ArrayPreallocation maxArrayPrealloc, ARGS&&... arguments)
520 {
521  const BitBuffer bitBuffer = readBufferFromFile(fileName);
522  return deserialize<T>(bitBuffer, maxArrayPrealloc, std::forward<ARGS>(arguments)...);
523 }
524 
525 } // namespace zserio
526 
527 #endif // ZSERIO_SERIALIZE_UTIL_H_INC
const vector< uint8_t, ALLOC > & getBytes() const
Definition: BitBuffer.h:414
zserio::string< PropagatingPolymorphicAllocator< char > > string
Definition: String.h:15
std::enable_if<!is_first_array_preallocation< ARGS... >::value, T >::type deserializeFromFile(const std::string &fileName, ARGS &&... arguments)
vector< uint8_t, ALLOC > serializeToBytes(T &object, const ALLOC &allocator, ARGS &&... arguments)
void write(BitStreamWriter &out, T value)
void writeBufferToFile(const uint8_t *buffer, size_t bitSize, BitsTag, const std::string &fileName)
Definition: FileUtil.cpp:10
BitBuffer readBufferFromFile(const std::string &fileName)
Definition: FileUtil.cpp:25
void serializeToFile(T &object, const std::string &fileName, ARGS &&... arguments)
std::vector< T, RebindAlloc< ALLOC, T > > vector
Definition: Vector.h:17
BasicBitBuffer< ALLOC > serialize(T &object, const ALLOC &allocator, ARGS &&... arguments)
std::enable_if<!std::is_enum< T >::value &&!is_first_array_preallocation< ARGS... >::value, T >::type deserialize(const BasicBitBuffer< ALLOC > &bitBuffer, ARGS &&... arguments)
size_t initializeOffsets(size_t bitPosition, T value)
std::enable_if<!std::is_enum< T >::value &&!is_first_array_preallocation< ARGS... >::value, T >::type deserializeFromBytes(Span< const uint8_t > buffer, ARGS &&... arguments)
BasicBitBuffer< ALLOC > serialize(T enumValue, const ALLOC &allocator=ALLOC())
size_t bitSizeOf(T value)