LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_VERSION_H
21 #define LIBREPCB_VERSION_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "fileio/sexpression.h"
27 
28 #include <optional/tl/optional.hpp>
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  * Class Version
39  ******************************************************************************/
40 
60 class Version final {
61  Q_DECLARE_TR_FUNCTIONS(Version)
62 
63 public:
64  // Constructors / Destructor
65 
69  Version() = delete;
70 
76  Version(const Version& other) noexcept : mNumbers(other.mNumbers) {}
77 
81  ~Version() noexcept = default;
82 
83  // Getters
84 
95  bool isPrefixOf(const Version& other) const noexcept;
96 
104  const QVector<uint>& getNumbers() const noexcept { return mNumbers; }
105 
111  QString toStr() const noexcept;
112 
125  QString toPrettyStr(int minSegCount, int maxSegCount = 10) const noexcept;
126 
138  QString toComparableStr() const noexcept;
139 
140  // Operator overloadings
141  Version& operator=(const Version& rhs) noexcept {
142  mNumbers = rhs.mNumbers;
143  return *this;
144  }
145 
147 
154  bool operator>(const Version& rhs) const noexcept {
155  return toComparableStr() > rhs.toComparableStr();
156  }
157  bool operator<(const Version& rhs) const noexcept {
158  return toComparableStr() < rhs.toComparableStr();
159  }
160  bool operator>=(const Version& rhs) const noexcept {
161  return toComparableStr() >= rhs.toComparableStr();
162  }
163  bool operator<=(const Version& rhs) const noexcept {
164  return toComparableStr() <= rhs.toComparableStr();
165  }
166  bool operator==(const Version& rhs) const noexcept {
167  return mNumbers == rhs.mNumbers;
168  }
169  bool operator!=(const Version& rhs) const noexcept {
170  return mNumbers != rhs.mNumbers;
171  }
173 
174  // Static Methods
175 
184  static bool isValid(const QString& str) noexcept;
185 
196  static Version fromString(const QString& str);
197 
207  static tl::optional<Version> tryFromString(const QString& str) noexcept;
208 
209 private: // Methods
210  explicit Version(const QVector<uint>& numbers) noexcept : mNumbers(numbers) {}
211 
212 private: // Data
218  QVector<uint> mNumbers;
219 };
220 
221 /*******************************************************************************
222  * Non-Member Functions
223  ******************************************************************************/
224 
225 template <>
227  return SExpression::createString(obj.toStr());
228 }
229 
230 template <>
232  bool throwIfEmpty) {
233  QString str = sexpr.getStringOrToken(throwIfEmpty);
234  return Version::fromString(str); // can throw
235 }
236 
237 /*******************************************************************************
238  * End of File
239  ******************************************************************************/
240 
241 } // namespace librepcb
242 
243 #endif // LIBREPCB_VERSION_H
bool operator<=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:163
static bool isValid(const QString &str) noexcept
Check if a string is a valid version number.
Definition: version.cpp:82
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
bool operator>=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:160
Version()=delete
Default constructor (disabled to avoid creating invalid versions)
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
bool isPrefixOf(const Version &other) const noexcept
Check if this version is the prefix of another version.
Definition: version.cpp:36
~Version() noexcept=default
bool operator==(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:166
QVector< uint > mNumbers
List of all version numbers of the whole version.
Definition: version.h:218
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
Version(const Version &other) noexcept
Copy constructor.
Definition: version.h:76
QString toStr() const noexcept
Get the version as a string in the format "1.2.3".
Definition: version.cpp:49
static SExpression createString(const QString &string)
Definition: sexpression.cpp:284
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:53
const QVector< uint > & getNumbers() const noexcept
Get the numbers in the version string.
Definition: version.h:104
bool operator<(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:157
bool operator>(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:154
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:98
bool operator!=(const Version &rhs) const noexcept
Comparison operators.
Definition: version.h:169
static Version fromString(const QString &str)
Create a Version object from a string.
Definition: version.cpp:87
Version(const QVector< uint > &numbers) noexcept
Definition: version.h:210
The SExpression class.
Definition: sexpression.h:60
QString toComparableStr() const noexcept
Get the version as a comparable string (59 characters)
Definition: version.cpp:65