Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.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/Vector.h"
18 
19 namespace zserio
20 {
21 
22 namespace detail
23 {
24 
25 template <typename T>
26 void initializeChildrenImpl(std::true_type, T& object)
27 {
28  object.initializeChildren();
29 }
30 
31 template <typename T>
32 void initializeChildrenImpl(std::false_type, T&)
33 {}
34 
35 template <typename T>
36 void initializeChildren(T& object)
37 {
38  initializeChildrenImpl(has_initialize_children<T>(), object);
39 }
40 
41 template <typename T, typename... ARGS>
42 void initializeImpl(std::true_type, T& object, ARGS&&... arguments)
43 {
44  object.initialize(std::forward<ARGS>(arguments)...);
45 }
46 
47 template <typename T>
48 void initializeImpl(std::false_type, T& object)
49 {
50  initializeChildren(object);
51 }
52 
53 template <typename T, typename... ARGS>
54 void initialize(T& object, ARGS&&... arguments)
55 {
56  initializeImpl(has_initialize<T>(), object, std::forward<ARGS>(arguments)...);
57 }
58 
59 template <typename T, typename = void>
60 struct allocator_chooser
61 {
62  using type = std::allocator<uint8_t>;
63 };
64 
65 template <typename T>
66 struct allocator_chooser<T, detail::void_t<typename T::allocator_type>>
67 {
68  using type = typename T::allocator_type;
69 };
70 
71 // This implementation needs to be in detail because old MSVC compiler 2015 has problems with calling overload.
72 template <typename T, typename ALLOC, typename... ARGS>
73 BasicBitBuffer<ALLOC> serialize(T& object, const ALLOC& allocator, ARGS&&... arguments)
74 {
75  detail::initialize(object, std::forward<ARGS>(arguments)...);
76  BasicBitBuffer<ALLOC> bitBuffer(object.initializeOffsets(), allocator);
77  BitStreamWriter writer(bitBuffer);
78  object.write(writer);
79  return bitBuffer;
80 }
81 
82 } // namespace detail
83 
109 template <typename T, typename ALLOC, typename... ARGS,
110  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
111 BasicBitBuffer<ALLOC> serialize(T& object, const ALLOC& allocator, ARGS&&... arguments)
112 {
113  return detail::serialize(object, allocator, std::forward<ARGS>(arguments)...);
114 }
115 
137 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename... ARGS,
138  typename std::enable_if<!std::is_enum<T>::value &&
139  !is_first_allocator<typename std::decay<ARGS>::type...>::value,
140  int>::type = 0>
141 BasicBitBuffer<ALLOC> serialize(T& object, ARGS&&... arguments)
142 {
143  return detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
144 }
145 
164 template <typename T, typename ALLOC = std::allocator<uint8_t>,
165  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
166 BasicBitBuffer<ALLOC> serialize(T enumValue, const ALLOC& allocator = ALLOC())
167 {
168  BasicBitBuffer<ALLOC> bitBuffer(zserio::bitSizeOf(enumValue), allocator);
169  BitStreamWriter writer(bitBuffer);
170  zserio::write(writer, enumValue);
171  return bitBuffer;
172 }
173 
193 template <typename T, typename ALLOC, typename... ARGS>
194 typename std::enable_if<!std::is_enum<T>::value, T>::type deserialize(
195  const BasicBitBuffer<ALLOC>& bitBuffer, ARGS&&... arguments)
196 {
197  BitStreamReader reader(bitBuffer);
198  return T(reader, std::forward<ARGS>(arguments)...);
199 }
200 
219 template <typename T, typename ALLOC>
220 typename std::enable_if<std::is_enum<T>::value, T>::type deserialize(const BasicBitBuffer<ALLOC>& bitBuffer)
221 {
222  BitStreamReader reader(bitBuffer);
223  return zserio::read<T>(reader);
224 }
225 
251 template <typename T, typename ALLOC, typename... ARGS,
252  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
253 vector<uint8_t, ALLOC> serializeToBytes(T& object, const ALLOC& allocator, ARGS&&... arguments)
254 {
255  const BasicBitBuffer<ALLOC> bitBuffer =
256  detail::serialize(object, allocator, std::forward<ARGS>(arguments)...);
257 
258  return bitBuffer.getBytes();
259 }
260 
285 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename... ARGS,
286  typename std::enable_if<!std::is_enum<T>::value &&
287  !is_first_allocator<typename std::decay<ARGS>::type...>::value,
288  int>::type = 0>
289 vector<uint8_t, ALLOC> serializeToBytes(T& object, ARGS&&... arguments)
290 {
291  const BasicBitBuffer<ALLOC> bitBuffer =
292  detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
293 
294  return bitBuffer.getBytes();
295 }
296 
315 template <typename T, typename ALLOC = std::allocator<uint8_t>,
316  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
317 vector<uint8_t, ALLOC> serializeToBytes(T enumValue, const ALLOC& allocator = ALLOC())
318 {
319  const BasicBitBuffer<ALLOC> bitBuffer = serialize(enumValue, allocator);
320 
321  return bitBuffer.getBytes();
322 }
323 
347 template <typename T, typename... ARGS>
348 typename std::enable_if<!std::is_enum<T>::value, T>::type deserializeFromBytes(
349  Span<const uint8_t> buffer, ARGS&&... arguments)
350 {
351  BitStreamReader reader(buffer);
352  return T(reader, std::forward<ARGS>(arguments)...);
353 }
354 
373 template <typename T>
374 typename std::enable_if<std::is_enum<T>::value, T>::type deserializeFromBytes(Span<const uint8_t> buffer)
375 {
376  BitStreamReader reader(buffer);
377  return zserio::read<T>(reader);
378 }
379 
396 template <typename T, typename... ARGS>
397 void serializeToFile(T& object, const std::string& fileName, ARGS&&... arguments)
398 {
399  const auto bitBuffer = serialize(object, std::forward<ARGS>(arguments)...);
400  writeBufferToFile(bitBuffer, fileName);
401 }
402 
425 template <typename T, typename... ARGS>
426 T deserializeFromFile(const std::string& fileName, ARGS&&... arguments)
427 {
428  const BitBuffer bitBuffer = readBufferFromFile(fileName);
429  return deserialize<T>(bitBuffer, std::forward<ARGS>(arguments)...);
430 }
431 
432 } // namespace zserio
433 
434 #endif // ZSERIO_SERIALIZE_UTIL_H_INC
const vector< uint8_t, ALLOC > & getBytes() const
Definition: BitBuffer.h:415
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)