data_version.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <string>
5 
7 
8 namespace point_one {
9 namespace fusion_engine {
10 namespace messages {
11 
12 // Enforce 4-byte alignment and packing of all data structures and values.
13 // Floating point values are aligned on platforms that require it. This is done
14 // with a combination of setting struct attributes, and manual alignment
15 // within the definitions. See the "Message Packing" section of the README.
16 #pragma pack(push, 1)
17 
18 /**
19  * @brief A struct representing the version of a data object.
20  *
21  * The version is considered invalid if @ref major_version is 0xFF and @ref minor_version is
22  * 0xFFFF.
23  */
25  // The reserved bytes must be 0xFF for backward compatibility.
26  uint8_t reserved = 0xFF;
27  uint8_t major_version = 0xFF;
28  uint16_t minor_version = 0xFFFF;
29 
30  constexpr DataVersion() = default;
31  constexpr DataVersion(uint8_t major, uint16_t minor)
32  : major_version{major}, minor_version{minor} {}
33 
34  /**
35  * @brief Returns whether the stored version is valid.
36  *
37  * @return `true` if the version is valid, `false` otherwise.
38  */
39  bool IsValid() const {
40  return major_version != 0xFF || minor_version != 0xFFFF;
41  }
42 };
43 
44 #pragma pack(pop)
45 
47 
48 inline constexpr bool operator==(const DataVersion& a, const DataVersion& b) {
49  return a.major_version == b.major_version &&
51 }
52 
53 inline constexpr bool operator!=(const DataVersion& a, const DataVersion& b) {
54  return !(a == b);
55 }
56 
57 inline constexpr bool operator<(const DataVersion& a, const DataVersion& b) {
58  return a.major_version < b.major_version ||
59  (a.major_version == b.major_version &&
61 }
62 
63 inline constexpr bool operator>(const DataVersion& a, const DataVersion& b) {
64  return b < a;
65 }
66 
67 inline constexpr bool operator<=(const DataVersion& a, const DataVersion& b) {
68  return !(a > b);
69 }
70 
71 inline constexpr bool operator>=(const DataVersion& a, const DataVersion& b) {
72  return !(a < b);
73 }
74 
75 /**
76  * @brief Helper class for printing out X.Y form of @ref DataVersion.
77  *
78  * ```cpp
79  * DataVersion ver{3, 2};
80  * std::cout << "Ver: " << ver;
81  * // Ver: 3.2
82  * ```
83  */
84 p1_ostream& operator<<(p1_ostream& stream, const DataVersion& ver);
85 
86 std::string ToString(const DataVersion& ver);
87 
88 DataVersion FromString(const char* str);
89 
90 inline DataVersion FromString(std::string str) {
91  return FromString(str.c_str());
92 }
93 
94 } // namespace messages
95 } // namespace fusion_engine
96 } // namespace point_one
constexpr bool operator==(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:48
Library portability helper definitions.
std::string ToString(const DataVersion &ver)
Definition: data_version.cc:17
#define P1_ALIGNAS(N)
Definition: portability.h:57
uint16_t minor_version
Definition: data_version.h:28
constexpr bool operator>(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:63
GNSS signal and frequency type definitions.
Definition: logging.h:38
uint8_t major_version
Definition: data_version.h:27
constexpr DataVersion()=default
constexpr bool operator>=(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:71
bool IsValid() const
Returns whether the stored version is valid.
Definition: data_version.h:39
DataVersion FromString(const char *str)
Definition: data_version.cc:26
constexpr bool operator!=(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:53
std::ostream p1_ostream
Definition: portability.h:75
p1_ostream & operator<<(p1_ostream &stream, ConfigType type)
ConfigType stream operator.
constexpr bool operator<(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:57
constexpr DataVersion(uint8_t major, uint16_t minor)
Definition: data_version.h:31
constexpr bool operator<=(const DataVersion &a, const DataVersion &b)
Definition: data_version.h:67
A struct representing the version of a data object.
Definition: data_version.h:24
constexpr DataVersion INVALID_DATA_VERSION
Definition: data_version.h:46