2#include "serialization.hpp"
3#include <vanetza/common/byte_order.hpp>
4#include <boost/units/cmath.hpp>
12const Lifetime Lifetime::zero()
19 set(Base::Fifty_Milliseconds, 0);
22Lifetime::Lifetime(Base base, BitNumber<uint8_t, 6> multiplier)
24 set(base, multiplier);
27void Lifetime::set(Base base, BitNumber<uint8_t, 6> multiplier)
29 m_lifetime = multiplier.raw() << 2 | (
static_cast<uint8_t
>(base) & base_mask);
32bool Lifetime::operator<(
const Lifetime& other)
const
34 return this->
decode() < other.decode();
37bool Lifetime::operator==(
const Lifetime& other)
const
39 const units::Duration diff = this->
decode() - other.decode();
41 const auto min_value = 0.050 * units::si::seconds;
42 return abs(diff) < min_value;
47 double seconds = duration / boost::units::si::seconds;
48 if (seconds >= 630.0) {
49 set(Base::Hundred_Seconds, std::lround(seconds / 100.0));
50 }
else if (seconds >= 63.0) {
51 set(Base::Ten_Seconds, std::lround(seconds / 10.0));
52 }
else if (seconds >= 3.15) {
53 set(Base::One_Second, std::lround(seconds));
55 set(Base::Fifty_Milliseconds, std::lround(seconds / 0.050));
61 using vanetza::units::si::seconds;
62 Base base =
static_cast<Base
>(m_lifetime & base_mask);
63 const double multiplier = (m_lifetime & multiplier_mask) >> 2;
67 case Base::Fifty_Milliseconds:
68 unit = 0.050 * seconds;
70 case Base::One_Second:
74 case Base::Ten_Seconds:
75 unit = 10.0 * seconds;
77 case Base::Hundred_Seconds:
78 unit = 100.0 * seconds;
81 throw std::runtime_error(
"Decoding of Lifetime::Base failed");
85 return multiplier * unit;
90 serialize(host_cast(lifetime.raw()), ar);
void encode(units::Duration)
units::Duration decode() const