Debian Codegraph @

src/mpt/base/semantic_version.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_SEMANTIC_VERSION_HPP
4 #define MPT_BASE_SEMANTIC_VERSION_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/base/version.hpp"
11
12
13
14 #include <tuple>
15
16
17
18 namespace mpt {
19 inline namespace MPT_INLINE_NS {
20
21
22 struct semantic_version {
23 unsigned long long major = 0;
24 unsigned long long minor = 0;
25 unsigned long long patch = 0;
26 constexpr std::tuple<unsigned long long, unsigned long long, unsigned long long> as_tuple() const noexcept {
27 return std::make_tuple(major, minor, patch);
28 }
29 };
30
31 constexpr bool operator==(const semantic_version a, const semantic_version b) noexcept {
32 return a.as_tuple() == b.as_tuple();
33 }
34 constexpr bool operator!=(const semantic_version a, const semantic_version b) noexcept {
35 return a.as_tuple() != b.as_tuple();
36 }
37 constexpr bool operator<(const semantic_version a, const semantic_version b) noexcept {
38 return a.as_tuple() < b.as_tuple();
39 }
40 constexpr bool operator>(const semantic_version a, const semantic_version b) noexcept {
41 return a.as_tuple() > b.as_tuple();
42 }
43 constexpr bool operator<=(const semantic_version a, const semantic_version b) noexcept {
44 return a.as_tuple() <= b.as_tuple();
45 }
46 constexpr bool operator>=(const semantic_version a, const semantic_version b) noexcept {
47 return a.as_tuple() >= b.as_tuple();
48 }
49
50 struct version_info {
51 semantic_version semver{};
52 unsigned long long build = 0;
53 constexpr std::tuple<std::tuple<unsigned long long, unsigned long long, unsigned long long>, unsigned long long> as_tuple() const noexcept {
54 return std::make_tuple(semver.as_tuple(), build);
55 }
56 template <typename Tostream>
57 friend Tostream & operator<<(Tostream & os, const version_info vi) {
58 if (vi.build > 0) {
59 os << vi.semver.major << "." << vi.semver.minor << "." << vi.semver.patch << "+build." << vi.build;
60 } else {
61 os << vi.semver.major << "." << vi.semver.minor << "." << vi.semver.patch;
62 }
63 return os;
64 }
65 };
66
67 constexpr bool operator==(const version_info a, const version_info b) noexcept {
68 return a.as_tuple() == b.as_tuple();
69 }
70 constexpr bool operator!=(const version_info a, const version_info b) noexcept {
71 return a.as_tuple() != b.as_tuple();
72 }
73 constexpr bool operator<(const version_info a, const version_info b) noexcept {
74 return a.as_tuple() < b.as_tuple();
75 }
76 constexpr bool operator>(const version_info a, const version_info b) noexcept {
77 return a.as_tuple() > b.as_tuple();
78 }
79 constexpr bool operator<=(const version_info a, const version_info b) noexcept {
80 return a.as_tuple() <= b.as_tuple();
81 }
82 constexpr bool operator>=(const version_info a, const version_info b) noexcept {
83 return a.as_tuple() >= b.as_tuple();
84 }
85
86 constexpr inline version_info Version = {
87 {MPT_VERSION_MAJOR, MPT_VERSION_MINOR, MPT_VERSION_PATCH},
88 MPT_VERSION_BUILD
89 };
90
91
92 } // namespace MPT_INLINE_NS
93 } // namespace mpt
94
95
96
97 #endif // MPT_BASE_SEMANTIC_VERSION_HPP