Vanetza
Loading...
Searching...
No Matches
serialization.hpp
1#ifndef SERIALIZATION_HPP_U2YGHSPB
2#define SERIALIZATION_HPP_U2YGHSPB
3
4#include <vanetza/common/archives.hpp>
5#include <vanetza/common/byte_order.hpp>
6#include <boost/units/quantity.hpp>
7#include <type_traits>
8
9namespace vanetza
10{
11
12template<typename T, ByteOrder ORDER>
13void serialize(OutputArchive& ar, EndianType<T, ORDER> value)
14{
15 typedef typename decltype(value)::network_type network_type;
16 T tmp = static_cast<network_type>(value).get();
17 ar << tmp;
18}
19
20template<typename T, ByteOrder ORDER>
21void deserialize(InputArchive& ar, EndianType<T, ORDER>& value)
22{
23 T tmp;
24 ar >> tmp;
25 value = network_cast<T>(tmp);
26}
27
28template<typename T>
29typename std::enable_if<std::is_integral<T>::value>::type
30serialize(OutputArchive& ar, T value)
31{
32 auto tmp = hton(value);
33 ar << tmp;
34}
35
36template<typename T>
37typename std::enable_if<std::is_integral<T>::value>::type
38deserialize(InputArchive& ar, T& value)
39{
40 T tmp;
41 ar >> tmp;
42 value = ntoh(tmp);
43}
44
45template<typename U, typename T>
46void serialize(OutputArchive& ar, boost::units::quantity<U, T> q)
47{
48 static_assert(std::is_integral<T>::value,
49 "Only integral based quantities are supported");
50 auto tmp = hton(q.value());
51 ar << tmp;
52}
53
54template<typename U, typename T>
55void deserialize(InputArchive& ar, boost::units::quantity<U, T>& q)
56{
57 static_assert(std::is_integral<T>::value,
58 "Only integral based quantities are supported");
59 T tmp;
60 ar >> tmp;
61 q = boost::units::quantity<U, T>::from_value(ntoh(tmp));
62}
63
64template<class T>
65typename std::enable_if<std::is_enum<T>::value>::type
66serialize(OutputArchive& ar, const T& t)
67{
68 serialize(ar, host_cast(static_cast<typename std::underlying_type<T>::type const>(t)));
69}
70
71template<class T>
72typename std::enable_if<std::is_enum<T>::value>::type
73deserialize(InputArchive& ar, T& t)
74{
75 typename std::underlying_type<T>::type tmp;
76 deserialize(ar, tmp);
77 t = static_cast<T>(tmp);
78}
79
80} // namespace vanetza
81
82#endif /* SERIALIZATION_HPP_U2YGHSPB */
static constexpr T mask
static constexpr std::size_t bits
Link layer header used by ITS-G5 stations.
Logical Link Control header with SNAP extension.
Frame Control field in IEEE 802.11 MAC header.
QoS Control field in IEEE 802.11 MAC header.
Sequence Control field in IEEE 802.11 MAC header.