Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
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"
19#include "zserio/DataView.h"
20#include "zserio/FileUtil.h"
21#include "zserio/Traits.h"
22#include "zserio/View.h"
23
24namespace zserio
25{
26
52template <typename T, typename ALLOC, typename... ARGS, std::enable_if_t<is_allocator_v<ALLOC>, int> = 0>
54{
55 const View<T> view(data, std::forward<ARGS>(arguments)...);
56
57 return serialize(view, allocator);
58}
59
86template <typename T, typename ALLOC, typename... ARGS, std::enable_if_t<is_allocator_v<ALLOC>, int> = 0>
88{
89 const View<T> view(data, std::forward<ARGS>(arguments)...);
90
92}
93
117template <typename T, typename... ARGS,
118 typename std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>,
119 int> = 0>
121{
122 return serialize(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
123}
124
150template <typename T, typename... ARGS,
151 typename std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>,
152 int> = 0>
154{
155 return serializeToBytes(data, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
156}
157
180template <typename T, typename ALLOC, typename... ARGS>
183{
185
186 return detail::read(reader, data, std::forward<ARGS>(arguments)...);
187}
188
209template <typename T, typename ALLOC, typename... ARGS>
211{
212 return deserialize<T>(buffer, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
213}
214
237template <typename T, typename... ARGS>
240{
242
243 return detail::read(reader, data, std::forward<ARGS>(arguments)...);
244}
245
266template <typename T, typename... ARGS>
268{
269 return deserializeFromBytes(buffer, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
270}
271
297template <typename T, typename ALLOC>
299{
300 detail::validate(view, "");
301 const BitSize bitSize = detail::initializeOffsets(view, 0);
303 BitStreamWriter writer(buffer);
304 detail::write(writer, view);
305
306 return buffer;
307}
308
334template <typename T, typename ALLOC>
336{
337 detail::validate(view, "");
338 const BitSize bitSize = detail::initializeOffsets(view, 0);
339 Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
340 BitStreamWriter writer(buffer);
341 detail::write(writer, view);
342
343 return buffer;
344}
345
369template <typename T>
371{
372 return serialize(view, typename T::allocator_type());
373}
374
396template <typename T>
398{
399 return serializeToBytes(view, typename T::allocator_type());
400}
401
426template <typename T, typename ALLOC>
428{
429 // there is no need to set offsets or call validation here, DataView is already consistent
430 size_t bitSize = detail::bitSizeOf(dataView, 0);
432 BitStreamWriter writer(buffer);
433
434 detail::write(writer, dataView);
435 return buffer;
436}
437
462template <typename T, typename ALLOC>
464{
465 // there is no need to set offsets or call validation here, DataView is already consistent
466 size_t bitSize = detail::bitSizeOf(dataView, 0);
467 Vector<uint8_t, ALLOC> buffer((bitSize + 7) / 8, allocator);
468 BitStreamWriter writer(buffer);
469
470 detail::write(writer, dataView);
471 return buffer;
472}
473
496template <typename T>
498{
499 return serialize(dataView, typename T::allocator_type());
500}
501
523template <typename T>
528
552template <typename T, typename ALLOC, typename... ARGS>
554 const typename T::allocator_type& allocator, ARGS&&... arguments)
555{
556 BitStreamReader reader(buffer, maxArrayPreallocation);
557 T data{allocator};
558
559 return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
560}
561
583template <typename T, typename ALLOC, typename... ARGS>
585 const BasicBitBuffer<ALLOC>& buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
586{
587 return deserialize<T>(buffer, ArrayPreallocation(), allocator, std::forward<ARGS>(arguments)...);
588}
589
612template <typename T, typename... ARGS>
614 const typename T::allocator_type& allocator, ARGS&&... arguments)
615{
616 BitStreamReader reader(buffer, maxArrayPreallocation);
617 T data{allocator};
618
619 return DataView<T>(reader, std::move(data), std::forward<ARGS>(arguments)...);
620}
621
643template <typename T, typename... ARGS>
645 Span<const uint8_t> buffer, const typename T::allocator_type& allocator, ARGS&&... arguments)
646{
647 return deserializeFromBytes<T>(buffer, ArrayPreallocation(), allocator, std::forward<ARGS>(arguments)...);
648}
649
671template <typename T, typename ALLOC, typename... ARGS,
672 std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
675{
676 return deserialize<T>(
677 buffer, maxArrayAlloc, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
678}
679
700template <typename T, typename ALLOC, typename... ARGS,
701 std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>, int> = 0>
703{
704 return deserialize<T>(buffer, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
705}
706
726template <typename T, typename... ARGS, std::enable_if_t<!is_first_allocator_v<ARGS...>, int> = 0>
729{
731 buffer, maxArrayPrealloc, typename T::allocator_type(), std::forward<ARGS>(arguments)...);
732}
733
752template <typename T, typename... ARGS,
753 std::enable_if_t<!is_first_allocator_v<ARGS...> && !is_first_array_preallocation_v<ARGS...>, int> = 0>
755{
756 return deserializeFromBytes<T>(buffer, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
757}
758
776template <typename T>
777void serializeToFile(const View<T>& view, std::string_view fileName)
778{
779 const auto bitBuffer = serialize(view);
781}
782
800template <typename T>
801void serializeToFile(const DataView<T>& dataView, std::string_view fileName)
802{
803 const auto bitBuffer = serialize(dataView);
805}
806
824template <typename T, typename... ARGS>
825void serializeToFile(const T& data, std::string_view fileName, ARGS&&... arguments)
826{
827 const View<T> view(data, std::forward<ARGS>(arguments)...);
829}
830
855template <typename T, typename... ARGS>
857 std::string_view fileName, ArrayPreallocation maxArrayPrealloc, T& data, ARGS&&... arguments)
858{
860 return deserialize(bitBuffer, maxArrayPrealloc, data, std::forward<ARGS>(arguments)...);
861}
862
885template <typename T, typename... ARGS>
886View<T> deserializeFromFile(std::string_view fileName, T& data, ARGS&&... arguments)
887{
888 return deserializeFromFile(fileName, ArrayPreallocation(), data, std::forward<ARGS>(arguments)...);
889}
890
915template <typename T, typename... ARGS>
922
945template <typename T, typename... ARGS>
947{
948 return deserializeFromFile<T>(fileName, ArrayPreallocation(), std::forward<ARGS>(arguments)...);
949}
950
951} // namespace zserio
952
953#endif // ZSERIO_SERIALIZE_UTIL_H_INC
Vector< uint8_t, ALLOC > serializeToBytes(const T &data, const ALLOC &allocator, ARGS &&... arguments)
View< T > deserializeFromFile(std::string_view fileName, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
void serializeToFile(const View< T > &view, std::string_view fileName)
BasicBitBuffer< ALLOC > serialize(const T &data, const ALLOC &allocator, ARGS &&... arguments)
void writeBufferToFile(const uint8_t *buffer, size_t bitSize, BitsTag, std::string_view fileName)
Definition FileUtil.cpp:10
View< T > deserialize(const BasicBitBuffer< ALLOC > &buffer, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
View< T > deserializeFromBytes(Span< const uint8_t > buffer, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)
BitBuffer readBufferFromFile(std::string_view fileName)
Definition FileUtil.cpp:25
constexpr bool is_first_allocator_v
Definition Traits.h:122
constexpr bool is_first_array_preallocation_v
Definition Traits.h:154