Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.0
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, T>::type deserialize(
196  const BasicBitBuffer<ALLOC>& bitBuffer, ARGS&&... arguments)
197 {
198  BitStreamReader reader(bitBuffer);
199  return T(reader, std::forward<ARGS>(arguments)...);
200 }
201 
220 template <typename T, typename ALLOC>
221 typename std::enable_if<std::is_enum<T>::value, T>::type deserialize(const BasicBitBuffer<ALLOC>& bitBuffer)
222 {
223  BitStreamReader reader(bitBuffer);
224  return zserio::read<T>(reader);
225 }
226 
252 template <typename T, typename ALLOC, typename... ARGS,
253  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
254 vector<uint8_t, ALLOC> serializeToBytes(T& object, const ALLOC& allocator, ARGS&&... arguments)
255 {
256  const BasicBitBuffer<ALLOC> bitBuffer =
257  detail::serialize(object, allocator, std::forward<ARGS>(arguments)...);
258 
259  return bitBuffer.getBytes();
260 }
261 
286 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename... ARGS,
287  typename std::enable_if<!std::is_enum<T>::value &&
288  !is_first_allocator<typename std::decay<ARGS>::type...>::value,
289  int>::type = 0>
290 vector<uint8_t, ALLOC> serializeToBytes(T& object, ARGS&&... arguments)
291 {
292  const BasicBitBuffer<ALLOC> bitBuffer =
293  detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
294 
295  return bitBuffer.getBytes();
296 }
297 
316 template <typename T, typename ALLOC = std::allocator<uint8_t>,
317  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
318 vector<uint8_t, ALLOC> serializeToBytes(T enumValue, const ALLOC& allocator = ALLOC())
319 {
320  const BasicBitBuffer<ALLOC> bitBuffer = serialize(enumValue, allocator);
321 
322  return bitBuffer.getBytes();
323 }
324 
348 template <typename T, typename... ARGS>
349 typename std::enable_if<!std::is_enum<T>::value, T>::type deserializeFromBytes(
350  Span<const uint8_t> buffer, ARGS&&... arguments)
351 {
352  BitStreamReader reader(buffer);
353  return T(reader, std::forward<ARGS>(arguments)...);
354 }
355 
374 template <typename T>
375 typename std::enable_if<std::is_enum<T>::value, T>::type deserializeFromBytes(Span<const uint8_t> buffer)
376 {
377  BitStreamReader reader(buffer);
378  return zserio::read<T>(reader);
379 }
380 
397 template <typename T, typename... ARGS>
398 void serializeToFile(T& object, const std::string& fileName, ARGS&&... arguments)
399 {
400  const auto bitBuffer = serialize(object, std::forward<ARGS>(arguments)...);
401  writeBufferToFile(bitBuffer, fileName);
402 }
403 
426 template <typename T, typename... ARGS>
427 T deserializeFromFile(const std::string& fileName, ARGS&&... arguments)
428 {
429  const BitBuffer bitBuffer = readBufferFromFile(fileName);
430  return deserialize<T>(bitBuffer, std::forward<ARGS>(arguments)...);
431 }
432 
433 } // namespace zserio
434 
435 #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<!std::is_enum< T >::value, T >::type deserializeFromBytes(Span< const uint8_t > buffer, 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
std::enable_if<!std::is_enum< T >::value, T >::type deserialize(const BasicBitBuffer< ALLOC > &bitBuffer, ARGS &&... arguments)
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)
size_t initializeOffsets(size_t bitPosition, T value)
T deserializeFromFile(const std::string &fileName, ARGS &&... arguments)
BasicBitBuffer< ALLOC > serialize(T enumValue, const ALLOC &allocator=ALLOC())
size_t bitSizeOf(T value)