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