Replaced QSet with QHash

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
(cherry picked from commit 73d33f93b30f658f9671358ac52bf4e03afeaefd)
This commit is contained in:
Trial97 2024-10-22 09:41:00 +03:00 committed by github-actions[bot]
parent 51a71d0471
commit 85422427b9
3 changed files with 24 additions and 19 deletions

View File

@ -1887,20 +1887,25 @@ const QString Application::javaPath()
void Application::addQSavePath(QString path)
{
QMutexLocker locker(&m_qsaveResourcesMutex);
m_qsaveResources.insert(path);
m_qsaveResources[path] = m_qsaveResources.value(path, 0) + 1;
}
void Application::removeQSavePath(QString path)
{
QMutexLocker locker(&m_qsaveResourcesMutex);
auto count = m_qsaveResources.value(path, 0) - 1;
if (count <= 0) {
m_qsaveResources.remove(path);
} else {
m_qsaveResources[path] = count;
}
}
bool Application::checkQSavePath(QString path)
{
QMutexLocker locker(&m_qsaveResourcesMutex);
for (auto r : m_qsaveResources) {
if (path.contains(r)) {
for (auto partialPath : m_qsaveResources.keys()) {
if (path.startsWith(partialPath) && m_qsaveResources.value(partialPath, 0) > 0) {
return true;
}
}

View File

@ -43,7 +43,6 @@
#include <QFlag>
#include <QIcon>
#include <QMutex>
#include <QSet>
#include <QUrl>
#include <memory>
@ -312,6 +311,6 @@ class Application : public QApplication {
bool checkQSavePath(QString);
private:
QSet<QString> m_qsaveResources;
QHash<QString, int> m_qsaveResources;
mutable QMutex m_qsaveResourcesMutex;
};

View File

@ -17,6 +17,7 @@
*/
#pragma once
#include <QFileInfo>
#include <QSaveFile>
#include "Application.h"
@ -46,24 +47,24 @@
*/
class PSaveFile : public QSaveFile {
public:
PSaveFile(const QString& name) : QSaveFile(name)
{
if (auto app = APPLICATION_DYN) {
app->addQSavePath(name + ".");
}
}
PSaveFile(const QString& name, QObject* parent) : QSaveFile(name, parent)
{
if (auto app = APPLICATION_DYN) {
app->addQSavePath(name + ".");
}
}
PSaveFile(const QString& name) : QSaveFile(name) { addPath(name); }
PSaveFile(const QString& name, QObject* parent) : QSaveFile(name, parent) { addPath(name); }
virtual ~PSaveFile()
{
if (auto app = APPLICATION_DYN) {
app->removeQSavePath(fileName() + ".");
app->removeQSavePath(m_absoluteFilePath);
}
}
private:
void addPath(const QString& path)
{
m_absoluteFilePath = QFileInfo(path).absoluteFilePath() + "."; // add dot for tmp files only
if (auto app = APPLICATION_DYN) {
app->addQSavePath(m_absoluteFilePath);
}
}
QString m_absoluteFilePath;
};
#else
#define PSaveFile QSaveFile