VersionTuple.h revision 221339
1218885Sdim//===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
2218885Sdim//
3353358Sdim//                     The LLVM Compiler Infrastructure
4353358Sdim//
5353358Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This header defines the VersionTuple class, which represents a version in
11218885Sdim// the form major[.minor[.subminor]].
12218885Sdim//
13218885Sdim//===----------------------------------------------------------------------===//
14218885Sdim#ifndef LLVM_CLANG_BASIC_VERSIONTUPLE_H
15218885Sdim#define LLVM_CLANG_BASIC_VERSIONTUPLE_H
16218885Sdim
17218885Sdim#include "llvm/ADT/Optional.h"
18218885Sdim#include <string>
19218885Sdim
20218885Sdimnamespace llvm {
21218885Sdim  class raw_ostream;
22218885Sdim}
23218885Sdim
24218885Sdimnamespace clang {
25218885Sdim
26218885Sdim/// \brief Represents a version number in the form major[.minor[.subminor]].
27218885Sdimclass VersionTuple {
28218885Sdim  unsigned Major;
29218885Sdim  unsigned Minor : 31;
30218885Sdim  unsigned Subminor : 31;
31296417Sdim  unsigned HasMinor : 1;
32218885Sdim  unsigned HasSubminor : 1;
33218885Sdim
34218885Sdimpublic:
35218885Sdim  VersionTuple()
36218885Sdim    : Major(0), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false) { }
37218885Sdim
38218885Sdim  explicit VersionTuple(unsigned Major)
39309124Sdim    : Major(Major), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false)
40309124Sdim  { }
41309124Sdim
42309124Sdim  explicit VersionTuple(unsigned Major, unsigned Minor)
43309124Sdim    : Major(Major), Minor(Minor), Subminor(0), HasMinor(true),
44309124Sdim      HasSubminor(false)
45309124Sdim  { }
46309124Sdim
47309124Sdim  explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
48296417Sdim    : Major(Major), Minor(Minor), Subminor(Subminor), HasMinor(true),
49296417Sdim      HasSubminor(true)
50218885Sdim  { }
51218885Sdim
52218885Sdim  /// \brief Determine whether this version information is empty
53218885Sdim  /// (e.g., all version components are zero).
54218885Sdim  bool empty() const { return Major == 0 && Minor == 0 && Subminor == 0; }
55218885Sdim
56218885Sdim  /// \brief Retrieve the major version number.
57218885Sdim  unsigned getMajor() const { return Major; }
58218885Sdim
59218885Sdim  /// \brief Retrieve the minor version number, if provided.
60218885Sdim  llvm::Optional<unsigned> getMinor() const {
61218885Sdim    if (!HasMinor)
62218885Sdim      return llvm::Optional<unsigned>();
63218885Sdim    return Minor;
64218885Sdim  }
65218885Sdim
66218885Sdim  /// \brief Retrieve the subminor version number, if provided.
67218885Sdim  llvm::Optional<unsigned> getSubminor() const {
68218885Sdim    if (!HasSubminor)
69218885Sdim      return llvm::Optional<unsigned>();
70218885Sdim    return Subminor;
71218885Sdim  }
72218885Sdim
73218885Sdim  /// \brief Determine if two version numbers are equivalent. If not
74218885Sdim  /// provided, minor and subminor version numbers are considered to be zero.
75218885Sdim  friend bool operator==(const VersionTuple& X, const VersionTuple &Y) {
76218885Sdim    return X.Major == Y.Major && X.Minor == Y.Minor && X.Subminor == Y.Subminor;
77  }
78
79  /// \brief Determine if two version numbers are not equivalent. If
80  /// not provided, minor and subminor version numbers are considered to be
81  /// zero.
82  friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
83    return !(X == Y);
84  }
85
86  /// \brief Determine whether one version number precedes another. If not
87  /// provided, minor and subminor version numbers are considered to be zero.
88  friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
89    if (X.Major != Y.Major)
90      return X.Major < Y.Major;
91
92    if (X.Minor != Y.Minor)
93      return X.Minor < Y.Minor;
94
95    return X.Subminor < Y.Subminor;
96  }
97
98  /// \brief Determine whether one version number follows another. If not
99  /// provided, minor and subminor version numbers are considered to be zero.
100  friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
101    return Y < X;
102  }
103
104  /// \brief Determine whether one version number precedes or is
105  /// equivalent to another. If not provided, minor and subminor
106  /// version numbers are considered to be zero.
107  friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
108    return !(Y < X);
109  }
110
111  /// \brief Determine whether one version number follows or is
112  /// equivalent to another. If not provided, minor and subminor
113  /// version numbers are considered to be zero.
114  friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
115    return !(X < Y);
116  }
117
118  /// \brief Retrieve a string representation of the version number/
119  std::string getAsString() const;
120};
121
122/// \brief Print a version number.
123llvm::raw_ostream& operator<<(llvm::raw_ostream &Out, const VersionTuple &V);
124
125} // end namespace clang
126#endif // LLVM_CLANG_BASIC_VERSIONTUPLE_H
127