ifm3d
semver.h
1 // -*- c++ -*-
2 /*
3  * Copyright 2022 ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_DEVICE_SEMVER_H
8 #define IFM3D_DEVICE_SEMVER_H
9 
10 #include <iostream>
11 #include <optional>
12 #include <string>
13 #include <ifm3d/device/module_device.h>
14 
15 namespace ifm3d
16 {
18  struct IFM3D_EXPORT SemVer
19  {
20  SemVer(size_t major,
21  size_t minor,
22  size_t patch,
23  const std::optional<std::string> prerelease = std::nullopt,
24  const std::optional<std::string> build_meta = std::nullopt)
25  : major_num(major),
26  minor_num(minor),
27  patch_num(patch),
28  prerelease(prerelease),
29  build_meta(build_meta)
30 
31  {}
32 
33  const size_t major_num;
34  const size_t minor_num;
35  const size_t patch_num;
36  const std::optional<std::string> prerelease;
37  const std::optional<std::string> build_meta;
38 
39  constexpr bool
40  operator<(const SemVer& rhs) const
41  {
42  // Note: sorting by prerelease is not implemented as it's not needed for
43  // our usecase
44  return major_num < rhs.major_num ||
45  (major_num == rhs.major_num &&
46  (minor_num < rhs.minor_num ||
47  (minor_num == rhs.minor_num && (patch_num < rhs.patch_num))));
48  }
49 
50  constexpr bool
51  operator==(const SemVer& rhs) const
52  {
53  return ((major_num == rhs.major_num) && (minor_num == rhs.minor_num) &&
54  (patch_num == rhs.patch_num) && (prerelease == rhs.prerelease) &&
55  (build_meta == rhs.build_meta));
56  }
57 
58  constexpr bool
59  operator!=(const SemVer& rhs) const
60  {
61  return !(*this == rhs);
62  }
63 
64  constexpr bool
65  operator>=(const SemVer& rhs) const
66  {
67  return !(*this < rhs);
68  }
69 
70  constexpr bool
71  operator>(const SemVer& rhs) const
72  {
73  return rhs < *this;
74  }
75 
76  constexpr bool
77  operator<=(const SemVer& rhs) const
78  {
79  return !(rhs < *this);
80  }
81 
82  /* To support fmt ostream */
83  friend std::ostream&
84  operator<<(std::ostream& os, const SemVer& version)
85  {
86  os << version.major_num << '.' << version.minor_num << '.'
87  << version.patch_num;
88 
89  if (version.prerelease.has_value())
90  {
91  os << '-' << version.prerelease.value();
92  }
93 
94  if (version.build_meta.has_value())
95  {
96  os << '+' << version.build_meta.value();
97  }
98 
99  return os;
100  }
101 
102  static std::optional<SemVer> Parse(const std::string& version_string);
103  };
104 } // end: namespace ifm3d
105 
106 #endif // IFM3D_DEVICE_SEMVER_H
ifm3d::SemVer
Definition: semver.h:18