MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
firmware_version.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stddef.h>
4 #include <stdint.h>
5 
6 #include <string>
7 
8 #if __cpp_impl_three_way_comparison
9 #include <compare>
10 #endif // __cpp_impl_three_way_comparison
11 #if __cpp_lib_string_view >= 201606L
12 #include <string_view>
13 #endif // __cpp_lib_string_view >= 201606L
14 
15 namespace mip
16 {
28  {
29  public:
30  FirmwareVersion() = default;
31  FirmwareVersion(uint8_t major, uint8_t minor, uint8_t patch) { fromParts(major, minor, patch); }
32  explicit FirmwareVersion(uint16_t version) : m_version(version) {}
33 
34  FirmwareVersion(const FirmwareVersion&) = default;
35  FirmwareVersion& operator=(const FirmwareVersion&) = default;
36  FirmwareVersion& operator=(uint16_t version) { m_version = version; return *this; }
37 
38  bool isNull() const { return m_version == 0; }
39  bool isValid() const { return !isNull() && major() < 10 && minor() < 10 && patch() < 100; }
40  bool isDevVersion() const { return major() == 0; }
41  bool isReleaseVersion() const { return major() > 0; }
42  bool isSpecialVersion() const { return major() > 1; }
43 
44  uint16_t asU16() const { return m_version; }
45  uint16_t& asU16() { return m_version; }
46 
47  void fromParts(uint8_t major, uint8_t minor, uint8_t patch) { m_version = major * 1000 + minor * 100 + patch; }
48 
49  uint8_t major() const { return uint8_t(m_version / 1000); }
50  uint8_t minor() const { return (m_version / 100) % 10; }
51  uint8_t patch() const { return m_version % 100; }
52 
53  bool operator==(FirmwareVersion other) const { return m_version == other.m_version; }
54  bool operator!=(FirmwareVersion other) const { return !(*this == other); }
55 #ifndef __cpp_impl_three_way_comparison
56  bool operator<=(FirmwareVersion other) const { return !isNull() && major() == other.major() && m_version <= other.m_version; }
57  bool operator>=(FirmwareVersion other) const { return !isNull() && major() == other.major() && m_version >= other.m_version; }
58  bool operator< (FirmwareVersion other) const { return !isNull() && major() == other.major() && m_version < other.m_version; }
59  bool operator> (FirmwareVersion other) const { return !isNull() && major() == other.major() && m_version > other.m_version; }
60 #else // __cpp_impl_three_way_comparison
61  std::partial_ordering operator<=>(FirmwareVersion other) const
62  {
63  if(major() != other.major() || isNull())
64  return std::partial_ordering::unordered;
65  else
66  return std::partial_ordering(m_version <=> other.m_version);
67  }
68 #endif // !__cpp_impl_three_way_comparison
69 
70  void toString(char* buffer, size_t buffer_size) const;
71  bool fromString(const char* str, size_t length=-1);
72 
73  std::string toString() const;
74 #if __cpp_lib_string_view >= 201606L
75  bool fromString(std::string_view str) { return fromString(str.data(), str.size()); }
76 #endif // __cpp_lib_string_view >= 201606L
77 
78  private:
79  uint16_t m_version = 0;
80  };
81 } // namespace mip
mip::FirmwareVersion::isDevVersion
bool isDevVersion() const
Definition: firmware_version.hpp:40
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion()=default
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion(uint16_t version)
Definition: firmware_version.hpp:32
mip::FirmwareVersion
Represents the device firmware version.
Definition: firmware_version.hpp:27
mip::FirmwareVersion::asU16
uint16_t asU16() const
Definition: firmware_version.hpp:44
mip::FirmwareVersion::toString
std::string toString() const
Convert a FirmwareVersion to a string separated by periods.
Definition: firmware_version.cpp:74
mip::FirmwareVersion::operator<=
bool operator<=(FirmwareVersion other) const
Definition: firmware_version.hpp:56
mip::FirmwareVersion::patch
uint8_t patch() const
Definition: firmware_version.hpp:51
mip::FirmwareVersion::fromString
bool fromString(const char *str, size_t length=-1)
Reads a standard-format string (X.Y.ZZ\0 or XYZZ\0).
Definition: firmware_version.cpp:31
mip::FirmwareVersion::operator==
bool operator==(FirmwareVersion other) const
Definition: firmware_version.hpp:53
mip::FirmwareVersion::major
uint8_t major() const
Definition: firmware_version.hpp:49
mip::FirmwareVersion::asU16
uint16_t & asU16()
Definition: firmware_version.hpp:45
mip::FirmwareVersion::fromParts
void fromParts(uint8_t major, uint8_t minor, uint8_t patch)
Definition: firmware_version.hpp:47
mip::FirmwareVersion::operator>=
bool operator>=(FirmwareVersion other) const
Definition: firmware_version.hpp:57
mip::FirmwareVersion::operator!=
bool operator!=(FirmwareVersion other) const
Definition: firmware_version.hpp:54
mip::FirmwareVersion::operator=
FirmwareVersion & operator=(uint16_t version)
Definition: firmware_version.hpp:36
mip::FirmwareVersion::isReleaseVersion
bool isReleaseVersion() const
Definition: firmware_version.hpp:41
mip::FirmwareVersion::operator>
bool operator>(FirmwareVersion other) const
Definition: firmware_version.hpp:59
mip::FirmwareVersion::FirmwareVersion
FirmwareVersion(uint8_t major, uint8_t minor, uint8_t patch)
Definition: firmware_version.hpp:31
mip::FirmwareVersion::isSpecialVersion
bool isSpecialVersion() const
Definition: firmware_version.hpp:42
mip::FirmwareVersion::minor
uint8_t minor() const
Definition: firmware_version.hpp:50
mip::FirmwareVersion::isNull
bool isNull() const
Definition: firmware_version.hpp:38
mip::FirmwareVersion::operator=
FirmwareVersion & operator=(const FirmwareVersion &)=default
mip::FirmwareVersion::isValid
bool isValid() const
Definition: firmware_version.hpp:39
mip::FirmwareVersion::operator<
bool operator<(FirmwareVersion other) const
Definition: firmware_version.hpp:58