LibrePCB Developers Documentation
version.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_VERSION_H
21 #define LIBREPCB_CORE_VERSION_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 #include <optional.hpp>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class Version
37  ******************************************************************************/
38 
58 class Version final {
59  Q_DECLARE_TR_FUNCTIONS(Version)
60 
61 public:
62  // Constructors / Destructor
63 
67  Version() = delete;
68 
74  Version(const Version& other) noexcept : mNumbers(other.mNumbers) {}
75 
79  ~Version() noexcept = default;
80 
81  // Getters
82 
93  bool isPrefixOf(const Version& other) const noexcept;
94 
102  const QVector<uint>& getNumbers() const noexcept { return mNumbers; }
103 
109  QString toStr() const noexcept;
110 
123  QString toPrettyStr(int minSegCount, int maxSegCount = 10) const noexcept;
124 
136  QString toComparableStr() const noexcept;
137 
138  // Operator overloadings
139  Version& operator=(const Version& rhs) noexcept {
140  mNumbers = rhs.mNumbers;
141  return *this;
142  }
143 
145 
152  bool operator>(const Version& rhs) const noexcept {
153  return toComparableStr() > rhs.toComparableStr();
154  }
155  bool operator<(const Version& rhs) const noexcept {
156  return toComparableStr() < rhs.toComparableStr();
157  }
158  bool operator>=(const Version& rhs) const noexcept {
159  return toComparableStr() >= rhs.toComparableStr();
160  }
161  bool operator<=(const Version& rhs) const noexcept {
162  return toComparableStr() <= rhs.toComparableStr();
163  }
164  bool operator==(const Version& rhs) const noexcept {
165  return mNumbers == rhs.mNumbers;
166  }
167  bool operator!=(const Version& rhs) const noexcept {
168  return mNumbers != rhs.mNumbers;
169  }
171 
172  // Static Methods
173 
182  static bool isValid(const QString& str) noexcept;
183 
194  static Version fromString(const QString& str);
195 
205  static tl::optional<Version> tryFromString(const QString& str) noexcept;
206 
207 private: // Methods
208  explicit Version(const QVector<uint>& numbers) noexcept : mNumbers(numbers) {}
209 
210 private: // Data
216  QVector<uint> mNumbers;
217 };
218 
219 /*******************************************************************************
220  * End of File
221  ******************************************************************************/
222 
223 } // namespace librepcb
224 
225 #endif
bool operator<=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:161
static bool isValid(const QString &str) noexcept
Check if a string is a valid version number.
Definition: version.cpp:85
The Version class represents a version number in the format "1.42.7".
Definition: version.h:58
Definition: occmodel.cpp:76
bool operator>=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:158
Version()=delete
Default constructor (disabled to avoid creating invalid versions)
Version & operator=(const Version &rhs) noexcept
Definition: version.h:139
bool isPrefixOf(const Version &other) const noexcept
Check if this version is the prefix of another version.
Definition: version.cpp:39
~Version() noexcept=default
bool operator==(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:164
QVector< uint > mNumbers
List of all version numbers of the whole version.
Definition: version.h:216
Version(const Version &other) noexcept
Copy constructor.
Definition: version.h:74
QString toStr() const noexcept
Get the version as a string in the format "1.2.3".
Definition: version.cpp:52
QString toPrettyStr(int minSegCount, int maxSegCount=10) const noexcept
Get the version as a string with trailing zeros (e.g. "1.2.0")
Definition: version.cpp:56
const QVector< uint > & getNumbers() const noexcept
Get the numbers in the version string.
Definition: version.h:102
bool operator<(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:155
bool operator>(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:152
static tl::optional< Version > tryFromString(const QString &str) noexcept
Try creating a Version object from a string, returning empty optional if invalid. ...
Definition: version.cpp:101
bool operator!=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:167
static Version fromString(const QString &str)
Create a Version object from a string.
Definition: version.cpp:90
Version(const QVector< uint > &numbers) noexcept
Definition: version.h:208
QString toComparableStr() const noexcept
Get the version as a comparable string (59 characters)
Definition: version.cpp:68