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