mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
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:
parent
e43a21866c
commit
5f4ad87a47
23 changed files with 100 additions and 55 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include "deck_loader.h"
|
||||
|
||||
#include "../game/cards/card_database.h"
|
||||
#include "../game/cards/card_database_manager.h"
|
||||
#include "../main.h"
|
||||
#include "decklist.h"
|
||||
|
||||
|
|
@ -124,7 +125,7 @@ struct FormatDeckListForExport
|
|||
void operator()(const InnerDecklistNode *node, const DecklistCardNode *card) const
|
||||
{
|
||||
// Get the card name
|
||||
CardInfoPtr dbCard = db->getCard(card->getName());
|
||||
CardInfoPtr dbCard = CardDatabaseManager::getInstance()->getCard(card->getName());
|
||||
if (!dbCard || dbCard->getIsToken()) {
|
||||
// If it's a token, we don't care about the card.
|
||||
return;
|
||||
|
|
@ -227,7 +228,7 @@ void DeckLoader::saveToStream_DeckZone(QTextStream &out, const InnerDecklistNode
|
|||
for (int j = 0; j < zoneNode->size(); j++) {
|
||||
auto *card = dynamic_cast<DecklistCardNode *>(zoneNode->at(j));
|
||||
|
||||
CardInfoPtr info = db->getCard(card->getName());
|
||||
CardInfoPtr info = CardDatabaseManager::getInstance()->getCard(card->getName());
|
||||
QString cardType = info ? info->getMainCardType() : "unknown";
|
||||
|
||||
cardsByType.insert(cardType, card);
|
||||
|
|
@ -280,7 +281,7 @@ void DeckLoader::saveToStream_DeckZoneCards(QTextStream &out,
|
|||
|
||||
QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneName)
|
||||
{
|
||||
CardInfoPtr card = db->getCard(cardName);
|
||||
CardInfoPtr card = CardDatabaseManager::getInstance()->getCard(cardName);
|
||||
|
||||
if (card && card->getIsToken()) {
|
||||
return DECK_ZONE_TOKENS;
|
||||
|
|
@ -291,8 +292,8 @@ QString DeckLoader::getCardZoneFromName(QString cardName, QString currentZoneNam
|
|||
|
||||
QString DeckLoader::getCompleteCardName(const QString &cardName) const
|
||||
{
|
||||
if (db) {
|
||||
CardInfoPtr temp = db->guessCard(cardName);
|
||||
if (CardDatabaseManager::getInstance()) {
|
||||
CardInfoPtr temp = CardDatabaseManager::getInstance()->guessCard(cardName);
|
||||
if (temp) {
|
||||
return temp->getName();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue