Vanetza
 
Loading...
Searching...
No Matches
confident_quantity.hpp
1#ifndef CONFIDENT_QUANTITY_HPP_B2XVJERI
2#define CONFIDENT_QUANTITY_HPP_B2XVJERI
3
4#include <limits>
5
6namespace vanetza
7{
8
9/**
10 * ConfidentQuantity combines a boost::quantity value with a confidence level.
11 *
12 * Usually, a confidence level of 95% is used in ITS specifications.
13 * If no confidence level is explicitly given a worst case value is used, i.e.
14 * the maximum value representable by the underlying type or infinity.
15 */
16template<typename T>
18{
19public:
20 constexpr T worst_confidence() const
21 {
22 using value_type = typename T::value_type;
23 return T::from_value(std::numeric_limits<value_type>::has_infinity ?
24 std::numeric_limits<value_type>::infinity() :
25 std::numeric_limits<value_type>::max());
26 }
27
28 constexpr T default_value() const
29 {
30 using value_type = typename T::value_type;
31 return T::from_value(std::numeric_limits<value_type>::has_quiet_NaN ?
32 std::numeric_limits<value_type>::quiet_NaN() : value_type());
33 }
34
35 constexpr bool is_nan(const T& t) const
36 {
37 return t.value() != t.value();
38 }
39
41 m_value(default_value()), m_confidence(worst_confidence()) {}
42 ConfidentQuantity(const T& value) :
43 m_value(value), m_confidence(worst_confidence()) {}
44 ConfidentQuantity(const T& value, const T& confidence) :
45 m_value(value), m_confidence(!is_nan(confidence) ? confidence : worst_confidence()) {}
46
47 ConfidentQuantity(const ConfidentQuantity&) = default;
48 ConfidentQuantity& operator=(const ConfidentQuantity&) = default;
50 ConfidentQuantity& operator=(ConfidentQuantity&&) = default;
51
52 void assign(const T& value, const T& confidence)
53 {
54 m_value = value;
55 m_confidence = !is_nan(confidence) ? confidence : worst_confidence();
56 }
57
58 const T& value() const
59 {
60 return m_value;
61 }
62
63 const T& confidence() const
64 {
65 return m_confidence;
66 }
67
68private:
69 T m_value;
70 T m_confidence;
71};
72
73
74} // namespace vanetza
75
76#endif /* CONFIDENT_QUANTITY_HPP_B2XVJERI */
77