WebAPI: Add a way to download .torrent file using search plugin
[qBittorrent.git] / src / base / utils / version.h
blob36f20512eb33f7b137fa1745f2f979010ee7605b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
4 * Copyright (C) 2016 Eugene Shalygin
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #pragma once
32 #include <array>
33 #include <type_traits>
35 #include <QList>
36 #include <QString>
37 #include <QStringView>
39 namespace Utils
41 // This class provides a default implementation of `isValid()` that should work for most cases
42 // It is ultimately up to the user to decide whether the version numbers are useful/meaningful
43 template <int N, int Mandatory = N>
44 class Version final
46 static_assert((N > 0), "The number of version components may not be smaller than 1");
47 static_assert((Mandatory > 0), "The number of mandatory components may not be smaller than 1");
48 static_assert((N >= Mandatory),
49 "The number of mandatory components may not be larger than the total number of components");
51 public:
52 using ThisType = Version<N, Mandatory>;
54 constexpr Version() = default;
56 Version(const QStringView string)
58 *this = fromString(string);
61 template <typename ... Ts>
62 constexpr Version(Ts ... params)
63 requires std::conjunction_v<std::is_convertible<Ts, int>...>
64 : m_components {{params ...}}
66 static_assert((sizeof...(Ts) <= N), "Too many parameters provided");
67 static_assert((sizeof...(Ts) >= Mandatory), "Not enough parameters provided");
70 constexpr bool isValid() const
72 bool hasValid = false;
73 for (const int i : m_components)
75 if (i < 0)
76 return false;
77 if (i > 0)
78 hasValid = true;
80 return hasValid;
83 constexpr int majorNumber() const
85 return m_components[0];
88 constexpr int minorNumber() const
90 static_assert((N >= 2), "The number of version components is too small");
92 return m_components[1];
95 constexpr int revisionNumber() const
97 static_assert((N >= 3), "The number of version components is too small");
99 return m_components[2];
102 constexpr int patchNumber() const
104 static_assert((N >= 4), "The number of version components is too small");
106 return m_components[3];
109 constexpr int operator[](const int i) const
111 return m_components.at(i);
114 QString toString() const
116 // find the last one non-zero component
117 int lastSignificantIndex = N - 1;
118 while ((lastSignificantIndex > 0) && (m_components[lastSignificantIndex] == 0))
119 --lastSignificantIndex;
121 if ((lastSignificantIndex + 1) < Mandatory) // lastSignificantIndex >= 0
122 lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1
124 QString res = QString::number(m_components[0]);
125 for (int i = 1; i <= lastSignificantIndex; ++i)
126 res += (u'.' + QString::number(m_components[i]));
127 return res;
130 friend constexpr bool operator==(const ThisType &left, const ThisType &right)
132 return (left.m_components == right.m_components);
135 friend constexpr bool operator<(const ThisType &left, const ThisType &right)
137 return (left.m_components < right.m_components);
140 static Version fromString(const QStringView string, const Version &defaultVersion = {})
142 const QList<QStringView> stringParts = string.split(u'.');
143 const int count = stringParts.size();
145 if ((count > N) || (count < Mandatory))
146 return defaultVersion;
148 Version version;
149 for (int i = 0; i < count; ++i)
151 bool ok = false;
152 version.m_components[i] = stringParts[i].toInt(&ok);
153 if (!ok)
154 return defaultVersion;
157 return version;
160 private:
161 std::array<int, N> m_components {{}};
164 template <int N, int Mandatory>
165 constexpr bool operator>(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
167 return (right < left);
170 template <int N, int Mandatory>
171 constexpr bool operator<=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
173 return !(left > right);
176 template <int N, int Mandatory>
177 constexpr bool operator>=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
179 return !(left < right);