Browse Source

Redesign Version class

PR #17484.
adaptive-webui-19844
Chocobo1 2 years ago committed by GitHub
parent
commit
33e6ca6778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/base/search/searchpluginmanager.cpp
  2. 2
      src/base/search/searchpluginmanager.h
  3. 11
      src/base/utils/foreignapps.cpp
  4. 2
      src/base/utils/foreignapps.h
  5. 152
      src/base/utils/version.h
  6. 28
      src/gui/programupdater.cpp
  7. 4
      src/gui/utils.cpp
  8. 2
      src/webui/webapplication.h
  9. 145
      test/testutilsversion.cpp

4
src/base/search/searchpluginmanager.cpp

@ -528,7 +528,7 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info) @@ -528,7 +528,7 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info)
if (list.size() != 2) continue;
const auto pluginName = QString::fromUtf8(list.first().trimmed());
const PluginVersion version = PluginVersion::tryParse(list.last().trimmed(), {});
const auto version = PluginVersion::fromString(QString::fromLatin1(list.last().trimmed()));
if (!version.isValid()) continue;
@ -577,7 +577,7 @@ PluginVersion SearchPluginManager::getPluginVersion(const Path &filePath) @@ -577,7 +577,7 @@ PluginVersion SearchPluginManager::getPluginVersion(const Path &filePath)
if (!line.startsWith(u"#VERSION:", Qt::CaseInsensitive)) continue;
const QString versionStr = line.mid(9);
const PluginVersion version = PluginVersion::tryParse(versionStr, {});
const auto version = PluginVersion::fromString(versionStr);
if (version.isValid())
return version;

2
src/base/search/searchpluginmanager.h

@ -36,7 +36,7 @@ @@ -36,7 +36,7 @@
#include "base/path.h"
#include "base/utils/version.h"
using PluginVersion = Utils::Version<short, 2>;
using PluginVersion = Utils::Version<2>;
Q_DECLARE_METATYPE(PluginVersion)
namespace Net

11
src/base/utils/foreignapps.cpp

@ -72,16 +72,11 @@ namespace @@ -72,16 +72,11 @@ namespace
// So trim off unrelated characters
const auto versionStr = QString::fromLocal8Bit(outputSplit[1]);
const int idx = versionStr.indexOf(QRegularExpression(u"[^\\.\\d]"_qs));
try
{
info = {exeName, versionStr.left(idx)};
}
catch (const RuntimeError &)
{
const auto version = PythonInfo::Version::fromString(versionStr.left(idx));
if (!version.isValid())
return false;
}
info = {exeName, version};
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, executable name: '%1', version: %2")
.arg(info.executableName, info.version.toString()), Log::INFO);
return true;

2
src/base/utils/foreignapps.h

