SOLA
Loading...
Searching...
No Matches
serializer.h
1// Copyright The SOLA Contributors
2//
3// Licensed under the MIT License.
4// For details on the licensing terms, see the LICENSE file.
5// SPDX-License-Identifier: MIT
6
7#ifndef SOLANET_SERIALIZER_SERIALIZER_H_
8#define SOLANET_SERIALIZER_SERIALIZER_H_
9
10#ifndef CPPCHECK_IGNORE
11#include <cereal/archives/binary.hpp>
12#include <cereal/cereal.hpp>
13#include <cereal/types/array.hpp>
14#include <cereal/types/optional.hpp>
15#include <cereal/types/set.hpp>
16#include <cereal/types/string.hpp>
17#include <cereal/types/tuple.hpp>
18#include <cereal/types/unordered_map.hpp>
19#include <cereal/types/variant.hpp>
20#include <cereal/types/vector.hpp>
21#endif
22
23#include <sstream>
24
25namespace solanet::serializer {
26
28
29template <typename T, typename SerializerType = BinarySerializer>
30std::string serialize(const T &msg) {
31 static_assert(std::is_class_v<SerializerType>);
32
33 std::stringstream ss;
34
35 if constexpr (std::is_same_v<SerializerType, BinarySerializer>) {
36 cereal::BinaryOutputArchive archive(ss);
37 archive(msg);
38 } else {
39 static_assert(std::is_void_v<SerializerType>); // No valid serializer type selected
40 }
41
42 return ss.str();
43}
44
45template <typename T, typename SerializerType = BinarySerializer>
46T deserialize(const std::string &msg) {
47 static_assert(std::is_class_v<SerializerType>);
48
49 T m;
50 std::istringstream iss(msg);
51
52 if constexpr (std::is_same_v<SerializerType, BinarySerializer>) {
53 cereal::BinaryInputArchive a(iss);
54 a(m);
55 } else {
56 static_assert(std::is_void_v<SerializerType>); // No valid serializer type selected
57 }
58
59 return m;
60}
61
62} // namespace solanet::serializer
63
64#endif // SOLANET_SERIALIZER_SERIALIZER_H_
Definition serializer.h:27