Zserio C++ runtime library  1.0.0
Built for Zserio 2.13.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/Vector.h"
16 #include "zserio/FileUtil.h"
17 #include "zserio/Traits.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, int>::type = 0>
140 BasicBitBuffer<ALLOC> serialize(T& object, ARGS&&... arguments)
141 {
142  return detail::serialize(object, ALLOC(), std::forward<ARGS>(arguments)...);
143 }
144 
163 template <typename T, typename ALLOC = std::allocator<uint8_t>,
164  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
165 BasicBitBuffer<ALLOC> serialize(T enumValue, const ALLOC& allocator = ALLOC())
166 {
167  BasicBitBuffer<ALLOC> bitBuffer(zserio::bitSizeOf(enumValue), allocator);
168  BitStreamWriter writer(bitBuffer);
169  zserio::write(writer, enumValue);
170  return bitBuffer;
171 }
172 
192 template <typename T, typename ALLOC, typename ...ARGS>
193 typename std::enable_if<!std::is_enum<T>::value, T>::type deserialize(
194  const BasicBitBuffer<ALLOC>& bitBuffer, ARGS&&... arguments)
195 {
196  BitStreamReader reader(bitBuffer);
197  return T(reader, std::forward<ARGS>(arguments)...);
198 }
199 
218 template <typename T, typename ALLOC>
219 typename std::enable_if<std::is_enum<T>::value, T>::type deserialize(const BasicBitBuffer<ALLOC>& bitBuffer)
220 {
221  BitStreamReader reader(bitBuffer);
222  return zserio::read<T>(reader);
223 }
224 
250 template <typename T, typename ALLOC, typename ...ARGS,
251  typename std::enable_if<!std::is_enum<T>::value && is_allocator<ALLOC>::value, int>::type = 0>
252 vector<uint8_t, ALLOC> serializeToBytes(T& object, const ALLOC& allocator, ARGS&&... arguments)
253 {
254  const BasicBitBuffer<ALLOC> bitBuffer = detail::serialize(object, allocator,
255  std::forward<ARGS>(arguments)...);
256 
257  return bitBuffer.getBytes();
258 }
259 
284 template <typename T, typename ALLOC = typename detail::allocator_chooser<T>::type, typename ...ARGS,
285  typename std::enable_if<!std::is_enum<T>::value &&
286  !is_first_allocator<typename std::decay<ARGS>::type...>::value, int>::type = 0>
287 vector<uint8_t, ALLOC> serializeToBytes(T& object, ARGS&&... arguments)
288 {
289  const BasicBitBuffer<ALLOC> bitBuffer = detail::serialize(object, ALLOC(),
290  std::forward<ARGS>(arguments)...);
291 
292  return bitBuffer.getBytes();
293 }
294 
313 template <typename T, typename ALLOC = std::allocator<uint8_t>,
314  typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
315 vector<uint8_t, ALLOC> serializeToBytes(T enumValue, const ALLOC& allocator = ALLOC())
316 {
317  const BasicBitBuffer<ALLOC> bitBuffer = serialize(enumValue, allocator);
318 
319  return bitBuffer.getBytes();
320 }
321 
345 template <typename T, typename ...ARGS>
346 typename std::enable_if<!std::is_enum<T>::value, T>::type deserializeFromBytes(
347  Span<const uint8_t> buffer, ARGS&&... arguments)
348 {
349  BitStreamReader reader(buffer);
350  return T(reader, std::forward<ARGS>(arguments)...);
351 }
352 
371 template <typename T>
372 typename std::enable_if<std::is_enum<T>::value, T>::type deserializeFromBytes(Span<const uint8_t> buffer)
373 {
374  BitStreamReader reader(buffer);
375  return zserio::read<T>(reader);
376 }
377 
394 template <typename T, typename ...ARGS>
395 void serializeToFile(T& object, const std::string& fileName, ARGS&&... arguments)
396 {
397  const auto bitBuffer = serialize(object, std::forward<ARGS>(arguments)...);
398  writeBufferToFile(bitBuffer, fileName);
399 }
400 
423 template <typename T, typename ...ARGS>
424 T deserializeFromFile(const std::string& fileName, ARGS&&... arguments)
425 {
426  const BitBuffer bitBuffer = readBufferFromFile(fileName);
427  return deserialize<T>(bitBuffer, std::forward<ARGS>(arguments)...);
428 }
429 
430 } // namespace zserio
431 
432 #endif // ZSERIO_SERIALIZE_UTIL_H_INC
const vector< uint8_t, ALLOC > & getBytes() const
Definition: BitBuffer.h:393
BasicBitBuffer< ALLOC > serialize(T &object, const ALLOC &allocator, ARGS &&...arguments)
void writeBufferToFile(const uint8_t *buffer, size_t bitSize, BitsTag, const std::string &fileName)
Definition: FileUtil.cpp:10
BasicBitBuffer< ALLOC > serialize(T enumValue, const ALLOC &allocator=ALLOC())
zserio::string< PropagatingPolymorphicAllocator< char >> string
Definition: String.h:15
void write(BitStreamWriter &out, T value)
void serializeToFile(T &object, const std::string &fileName, ARGS &&...arguments)
std::vector< T, RebindAlloc< ALLOC, T >> vector
Definition: Vector.h:16
vector< uint8_t, ALLOC > serializeToBytes(T &object, const ALLOC &allocator, ARGS &&...arguments)
BitBuffer readBufferFromFile(const std::string &fileName)
Definition: FileUtil.cpp:22
size_t initializeOffsets(size_t bitPosition, T value)
T deserializeFromFile(const std::string &fileName, ARGS &&...arguments)
std::enable_if<!std::is_enum< T >::value, T >::type deserialize(const BasicBitBuffer< ALLOC > &bitBuffer, ARGS &&...arguments)
size_t bitSizeOf(T value)
std::enable_if<!std::is_enum< T >::value, T >::type deserializeFromBytes(Span< const uint8_t > buffer, ARGS &&...arguments)