Utilities
Utilities header only library
Version.h
1 #ifndef UTILS_VERSION_H
2 #define UTILS_VERSION_H
3 #include <Utils/ExtSTD.h>
4 
5 //major and minor are macros in glibc <sys/sysmacros.h>
6 //The dependency on <sys/sysmacros.h> should be removed in future releases of glibc.
7 #undef major
8 #undef minor
9 
10 namespace utils
11 {
12 
13 
15  struct Version
16  {
17  Version() = default;
18 
19  Version(uint32_t maj, uint32_t min = 0, uint32_t pat = 0) : major(maj), minor(min), patch(pat) {};
20 
21  Version(const std::string &version_str)
22  {
23  const auto version_nums = std_ext::Parse(version_str, ".");
24  major = version_nums.empty() ? 0 : std::stoul(version_nums[0]);
25  minor = version_nums.size() <= 1 ? 0 : std::stoul(version_nums[1]);
26  patch = version_nums.size() <= 2 ? 0 : std::stoul(version_nums[2]);
27  };
28 
30  std::string ToString() const
31  {
32  return std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
33  };
34 
35  uint32_t major{ 0u };
36  uint32_t minor{ 0u };
37  uint32_t patch{ 0u };
38  };
39 
40  inline bool operator<(const Version &a, const Version &b)
41  {
42  if (a.major == b.major)
43  {
44  if (a.minor == b.minor)
45  {
46  return a.patch < b.patch;
47  }
48  return a.minor < b.minor;
49  }
50  return a.major < b.major;
51  }
52 
53  inline bool operator>(const Version &a, const Version &b)
54  {
55  if (a.major == b.major)
56  {
57  if (a.minor == b.minor)
58  {
59  return a.patch > b.patch;
60  }
61  return a.minor > b.minor;
62  }
63  return a.major > b.major;
64  }
65 
66  inline bool operator<=(const Version &a, const Version &b)
67  {
68  return !(a > b);
69  }
70 
71  inline bool operator>=(const Version &a, const Version &b)
72  {
73  return !(a < b);
74  }
75 
76  inline bool operator==(const Version &a, const Version &b)
77  {
78  return (a.major == b.major) && (a.minor == b.minor) && (a.patch == b.patch);
79  }
80 
81  inline bool operator!=(const Version &a, const Version &b)
82  {
83  return !(a == b);
84  }
85 
86 }
87 #endif /* !UTILS_VERSION_H */
UTILS_API bool operator==(const IntrinsicParams &p1, const IntrinsicParams &p2)
Compare equality of two parameter structures.
std::vector< std::string > Parse(const std::string &text, const std::string &separators=WHITESPACES)
Parses the input text to sub strings by a given list of separators.
Definition: ExtSTD.h:735
Holder for project version.
Definition: Version.h:15
std::string ToString() const
Returns version in string form. Major, minor and patch numbers separated by dots. ...
Definition: Version.h:30
UTILS_API bool operator!=(const IntrinsicParams &p1, const IntrinsicParams &p2)
Compare unequality of two parameter structures.
General namespace for utilities.
Definition: BulletConversions.h:9