WPILibC++ 2023.4.3-108-ge5452e3
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 {
24class raw_ostream;
25
26/// Represents a version number in the form major[.minor[.subminor[.build]]].
28 unsigned Major : 32;
29
30 unsigned Minor : 31;
31 unsigned HasMinor : 1;
32
33 unsigned Subminor : 31;
34 unsigned HasSubminor : 1;
35
36 unsigned Build : 31;
37 unsigned HasBuild : 1;
38
39public:
41 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
42 Build(0), HasBuild(false) {}
43
44 explicit VersionTuple(unsigned Major)
45 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
46 HasSubminor(false), Build(0), HasBuild(false) {}
47
48 explicit VersionTuple(unsigned Major, unsigned Minor)
49 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
50 HasSubminor(false), Build(0), HasBuild(false) {}
51
52 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
53 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
54 HasSubminor(true), Build(0), HasBuild(false) {}
55
56 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
57 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 only components that are non-zero.
100 VersionTuple Result = *this;
101 if (Result.Build == 0) {
102 Result.HasBuild = false;
103 if (Result.Subminor == 0) {
104 Result.HasSubminor = false;
105 if (Result.Minor == 0)
106 Result.HasMinor = false;
107 }
108 }
109 return Result;
110 }
111
112 /// Determine if two version numbers are equivalent. If not
113 /// provided, minor and subminor version numbers are considered to be zero.
114 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
115 return X.Major == Y.Major && X.Minor == Y.Minor &&
116 X.Subminor == Y.Subminor && X.Build == Y.Build;
117 }
118
119 /// Determine if two version numbers are not equivalent.
120 ///
121 /// If not provided, minor and subminor version numbers are considered to be
122 /// zero.
123 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
124 return !(X == Y);
125 }
126
127 /// Determine whether one version number precedes another.
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 std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
133 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
134 }
135
136 /// Determine whether one version number follows another.
137 ///
138 /// If not provided, minor and subminor version numbers are considered to be
139 /// zero.
140 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
141 return Y < X;
142 }
143
144 /// Determine whether one version number precedes or is
145 /// equivalent to another.
146 ///
147 /// If not provided, minor and subminor version numbers are considered to be
148 /// zero.
149 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
150 return !(Y < X);
151 }
152
153 /// Determine whether one version number follows or is
154 /// equivalent to another.
155 ///
156 /// If not provided, minor and subminor version numbers are considered to be
157 /// zero.
158 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
159 return !(X < Y);
160 }
161};
162
163} // end namespace wpi
164#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:27
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition: VersionTuple.h:149
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition: VersionTuple.h:92
std::optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:85
VersionTuple(unsigned Major, unsigned Minor)
Definition: VersionTuple.h:48
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:123
VersionTuple(unsigned Major)
Definition: VersionTuple.h:44
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition: VersionTuple.h:131
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition: VersionTuple.h:158
VersionTuple normalize() const
Return a version tuple that contains only components that are non-zero.
Definition: VersionTuple.h:99
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(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition: VersionTuple.h:56
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:68
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition: VersionTuple.h:52
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition: VersionTuple.h:114
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition: VersionTuple.h:140
VersionTuple()
Definition: VersionTuple.h:40
Definition: AprilTagFieldLayout.h:18