Fcitx
semver.h
1 /*
2  * SPDX-FileCopyrightText: 2021~2021 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_SEMVER_H_
8 #define _FCITX_UTILS_SEMVER_H_
9 
10 #include <cstdint>
11 #include <optional>
12 #include <string>
13 #include <string_view>
14 #include <variant>
15 #include <vector>
16 #include <fcitx-utils/fcitxutils_export.h>
17 #include <fcitx-utils/log.h>
18 #include <fcitx-utils/macros.h>
19 
20 namespace fcitx {
21 
22 class FCITXUTILS_EXPORT PreReleaseId {
23 public:
24  explicit PreReleaseId(uint32_t id);
25  explicit PreReleaseId(std::string id);
26 
27  FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(PreReleaseId);
28 
29  std::string toString() const;
30 
31  bool isNumeric() const noexcept;
32  uint32_t numericId() const noexcept;
33  const std::string &id() const noexcept;
34 
35  int compare(const PreReleaseId &other) const noexcept;
36 
37 private:
38  std::variant<std::string, uint32_t> value_;
39 };
40 
41 /**
42  * @brief Provide a Semantic version 2.0 implementation
43  *
44  * @since 5.0.6
45  */
46 class FCITXUTILS_EXPORT SemanticVersion {
47 public:
48  SemanticVersion() = default;
49  FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(SemanticVersion);
50 
51  static std::optional<SemanticVersion> parse(std::string_view data);
52 
53  std::string toString() const;
54 
55  FCITX_DECLARE_PROPERTY(uint32_t, (major), setMajor);
56  FCITX_DECLARE_PROPERTY(uint32_t, (minor), setMinor);
57  FCITX_DECLARE_PROPERTY(uint32_t, patch, setPatch);
58  FCITX_DECLARE_PROPERTY(std::vector<PreReleaseId>, preReleaseIds,
59  setPreReleaseIds);
60  FCITX_DECLARE_PROPERTY(std::vector<std::string>, buildIds, setBuildIds);
61  bool isPreRelease() const;
62 
63  int compare(const SemanticVersion &other) const noexcept;
64 
65 private:
66  uint32_t major_ = 0;
67  uint32_t minor_ = 1;
68  uint32_t patch_ = 0;
69  std::vector<PreReleaseId> preReleaseIds_;
70  std::vector<std::string> buildIds_;
71 };
72 
73 inline bool operator<(const PreReleaseId &lhs,
74  const PreReleaseId &rhs) noexcept {
75  return lhs.compare(rhs) < 0;
76 }
77 
78 inline bool operator>(const PreReleaseId &lhs,
79  const PreReleaseId &rhs) noexcept {
80  return rhs < lhs;
81 }
82 
83 inline bool operator==(const PreReleaseId &lhs,
84  const PreReleaseId &rhs) noexcept {
85  return lhs.compare(rhs) == 0;
86 }
87 
88 inline bool operator!=(const PreReleaseId &lhs,
89  const PreReleaseId &rhs) noexcept {
90  return !(lhs == rhs);
91 }
92 
93 inline bool operator<=(const PreReleaseId &lhs,
94  const PreReleaseId &rhs) noexcept {
95  return !(lhs > rhs);
96 }
97 
98 inline bool operator>=(const PreReleaseId &lhs,
99  const PreReleaseId &rhs) noexcept {
100  return !(lhs < rhs);
101 }
102 
103 inline bool operator<(const SemanticVersion &lhs,
104  const SemanticVersion &rhs) noexcept {
105  return lhs.compare(rhs) < 0;
106 }
107 
108 inline bool operator>(const SemanticVersion &lhs,
109  const SemanticVersion &rhs) noexcept {
110  return rhs < lhs;
111 }
112 
113 inline bool operator==(const SemanticVersion &lhs,
114  const SemanticVersion &rhs) noexcept {
115  return lhs.compare(rhs) == 0;
116 }
117 
118 inline bool operator!=(const SemanticVersion &lhs,
119  const SemanticVersion &rhs) noexcept {
120  return !(lhs == rhs);
121 }
122 
123 inline bool operator<=(const SemanticVersion &lhs,
124  const SemanticVersion &rhs) noexcept {
125  return !(lhs > rhs);
126 }
127 
128 inline bool operator>=(const SemanticVersion &lhs,
129  const SemanticVersion &rhs) noexcept {
130  return !(lhs < rhs);
131 }
132 
133 inline LogMessageBuilder &operator<<(LogMessageBuilder &builder,
134  const SemanticVersion &version) {
135  builder << "SemanticVersion(" << version.toString() << ")";
136  return builder;
137 }
138 
139 } // namespace fcitx
140 
141 #endif // _FCITX_UTILS_SEMVER_H_
Definition: action.cpp:17
Provide a Semantic version 2.0 implementation.
Definition: semver.h:46
Log utilities.