Vanetza
 
Loading...
Searching...
No Matches
serialization.hpp
1#ifndef SERIALIZATION_HPP_IENSIAL4
2#define SERIALIZATION_HPP_IENSIAL4
3
4#include <vanetza/common/serialization.hpp>
5#include <vanetza/security/v2/length_coding.hpp>
6#include <cassert>
7#include <list>
8
9namespace vanetza
10{
11namespace security
12{
13namespace v2
14{
15
16using vanetza::serialize;
17using vanetza::deserialize;
18
19/**
20 * \brief Serialize given length
21 * \param ar to serialize in
22 * \param size to encode
23 */
24void serialize_length(OutputArchive&, std::uintmax_t);
25
26/**
27 * \brief Deserialize length from a given archive
28 * \param ar shall start with encoded length
29 * \return length deserialized from archive
30 */
31std::uintmax_t deserialize_length(InputArchive&);
32
33/**
34 * \brief Calculate size of a list
35 *
36 * Sums up sizes of all list elements only, length itself is not included.
37 * Therefore, the returned length is suitable as argument for serialize_length.
38 *
39 * \tparam T list element type
40 * \param list
41 * \return accumulated elements' size
42 */
43template<class T>
44size_t get_size(const std::list<T>& list)
45{
46 using vanetza::security::v2::get_size;
47 size_t size = 0;
48 for (auto& elem : list) {
49 size += get_size(elem);
50 }
51 return size;
52}
53
54/**
55 * \brief Trim (possibly) wider size type safely
56 *
57 * This function throws an exception if size would be truncated.
58 *
59 * \param in wide size type
60 * \return same size using narrow type
61 */
62std::size_t trim_size(std::uintmax_t in);
63
64/** \brief Serialize from any given list into given binary archive
65 * \tparam T the type of the list
66 * \tparam ARGS all additional arguments for the underlying functions
67 * \param ar to serialize in
68 * \param list
69 * \param args the additional arguments
70 */
71template<class T, typename... ARGS>
72void serialize(OutputArchive& ar, const std::list<T>& list, ARGS&&... args)
73{
74 using vanetza::security::v2::get_size;
75 using vanetza::security::v2::serialize;
76 size_t size = get_size(list);
77 serialize_length(ar, size);
78 for (auto& elem : list) {
79 serialize(ar, elem, std::forward<ARGS>(args)...);
80 }
81}
82
83/** \brief Deserialize a list from given archive
84 * \tparam T the type of the list
85 * \tparam ARGS all additional arguments for the underlying functions
86 * \param ar, shall start with the list
87 * \param args the additional arguments
88 * \return size of the deserialized list in bytes
89 */
90template<class T, typename... ARGS>
91std::size_t deserialize(InputArchive& ar, std::list<T>& list, ARGS&&... args)
92{
93 using vanetza::security::v2::deserialize;
94 const auto length = trim_size(deserialize_length(ar));
95 std::intmax_t remainder = length;
96 while (remainder > 0) {
97 T t;
98 remainder -= deserialize(ar, t, std::forward<ARGS>(args)...);
99 list.push_back(std::move(t));
100 }
101 assert(remainder == 0);
102 return length;
103}
104
105} // namespace v2
106} // namespace security
107} // namespace vanetza
108
109#endif /* SERIALIZATION_HPP_IENSIAL4 */