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