Refactor CardDatabase *db global variable to singleton CardDatabaseManager. (#5159)

* Refactor CardDatabase *db global variable to singleton CardDatabaseManager.

This commit refactors the global variable CardDatabase *db into a singleton encapsulated by the DatabaseManager class, accessible via DatabaseManager::getInstance(). This change centralizes access to the database instance, improving code modularity and encapsulation, resolving dependencies on main.h for code that requires access to the database instance.

- Added DatabaseManager class with getInstance() method returning a pointer to the singleton CardDatabase.
- Removed global db variable and updated references across the codebase.
 - Thread-safe static initialization for the singleton.

Impact: This refactor should have no functional impact on the application, as it maintains the same interface for accessing the CardDatabase instance. However, the codebase now benefits from improved encapsulation, lifetime management, and thread-safety.

* Refactor CardDatabase *db global variable to singleton CardDatabaseManager.

This commit refactors the global variable CardDatabase *db into a singleton encapsulated by the DatabaseManager class, accessible via DatabaseManager::getInstance(). This change centralizes access to the database instance, improving code modularity and encapsulation, resolving dependencies on main.h for code that requires access to the database instance.

- Added DatabaseManager class with getInstance() method returning a pointer to the singleton CardDatabase.
- Removed global db variable and updated references across the codebase.
 - Thread-safe static initialization for the singleton.

Impact: This refactor should have no functional impact on the application, as it maintains the same interface for accessing the CardDatabase instance. However, the codebase now benefits from improved encapsulation, lifetime management, and thread-safety.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2024-11-05 19:32:59 +01:00 committed by GitHub
parent e43a21866c
commit 5f4ad87a47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 100 additions and 55 deletions

View file

@ -31,6 +31,7 @@
#include "../../dialogs/dlg_update.h"
#include "../../dialogs/dlg_view_log.h"
#include "../../game/cards/card_database.h"
#include "../../game/cards/card_database_manager.h"
#include "../../main.h"
#include "../../server/local_client.h"
#include "../../server/local_server.h"
@ -858,11 +859,12 @@ MainWindow::MainWindow(QWidget *parent)
connect(&SettingsCache::instance().shortcuts(), SIGNAL(shortCutChanged()), this, SLOT(refreshShortcuts()));
refreshShortcuts();
connect(db, SIGNAL(cardDatabaseLoadingFailed()), this, SLOT(cardDatabaseLoadingFailed()));
connect(db, SIGNAL(cardDatabaseNewSetsFound(int, QStringList)), this,
connect(CardDatabaseManager::getInstance(), SIGNAL(cardDatabaseLoadingFailed()), this,
SLOT(cardDatabaseLoadingFailed()));
connect(CardDatabaseManager::getInstance(), SIGNAL(cardDatabaseNewSetsFound(int, QStringList)), this,
SLOT(cardDatabaseNewSetsFound(int, QStringList)));
connect(db, SIGNAL(cardDatabaseAllNewSetsEnabled()), this, SLOT(cardDatabaseAllNewSetsEnabled()));
connect(CardDatabaseManager::getInstance(), SIGNAL(cardDatabaseAllNewSetsEnabled()), this,
SLOT(cardDatabaseAllNewSetsEnabled()));
tip = new DlgTipOfTheDay();
@ -884,13 +886,13 @@ void MainWindow::startupConfigCheck()
if (SettingsCache::instance().getNotifyAboutNewVersion()) {
alertForcedOracleRun(VERSION_STRING, true);
} else {
const auto reloadOk0 = QtConcurrent::run([] { db->loadCardDatabases(); });
const auto reloadOk0 = QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); });
}
SettingsCache::instance().setClientVersion(VERSION_STRING);
} else {
// previous config from this version found
qDebug() << "Startup: found config with current version";
const auto reloadOk1 = QtConcurrent::run([] { db->loadCardDatabases(); });
const auto reloadOk1 = QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); });
// Run the tips dialog only on subsequent startups.
// On the first run after an install/update the startup is already crowded enough
@ -1075,12 +1077,12 @@ void MainWindow::cardDatabaseNewSetsFound(int numUnknownSets, QStringList unknow
msgBox.exec();
if (msgBox.clickedButton() == yesButton) {
db->enableAllUnknownSets();
const auto reloadOk1 = QtConcurrent::run([] { db->loadCardDatabases(); });
CardDatabaseManager::getInstance()->enableAllUnknownSets();
const auto reloadOk1 = QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); });
} else if (msgBox.clickedButton() == noButton) {
db->markAllSetsAsKnown();
CardDatabaseManager::getInstance()->markAllSetsAsKnown();
} else if (msgBox.clickedButton() == settingsButton) {
db->markAllSetsAsKnown();
CardDatabaseManager::getInstance()->markAllSetsAsKnown();
actManageSets();
}
}
@ -1167,7 +1169,7 @@ void MainWindow::exitCardDatabaseUpdate()
cardUpdateProcess->deleteLater();
cardUpdateProcess = nullptr;
const auto reloadOk1 = QtConcurrent::run([] { db->loadCardDatabases(); });
const auto reloadOk1 = QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); });
}
void MainWindow::cardUpdateError(QProcess::ProcessError err)
@ -1297,7 +1299,7 @@ void MainWindow::actAddCustomSet()
QMessageBox::information(
this, tr("Load sets/cards"),
tr("The new sets/cards have been added successfully.\nCockatrice will now reload the card database."));
const auto reloadOk1 = QtConcurrent::run([] { db->loadCardDatabases(); });
const auto reloadOk1 = QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); });
} else {
QMessageBox::warning(this, tr("Load sets/cards"), tr("Sets/cards failed to import."));
}
@ -1328,7 +1330,7 @@ void MainWindow::actEditTokens()
{
DlgEditTokens dlg(this);
dlg.exec();
db->saveCustomTokensToFile();
CardDatabaseManager::getInstance()->saveCustomTokensToFile();
}
void MainWindow::actForgotPasswordRequest()