@ -37,7 +37,7 @@ namespace Utils::ForeignApps @@ -37,7 +37,7 @@ namespace Utils::ForeignApps
{
struct PythonInfo
{
using Version = Utils::Version<quint8, 3, 1>;
using Version = Utils::Version<3, 1>;
bool isValid() const;
bool isSupportedVersion() const;

152
src/base/utils/version.h

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1)
* Copyright (C) 2016 Eugene Shalygin
*
* This program is free software; you can redistribute it and/or
@ -31,84 +32,78 @@ @@ -31,84 +32,78 @@
#include <array>
#include <type_traits>
#include <QDebug>
#include <QList>
#include <QString>
#include <QStringView>
#include "base/exceptions.h"
#include "base/global.h"
#include "base/interfaces/istringable.h"
namespace Utils
{
template <typename T, std::size_t N, std::size_t Mandatory = N>
// This class provides a default implementation of `isValid()` that should work for most cases
// It is ultimately up to the user to decide whether the version numbers are useful/meaningful
template <int N, int Mandatory = N>
class Version final : public IStringable
{
static_assert((N > 0), "The number of version components may not be smaller than 1");
static_assert((Mandatory > 0), "The number of mandatory components may not be smaller than 1");
static_assert((N >= Mandatory),
"The number of mandatory components may not be larger than the total number of components");
public:
typedef T ComponentType;
typedef Version<T, N, Mandatory> ThisType;
using ThisType = Version<N, Mandatory>;
constexpr Version() = default;
template <typename ... Other
, typename std::enable_if_t<std::conjunction_v<std::is_constructible<T, Other>...>, int> = 0>
constexpr Version(Other ... components)
: m_components {{static_cast<T>(components) ...}}
template <typename ... Ts
, typename std::enable_if_t<std::conjunction_v<std::is_convertible<Ts, int>...>, int> = 0>
constexpr Version(Ts ... params)
: m_components {{params ...}}
{
static_assert((sizeof...(Other) <= N), "Too many parameters provided");
static_assert((sizeof...(Other) >= Mandatory), "Not enough parameters provided");
static_assert((sizeof...(Ts) <= N), "Too many parameters provided");
static_assert((sizeof...(Ts) >= Mandatory), "Not enough parameters provided");
}
/**
* @brief Creates version from string in format "x.y.z"
*
* @param version Version string in format "x.y.z"
* @throws RuntimeError if parsing fails
*/
Version(const QString &version)
: m_components {parseList(version.split(u'.'))}
{
}
/**
* @brief Creates version from byte array in format "x.y.z"
*
* @param version Version string in format "x.y.z"
* @throws RuntimeError if parsing fails
*/
Version(const QByteArray &version)
: m_components {parseList(version.split('.'))}
constexpr bool isValid() const
{
bool hasValid = false;
for (const int i : m_components)
{
if (i < 0)
return false;
if (i > 0)
hasValid = true;
}
return hasValid;
}
constexpr ComponentType majorNumber() const
constexpr int majorNumber() const
{
static_assert(N >= 1, "The number of version components is too small");
return m_components[0];
}
constexpr ComponentType minorNumber() const
constexpr int minorNumber() const
{
static_assert(N >= 2, "The number of version components is too small");
static_assert((N >= 2), "The number of version components is too small");
return m_components[1];
}
constexpr ComponentType revisionNumber() const
constexpr int revisionNumber() const
{
static_assert(N >= 3, "The number of version components is too small");
static_assert((N >= 3), "The number of version components is too small");
return m_components[2];
}
constexpr ComponentType patchNumber() const
constexpr int patchNumber() const
{
static_assert(N >= 4, "The number of version components is too small");
static_assert((N >= 4), "The number of version components is too small");
return m_components[3];
}
constexpr ComponentType operator[](const std::size_t i) const
constexpr int operator[](const int i) const
{
return m_components.at(i);
}
@ -116,24 +111,19 @@ namespace Utils @@ -116,24 +111,19 @@ namespace Utils
QString toString() const override
{
// find the last one non-zero component
std::size_t lastSignificantIndex = N - 1;
while ((lastSignificantIndex > 0) && ((*this)[lastSignificantIndex] == 0))
int lastSignificantIndex = N - 1;
while ((lastSignificantIndex > 0) && (m_components[lastSignificantIndex] == 0))
--lastSignificantIndex;
if (lastSignificantIndex + 1 < Mandatory) // lastSignificantIndex >= 0
if ((lastSignificantIndex + 1) < Mandatory) // lastSignificantIndex >= 0
lastSignificantIndex = Mandatory - 1; // and Mandatory >= 1
QString res = QString::number((*this)[0]);
for (std::size_t i = 1; i <= lastSignificantIndex; ++i)
res += (u'.' + QString::number((*this)[i]));
QString res = QString::number(m_components[0]);
for (int i = 1; i <= lastSignificantIndex; ++i)
res += (u'.' + QString::number(m_components[i]));
return res;
}
constexpr bool isValid() const
{
return (*this != ThisType {});
}
// TODO: remove manually defined operators and use compiler generated `operator<=>()` in C++20
friend bool operator==(const ThisType &left, const ThisType &right)
{
@ -145,66 +135,50 @@ namespace Utils @@ -145,66 +135,50 @@ namespace Utils
return (left.m_components < right.m_components);
}
template <typename StringClassWithSplitMethod>
static Version tryParse(const StringClassWithSplitMethod &s, const Version &defaultVersion)
static Version fromString(const QStringView string, const Version &defaultVersion = {})
{
try
{
return Version(s);
}
catch (const RuntimeError &error)
{
qDebug() << "Error parsing version:" << error.message();
return defaultVersion;
}
}
private:
using ComponentsArray = std::array<T, N>;
const QList<QStringView> stringParts = string.split(u'.');
const int count = stringParts.size();
template <typename StringList>
static ComponentsArray parseList(const StringList &versionParts)
{
if ((static_cast<std::size_t>(versionParts.size()) > N)
|| (static_cast<std::size_t>(versionParts.size()) < Mandatory))
{
throw RuntimeError(u"Incorrect number of version components"_qs);
}
if ((count > N) || (count < Mandatory))
return defaultVersion;
bool ok = false;
ComponentsArray res {{}};
for (std::size_t i = 0; i < static_cast<std::size_t>(versionParts.size()); ++i)
Version version;
for (int i = 0; i < count; ++i)
{
res[i] = static_cast<T>(versionParts[static_cast<typename StringList::size_type>(i)].toInt(&ok));
bool ok = false;
version.m_components[i] = stringParts[i].toInt(&ok);
if (!ok)
throw RuntimeError(u"Can not parse version component"_qs);
return defaultVersion;
}
return res;
return version;
}
ComponentsArray m_components {{}};
private:
std::array<int, N> m_components {{}};
};
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator!=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
template <int N, int Mandatory>
constexpr bool operator!=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{
return !(left == right);
}
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator>(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
template <int N, int Mandatory>
constexpr bool operator>(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{
return (right < left);
}
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator<=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
template <int N, int Mandatory>
constexpr bool operator<=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{
return !(left > right);
}
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator>=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
template <int N, int Mandatory>
constexpr bool operator>=(const Version<N, Mandatory> &left, const Version<N, Mandatory> &right)
{
return !(left < right);
}

28
src/gui/programupdater.cpp

@ -52,25 +52,21 @@ namespace @@ -52,25 +52,21 @@ namespace
{
bool isVersionMoreRecent(const QString &remoteVersion)
{
using Version = Utils::Version<int, 4, 3>;
using Version = Utils::Version<4, 3>;
try
{
const Version newVersion {remoteVersion};
const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
if (newVersion == currentVersion)
{
const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
QRegularExpression(u"(alpha|beta|rc)"_qs));
if (isDevVersion)
return true;
}
return (newVersion > currentVersion);
}
catch (const RuntimeError &)
{
const auto newVersion = Version::fromString(remoteVersion);
if (!newVersion.isValid())
return false;
const Version currentVersion {QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD};
if (newVersion == currentVersion)
{
const bool isDevVersion = QStringLiteral(QBT_VERSION_STATUS).contains(
QRegularExpression(u"(alpha|beta|rc)"_qs));
if (isDevVersion)
return true;
}
return (newVersion > currentVersion);
}
}

4
src/gui/utils.cpp

@ -193,8 +193,8 @@ void Utils::Gui::openFolderSelect(const Path &path) @@ -193,8 +193,8 @@ void Utils::Gui::openFolderSelect(const Path &path)
proc.start(u"nautilus"_qs, {u"--version"_qs});
proc.waitForFinished();
const auto nautilusVerStr = QString::fromLocal8Bit(proc.readLine()).remove(QRegularExpression(u"[^0-9.]"_qs));
using NautilusVersion = Utils::Version<int, 3>;
if (NautilusVersion::tryParse(nautilusVerStr, {1, 0, 0}) > NautilusVersion {3, 28, 0})
using NautilusVersion = Utils::Version<3>;
if (NautilusVersion::fromString(nautilusVerStr, {1, 0, 0}) > NautilusVersion(3, 28, 0))
proc.startDetached(u"nautilus"_qs, {(Fs::isDir(path) ? path.parentPath() : path).toString()});
else
proc.startDetached(u"nautilus"_qs, {u"--no-desktop"_qs, (Fs::isDir(path) ? path.parentPath() : path).toString()});

2
src/webui/webapplication.h

@ -49,7 +49,7 @@ @@ -49,7 +49,7 @@
#include "base/utils/version.h"
#include "api/isessionmanager.h"
inline const Utils::Version<int, 3, 2> API_VERSION {2, 8, 14};
inline const Utils::Version<3, 2> API_VERSION {2, 8, 14};
class APIController;
class AuthController;

145
test/testutilsversion.cpp

@ -42,96 +42,131 @@ public: @@ -42,96 +42,131 @@ public:
private slots:
void testConstructors() const
{
using TwoDigits = Utils::Version<unsigned char, 2, 1>;
TwoDigits();
// should not compile:
// Utils::Version<-1>();
// Utils::Version<0>();
// Utils::Version<2, 3>();
// Utils::Version<2, -1>();
// Utils::Version<2, 0>();
using TwoDigits = Utils::Version<2, 1>;
TwoDigits(0);
TwoDigits(50);
TwoDigits(0, 1);
using ThreeDigits = Utils::Version<int, 3>;
using ThreeDigits = Utils::Version<3, 3>;
// should not compile:
// ThreeDigits(1);
// ThreeDigits(1, 2);
// ThreeDigits(1.0, 2, 3);
// ThreeDigits(1, 2, 3, 4);
QCOMPARE(ThreeDigits(u"1.2.3"_qs), ThreeDigits(1, 2, 3));
QCOMPARE(ThreeDigits(QByteArrayLiteral("1.2.3")), ThreeDigits(1, 2, 3));
ThreeDigits(1, 2, 3);
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u""_qs));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1"_qs));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1.0"_qs));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"1.0.1.1"_qs));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(u"random_string"_qs));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1")));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1.0")));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("1.0.1.1")));
QVERIFY_THROWS_EXCEPTION(RuntimeError, ThreeDigits(QByteArrayLiteral("random_string")));
#endif
void testIsValid() const
{
using ThreeDigits = Utils::Version<3>;
QCOMPARE(ThreeDigits().isValid(), false);
QCOMPARE(ThreeDigits(0, 0, 0).isValid(), false);
QCOMPARE(ThreeDigits(0, 0, -1).isValid(), false);
QCOMPARE(ThreeDigits(0, 0, 1).isValid(), true);
QCOMPARE(ThreeDigits(0, 1, 0).isValid(), true);
QCOMPARE(ThreeDigits(1, 0, 0).isValid(), true);
QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
}
void testVersionComponents() const
{
const Utils::Version<int, 1> version1 {1};
const Utils::Version<1> version1 {1};
QCOMPARE(version1[0], 1);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[1]);
#endif
QCOMPARE(version1.majorNumber(), 1);
// should not compile:
// version1.minorNumber();
// version1.revisionNumber();
// version1.patchNumber();
const Utils::Version<2, 1> version2 {2};
QCOMPARE(version2[0], 2);
QCOMPARE(version2[1], 0);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[1]);
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[2]);
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version2[2]);
#endif
QCOMPARE(version2.majorNumber(), 2);
QCOMPARE(version2.minorNumber(), 0);
// should not compile:
// version2.revisionNumber();
// version2.patchNumber();
const Utils::Version<int, 4> version2 {10, 11, 12, 13};
QCOMPARE(version2[0], 10);
QCOMPARE(version2[1], 11);
QCOMPARE(version2[2], 12);
QCOMPARE(version2[3], 13);
QCOMPARE(version2.majorNumber(), 10);
QCOMPARE(version2.minorNumber(), 11);
QCOMPARE(version2.revisionNumber(), 12);
QCOMPARE(version2.patchNumber(), 13);
const Utils::Version<3, 2> version3 {3, 2};
QCOMPARE(version3[0], 3);
QCOMPARE(version3[1], 2);
QCOMPARE(version3[2], 0);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version3[3]);
#endif
QCOMPARE(version3.majorNumber(), 3);
QCOMPARE(version3.minorNumber(), 2);
QCOMPARE(version3.revisionNumber(), 0);
// should not compile:
// version3.patchNumber();
const Utils::Version<4> version4 {10, 11, 12, 13};
QCOMPARE(version4[0], 10);
QCOMPARE(version4[1], 11);
QCOMPARE(version4[2], 12);
QCOMPARE(version4[3], 13);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QVERIFY_THROWS_EXCEPTION(std::out_of_range, version4[4]);
#endif
QCOMPARE(version4.majorNumber(), 10);
QCOMPARE(version4.minorNumber(), 11);
QCOMPARE(version4.revisionNumber(), 12);
QCOMPARE(version4.patchNumber(), 13);
}
void testToString() const
{
using OneMandatory = Utils::Version<int, 2, 1>;
QCOMPARE(OneMandatory(u"10"_qs).toString(), u"10"_qs);
using OneMandatory = Utils::Version<2, 1>;
QCOMPARE(OneMandatory(10).toString(), u"10"_qs);
QCOMPARE(OneMandatory(2).toString(), u"2"_qs);
QCOMPARE(OneMandatory(2, 0).toString(), u"2"_qs);
QCOMPARE(OneMandatory(2, 2).toString(), u"2.2"_qs);
using FourDigits = Utils::Version<int, 4>;
QCOMPARE(FourDigits().toString(), u"0.0.0.0"_qs);
using FourDigits = Utils::Version<4>;
QCOMPARE(FourDigits(10, 11, 12, 13).toString(), u"10.11.12.13"_qs);
}
void testIsValid() const
{
using ThreeDigits = Utils::Version<int, 3>;
QCOMPARE(ThreeDigits().isValid(), false);
QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
}
void testTryParse() const
void testFromString() const
{
using OneMandatory = Utils::Version<int, 2, 1>;
using OneMandatory = Utils::Version<2, 1>;
const OneMandatory default1 {10, 11};
QCOMPARE(OneMandatory::tryParse(u"1"_qs, default1), OneMandatory(1));
QCOMPARE(OneMandatory::tryParse(u"1.2"_qs, default1), OneMandatory(1, 2));
QCOMPARE(OneMandatory::tryParse(u"1,2"_qs, default1), default1);
QCOMPARE(OneMandatory::tryParse(u""_qs, default1), default1);
QCOMPARE(OneMandatory::tryParse(u"random_string"_qs, default1), default1);
using FourDigits = Utils::Version<int, 4>;
const FourDigits default4 {10, 11, 12, 13};
QCOMPARE(FourDigits::tryParse(u"1"_qs, default4), default4);
QCOMPARE(FourDigits::tryParse(u"1.2.3.4"_qs, default4), FourDigits(1, 2, 3, 4));
QCOMPARE(FourDigits::tryParse(u"1,2.3.4"_qs, default4), default4);
QCOMPARE(OneMandatory::fromString(u"1"_qs, default1), OneMandatory(1));
QCOMPARE(OneMandatory::fromString(u"1.2"_qs, default1), OneMandatory(1, 2));
QCOMPARE(OneMandatory::fromString(u"100.2000"_qs, default1), OneMandatory(100, 2000));
QCOMPARE(OneMandatory::fromString(u"1,2"_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u"1,2"_qs, default1), default1);
QCOMPARE(OneMandatory::fromString(u"1.2a"_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u"1.2.a"_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u""_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u""_qs, default1), default1);
QCOMPARE(OneMandatory::fromString(u"random_string"_qs), OneMandatory());
QCOMPARE(OneMandatory::fromString(u"random_string"_qs, default1), default1);
using FourDigits = Utils::Version<4, 3>;
const FourDigits default2 {10, 11, 12, 13};
QCOMPARE(FourDigits::fromString(u"1"_qs, default2), default2);
QCOMPARE(FourDigits::fromString(u"1.2"_qs), FourDigits());
QCOMPARE(FourDigits::fromString(u"1.2.3"_qs), FourDigits(1, 2, 3));
QCOMPARE(FourDigits::fromString(u"1.2.3.0"_qs), FourDigits(1, 2, 3));
QCOMPARE(FourDigits::fromString(u"1.2.3.4"_qs), FourDigits(1, 2, 3, 4));
}
void testComparisons() const
{
using ThreeDigits = Utils::Version<int, 3>;
using ThreeDigits = Utils::Version<3>;
QVERIFY(ThreeDigits() == ThreeDigits());
QVERIFY(!(ThreeDigits() != ThreeDigits()));

Loading…
Cancel
Save