WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
VersionTuple.h
Go to the documentation of this file.
1//===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines the llvm::VersionTuple class, which represents a version in
11/// the form major[.minor[.subminor]].
12///
13//===----------------------------------------------------------------------===//
14#ifndef WPIUTIL_WPI_VERSIONTUPLE_H
15#define WPIUTIL_WPI_VERSIONTUPLE_H
16
17#include "wpi/DenseMapInfo.h"
18#include <optional>
19#include <string>
20#include <tuple>
21
22namespace wpi {
23class raw_ostream;
24
25/// Represents a version number in the form major[.minor[.subminor[.build]]].
27 unsigned Major : 32;
28
29 unsigned Minor : 31;
30 unsigned HasMinor : 1;
31
32 unsigned Subminor : 31;
33 unsigned HasSubminor : 1;
34
35 unsigned Build : 31;
36 unsigned HasBuild : 1;
37
38public:
39 constexpr VersionTuple()
40 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
41 Build(0), HasBuild(false) {}
42
43 explicit constexpr VersionTuple(unsigned Major)
44 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
45 HasSubminor(false), Build(0), HasBuild(false) {}
46
47 explicit constexpr VersionTuple(unsigned Major, unsigned Minor)
48 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
49 HasSubminor(false), Build(0), HasBuild(false) {}
50
51 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
52 unsigned Subminor)
53 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
54 HasSubminor(true), Build(0), HasBuild(false) {}
55
56 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
57 unsigned Subminor, unsigned Build)
58 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
59 HasSubminor(true), Build(Build), HasBuild(true) {}
60
61 /// Determine whether this version information is empty
62 /// (e.g., all version components are zero).
63 bool empty() const {
64 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
65 }
66
67 /// Retrieve the major version number.
68 unsigned getMajor() const { return Major; }
69
70 /// Retrieve the minor version number, if provided.
71 std::optional<unsigned> getMinor() const {
72 if (!HasMinor)
73 return std::nullopt;
74 return Minor;
75 }
76
77 /// Retrieve the subminor version number, if provided.
78 std::optional<unsigned> getSubminor() const {
79 if (!HasSubminor)
80 return std::nullopt;
81 return Subminor;
82 }
83
84 /// Retrieve the build version number, if provided.
85 std::optional<unsigned> getBuild() const {
86 if (!HasBuild)
87 return std::nullopt;
88 return Build;
89 }
90
91 /// Return a version tuple that contains only the first 3 version components.
93 if (HasBuild)
94 return VersionTuple(Major, Minor, Subminor);
95 return *this;
96 }
97
98 /// Return a version tuple that contains a different major version but
99 /// everything else is the same.
100 VersionTuple withMajorReplaced(unsigned NewMajor) const {
101 return VersionTuple(NewMajor, Minor, Subminor, Build);
102 }
103
104 /// Return a version tuple that contains only components that are non-zero.
106 VersionTuple Result = *this;
107 if (Result.Build == 0) {
108 Result.HasBuild = false;
109 if (Result.Subminor == 0) {
110 Result.HasSubminor = false;
111 if (Result.Minor == 0)
112 Result.HasMinor = false;
113 }
114 }
115 return Result;
116 }
117
118 /// Determine if two version numbers are equivalent. If not
119 /// provided, minor and subminor version numbers are considered to be zero.
120 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
121 return X.Major == Y.Major && X.Minor == Y.Minor &&
122 X.Subminor == Y.Subminor && X.Build == Y.Build;
123 }
124
125 /// Determine if two version numbers are not equivalent.
126 ///
127 /// If not provided, minor and subminor version numbers are considered to be
128 /// zero.
129 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
130 return !(X == Y);
131 }
132
133 /// Determine whether one version number precedes another.
134 ///
135 /// If not provided, minor and subminor version numbers are considered to be
136 /// zero.
137 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
138 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
139 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
140 }
141
142 /// Determine whether one version number follows another.
143 ///
144 /// If not provided, minor and subminor version numbers are considered to be
145 /// zero.
146 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
147 return Y < X;
148 }
149
150 /// Determine whether one version number precedes or is
151 /// equivalent to another.
152 ///
153 /// If not provided, minor and subminor version numbers are considered to be
154 /// zero.
155 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
156 return !(Y < X);
157 }
158
159 /// Determine whether one version number follows or is
160 /// equivalent to another.
161 ///
162 /// If not provided, minor and subminor version numbers are considered to be
163 /// zero.
164 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
165 return !(X < Y);
166 }
167};
168
169} // end namespace wpi
170#endif // WPIUTIL_WPI_VERSIONTUPLE_H
This file defines DenseMapInfo traits for DenseMap.
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition VersionTuple.h:26
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition VersionTuple.h:155
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition VersionTuple.h:92
constexpr VersionTuple()
Definition VersionTuple.h:39
std::optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition VersionTuple.h:85
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition VersionTuple.h:51
std::optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition VersionTuple.h:78
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
Definition VersionTuple.h:129
constexpr VersionTuple(unsigned Major, unsigned Minor)
Definition VersionTuple.h:47
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition VersionTuple.h:137
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition VersionTuple.h:164
VersionTuple normalize() const
Return a version tuple that contains only components that are non-zero.
Definition VersionTuple.h:105
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition VersionTuple.h:71
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
Definition VersionTuple.h:63
VersionTuple withMajorReplaced(unsigned NewMajor) const
Return a version tuple that contains a different major version but everything else is the same.
Definition VersionTuple.h:100
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition VersionTuple.h:56
unsigned getMajor() const
Retrieve the major version number.
Definition VersionTuple.h:68
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition VersionTuple.h:120
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition VersionTuple.h:146
constexpr VersionTuple(unsigned Major)
Definition VersionTuple.h:43
Definition ntcore_cpp.h:26