mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-24 13:34:27 +00:00
Merge pull request #10627 from Chocobo1/alg
Improve removeIf() to support other types
This commit is contained in:
commit
3d6dccc689
@ -28,14 +28,42 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace Dict
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace Algorithm
|
||||||
{
|
{
|
||||||
// To be used with QMap, QHash and it's variants
|
template <typename ...>
|
||||||
template <typename Dictionary, typename BinaryPredicate>
|
using void_t = void; // replace this with std::void_t in C++17
|
||||||
void removeIf(Dictionary &&dict, BinaryPredicate p)
|
|
||||||
|
template <typename T, typename = void>
|
||||||
|
struct HasMappedType
|
||||||
|
: std::false_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct HasMappedType<T, void_t<typename T::mapped_type>>
|
||||||
|
: std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
// To be used with associative array types, such as QMap, QHash and it's variants
|
||||||
|
template <typename T, typename BinaryPredicate
|
||||||
|
, typename std::enable_if_t<HasMappedType<T>::value, int> = 0>
|
||||||
|
void removeIf(T &dict, BinaryPredicate p)
|
||||||
{
|
{
|
||||||
auto it = dict.begin();
|
auto it = dict.begin();
|
||||||
while (it != dict.end())
|
while (it != dict.end())
|
||||||
it = (p(it.key(), it.value()) ? dict.erase(it) : it + 1);
|
it = (p(it.key(), it.value()) ? dict.erase(it) : (it + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// To be used with set types, such as QSet, std::set
|
||||||
|
template <typename T, typename UnaryPredicate
|
||||||
|
, typename std::enable_if_t<!HasMappedType<T>::value, int> = 0>
|
||||||
|
void removeIf(T &set, UnaryPredicate p)
|
||||||
|
{
|
||||||
|
auto it = set.begin();
|
||||||
|
while (it != set.end())
|
||||||
|
it = (p(*it) ? set.erase(it) : (it + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -754,7 +754,7 @@ bool Session::removeCategory(const QString &name)
|
|||||||
if (isSubcategoriesEnabled()) {
|
if (isSubcategoriesEnabled()) {
|
||||||
// remove subcategories
|
// remove subcategories
|
||||||
const QString test = name + '/';
|
const QString test = name + '/';
|
||||||
Dict::removeIf(m_categories, [this, &test, &result](const QString &category, const QString &)
|
Algorithm::removeIf(m_categories, [this, &test, &result](const QString &category, const QString &)
|
||||||
{
|
{
|
||||||
if (category.startsWith(test)) {
|
if (category.startsWith(test)) {
|
||||||
result = true;
|
result = true;
|
||||||
|
@ -122,7 +122,7 @@ void FileSystemWatcher::processPartialTorrents()
|
|||||||
QStringList noLongerPartial;
|
QStringList noLongerPartial;
|
||||||
|
|
||||||
// Check which torrents are still partial
|
// Check which torrents are still partial
|
||||||
Dict::removeIf(m_partialTorrents, [&noLongerPartial](const QString &torrentPath, int &value)
|
Algorithm::removeIf(m_partialTorrents, [&noLongerPartial](const QString &torrentPath, int &value)
|
||||||
{
|
{
|
||||||
if (!QFile::exists(torrentPath))
|
if (!QFile::exists(torrentPath))
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include <QMutableListIterator>
|
|
||||||
#include <QNetworkProxy>
|
#include <QNetworkProxy>
|
||||||
#include <QSslCipher>
|
#include <QSslCipher>
|
||||||
#include <QSslConfiguration>
|
#include <QSslConfiguration>
|
||||||
@ -40,6 +39,7 @@
|
|||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include "base/algorithm.h"
|
||||||
#include "base/utils/net.h"
|
#include "base/utils/net.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
|
||||||
@ -119,14 +119,14 @@ void Server::removeConnection(Connection *connection)
|
|||||||
|
|
||||||
void Server::dropTimedOutConnection()
|
void Server::dropTimedOutConnection()
|
||||||
{
|
{
|
||||||
QMutableSetIterator<Connection *> i(m_connections);
|
Algorithm::removeIf(m_connections, [](Connection *connection)
|
||||||
while (i.hasNext()) {
|
{
|
||||||
Connection *connection = i.next();
|
if (!connection->hasExpired(KEEP_ALIVE_DURATION))
|
||||||
if (connection->hasExpired(KEEP_ALIVE_DURATION)) {
|
return false;
|
||||||
connection->deleteLater();
|
|
||||||
i.remove();
|
connection->deleteLater();
|
||||||
}
|
return true;
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Server::setupHttps(const QByteArray &certificates, const QByteArray &privateKey)
|
bool Server::setupHttps(const QByteArray &certificates, const QByteArray &privateKey)
|
||||||
|
@ -31,7 +31,6 @@
|
|||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
#include <QMutableListIterator>
|
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
#ifndef DISABLE_GUI
|
#ifndef DISABLE_GUI
|
||||||
@ -49,6 +48,7 @@
|
|||||||
#include <CoreServices/CoreServices.h>
|
#include <CoreServices/CoreServices.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "algorithm.h"
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "settingsstorage.h"
|
#include "settingsstorage.h"
|
||||||
#include "utils/fs.h"
|
#include "utils/fs.h"
|
||||||
@ -516,13 +516,12 @@ QList<Utils::Net::Subnet> Preferences::getWebUiAuthSubnetWhitelist() const
|
|||||||
|
|
||||||
void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets)
|
void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets)
|
||||||
{
|
{
|
||||||
QMutableListIterator<QString> i(subnets);
|
Algorithm::removeIf(subnets, [](const QString &subnet)
|
||||||
while (i.hasNext()) {
|
{
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
const Utils::Net::Subnet subnet = Utils::Net::parseSubnet(i.next().trimmed(), &ok);
|
Utils::Net::parseSubnet(subnet.trimmed(), &ok);
|
||||||
if (!ok)
|
return !ok;
|
||||||
i.remove();
|
});
|
||||||
}
|
|
||||||
|
|
||||||
setValue("Preferences/WebUI/AuthSubnetWhitelist", subnets);
|
setValue("Preferences/WebUI/AuthSubnetWhitelist", subnets);
|
||||||
}
|
}
|
||||||
|
@ -364,9 +364,7 @@ void PeerListWidget::loadPeers(BitTorrent::TorrentHandle *const torrent, bool fo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Delete peers that are gone
|
// Delete peers that are gone
|
||||||
QSetIterator<QString> it(oldPeersSet);
|
for (const QString &ip : oldPeersSet) {
|
||||||
while (it.hasNext()) {
|
|
||||||
const QString &ip = it.next();
|
|
||||||
m_missingFlags.remove(ip);
|
m_missingFlags.remove(ip);
|
||||||
m_peerAddresses.remove(ip);
|
m_peerAddresses.remove(ip);
|
||||||
QStandardItem *item = m_peerItems.take(ip);
|
QStandardItem *item = m_peerItems.take(ip);
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
|
#include "base/algorithm.h"
|
||||||
#include "base/global.h"
|
#include "base/global.h"
|
||||||
#include "base/http/httperror.h"
|
#include "base/http/httperror.h"
|
||||||
#include "base/iconprovider.h"
|
#include "base/iconprovider.h"
|
||||||
@ -527,11 +528,14 @@ void WebApplication::sessionStart()
|
|||||||
|
|
||||||
// remove outdated sessions
|
// remove outdated sessions
|
||||||
const qint64 now = QDateTime::currentMSecsSinceEpoch() / 1000;
|
const qint64 now = QDateTime::currentMSecsSinceEpoch() / 1000;
|
||||||
const QHash<QString, WebSession *> sessionsCopy {m_sessions};
|
Algorithm::removeIf(m_sessions, [now](const QString &, const WebSession *session)
|
||||||
for (const auto session : sessionsCopy) {
|
{
|
||||||
if ((now - session->timestamp()) > INACTIVE_TIME)
|
if ((now - session->timestamp()) <= INACTIVE_TIME)
|
||||||
delete m_sessions.take(session->id());
|
return false;
|
||||||
}
|
|
||||||
|
delete session;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
m_currentSession = new WebSession(generateSid());
|
m_currentSession = new WebSession(generateSid());
|
||||||
m_sessions[m_currentSession->id()] = m_currentSession;
|
m_sessions[m_currentSession->id()] = m_currentSession;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user