WPILibC++ 2024.3.2
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 "wpi/Hashing.h"
19#include "wpi/Endian.h"
20#include <optional>
21#include <string>
22#include <tuple>
23
24namespace wpi {
25template <typename HasherT, support::endianness Endianness>
27class raw_ostream;
28
29/// Represents a version number in the form major[.minor[.subminor[.build]]].
31 unsigned Major : 32;
32
33 unsigned Minor : 31;
34 unsigned HasMinor : 1;
35
36 unsigned Subminor : 31;
37 unsigned HasSubminor : 1;
38
39 unsigned Build : 31;
40 unsigned HasBuild : 1;
41
42public:
43 constexpr VersionTuple()
44 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
45 Build(0), HasBuild(false) {}
46
47 explicit constexpr VersionTuple(unsigned Major)
48 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
49 HasSubminor(false), Build(0), HasBuild(false) {}
50
51 explicit constexpr VersionTuple(unsigned Major, unsigned Minor)
52 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
53 HasSubminor(false), Build(0), HasBuild(false) {}
54
55 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
56 unsigned Subminor)
57 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
58 HasSubminor(true), Build(0), HasBuild(false) {}
59
60 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
61 unsigned Subminor, unsigned Build)
62 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
63 HasSubminor(true), Build(Build), HasBuild(true) {}
64
65 /// Determine whether this version information is empty
66 /// (e.g., all version components are zero).
67 bool empty() const {
68 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
69 }
70
71 /// Retrieve the major version number.
72 unsigned getMajor() const { return Major; }
73
74 /// Retrieve the minor version number, if provided.
75 std::optional<unsigned> getMinor() const {
76 if (!HasMinor)
77 return std::nullopt;
78 return Minor;
79 }
80
81 /// Retrieve the subminor version number, if provided.
82 std::optional<unsigned> getSubminor() const {
83 if (!HasSubminor)
84 return std::nullopt;
85 return Subminor;
86 }
87
88 /// Retrieve the build version number, if provided.
89 std::optional<unsigned> getBuild() const {
90 if (!HasBuild)
91 return std::nullopt;
92 return Build;
93 }
94
95 /// Return a version tuple that contains only the first 3 version components.
97 if (HasBuild)
98 return VersionTuple(Major, Minor, Subminor);
99 return *this;
100 }
101
102 /// Return a version tuple that contains a different major version but
103 /// everything else is the same.
104 VersionTuple withMajorReplaced(unsigned NewMajor) const {
105 return VersionTuple(NewMajor, Minor, Subminor, Build);
106 }
107
108 /// Return a version tuple that contains only components that are non-zero.
110 VersionTuple Result = *this;
111 if (Result.Build == 0) {
112 Result.HasBuild = false;
113 if (Result.Subminor == 0) {
114 Result.HasSubminor = false;
115 if (Result.Minor == 0)
116 Result.HasMinor = false;
117 }
118 }
119 return Result;
120 }
121
122 /// Determine if two version numbers are equivalent. If not
123 /// provided, minor and subminor version numbers are considered to be zero.
124 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
125 return X.Major == Y.Major && X.Minor == Y.Minor &&
126 X.Subminor == Y.Subminor && X.Build == Y.Build;
127 }
128
129 /// Determine if two version numbers are not equivalent.
130 ///
131 /// If not provided, minor and subminor version numbers are considered to be
132 /// zero.
133 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
134 return !(X == Y);
135 }
136
137 /// Determine whether one version number precedes another.
138 ///
139 /// If not provided, minor and subminor version numbers are considered to be
140 /// zero.
141 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
142 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
143 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
144 }
145
146 /// Determine whether one version number follows another.
147 ///
148 /// If not provided, minor and subminor version numbers are considered to be
149 /// zero.
150 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
151 return Y < X;
152 }
153
154 /// Determine whether one version number precedes or is
155 /// equivalent to another.
156 ///
157 /// If not provided, minor and subminor version numbers are considered to be
158 /// zero.
159 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
160 return !(Y < X);
161 }
162
163 /// Determine whether one version number follows or is
164 /// equivalent to another.
165 ///
166 /// If not provided, minor and subminor version numbers are considered to be
167 /// zero.
168 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
169 return !(X < Y);
170 }
171};
172
173} // end namespace wpi
174#endif // WPIUTIL_WPI_VERSIONTUPLE_H
This file defines DenseMapInfo traits for DenseMap.
Definition: VersionTuple.h:26
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:30
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition: VersionTuple.h:159
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition: VersionTuple.h:96
constexpr VersionTuple()
Definition: VersionTuple.h:43
std::optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:89
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition: VersionTuple.h:55
std::optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:82
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
Definition: VersionTuple.h:133
constexpr VersionTuple(unsigned Major, unsigned Minor)
Definition: VersionTuple.h:51
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition: VersionTuple.h:141
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition: VersionTuple.h:168
VersionTuple normalize() const
Return a version tuple that contains only components that are non-zero.
Definition: VersionTuple.h:109
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:75
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
Definition: VersionTuple.h:67
VersionTuple withMajorReplaced(unsigned NewMajor) const
Return a version tuple that contains a different major version but everything else is the same.
Definition: VersionTuple.h:104
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition: VersionTuple.h:60
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:72
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition: VersionTuple.h:124
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition: VersionTuple.h:150
constexpr VersionTuple(unsigned Major)
Definition: VersionTuple.h:47
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:43
Definition: ntcore_cpp.h:26