mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-04 20:43:54 -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
|
|
@ -5,6 +5,7 @@
|
|||
#include "../../settings/cache_settings.h"
|
||||
#include "../game_scene.h"
|
||||
#include "card_database.h"
|
||||
#include "card_database_manager.h"
|
||||
|
||||
#include <QCursor>
|
||||
#include <QGraphicsScene>
|
||||
|
|
@ -49,7 +50,7 @@ void AbstractCardItem::pixmapUpdated()
|
|||
|
||||
void AbstractCardItem::cardInfoUpdated()
|
||||
{
|
||||
info = db->getCard(name);
|
||||
info = CardDatabaseManager::getInstance()->getCard(name);
|
||||
|
||||
if (!info && !name.isEmpty()) {
|
||||
QVariantHash properties = QVariantHash();
|
||||
|
|
|
|||
7
cockatrice/src/game/cards/card_database_manager.cpp
Normal file
7
cockatrice/src/game/cards/card_database_manager.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include "card_database_manager.h"
|
||||
|
||||
CardDatabase *CardDatabaseManager::getInstance()
|
||||
{
|
||||
static CardDatabase instance; // Created only once, on first access
|
||||
return &instance;
|
||||
}
|
||||
23
cockatrice/src/game/cards/card_database_manager.h
Normal file
23
cockatrice/src/game/cards/card_database_manager.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
#ifndef CARD_DATABASE_ACCESSOR_H
|
||||
#define CARD_DATABASE_ACCESSOR_H
|
||||
|
||||
#pragma once
|
||||
#include "card_database.h"
|
||||
|
||||
class CardDatabaseManager
|
||||
{
|
||||
public:
|
||||
// Delete copy constructor and assignment operator to enforce singleton
|
||||
CardDatabaseManager(const CardDatabaseManager &) = delete;
|
||||
CardDatabaseManager &operator=(const CardDatabaseManager &) = delete;
|
||||
|
||||
// Static method to access the singleton instance
|
||||
static CardDatabase *getInstance();
|
||||
|
||||
private:
|
||||
CardDatabaseManager() = default; // Private constructor
|
||||
~CardDatabaseManager() = default;
|
||||
};
|
||||
|
||||
#endif // CARD_DATABASE_ACCESSOR_H
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "../../main.h"
|
||||
#include "../../settings/cache_settings.h"
|
||||
#include "card_database_manager.h"
|
||||
#include "card_info_picture.h"
|
||||
#include "card_info_text.h"
|
||||
#include "card_item.h"
|
||||
|
|
@ -57,7 +58,7 @@ CardFrame::CardFrame(const QString &cardName, QWidget *parent) : QTabWidget(pare
|
|||
|
||||
setViewMode(SettingsCache::instance().getCardInfoViewMode());
|
||||
|
||||
setCard(db->getCard(cardName));
|
||||
setCard(CardDatabaseManager::getInstance()->getCard(cardName));
|
||||
}
|
||||
|
||||
void CardFrame::retranslateUi()
|
||||
|
|
@ -107,7 +108,7 @@ void CardFrame::setCard(CardInfoPtr card)
|
|||
|
||||
void CardFrame::setCard(const QString &cardName)
|
||||
{
|
||||
setCard(db->guessCard(cardName));
|
||||
setCard(CardDatabaseManager::getInstance()->guessCard(cardName));
|
||||
}
|
||||
|
||||
void CardFrame::setCard(AbstractCardItem *card)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "card_info_widget.h"
|
||||
|
||||
#include "../../main.h"
|
||||
#include "card_database_manager.h"
|
||||
#include "card_info_picture.h"
|
||||
#include "card_info_text.h"
|
||||
#include "card_item.h"
|
||||
|
|
@ -56,7 +57,7 @@ void CardInfoWidget::setCard(CardInfoPtr card)
|
|||
|
||||
void CardInfoWidget::setCard(const QString &cardName)
|
||||
{
|
||||
setCard(db->guessCard(cardName));
|
||||
setCard(CardDatabaseManager::getInstance()->guessCard(cardName));
|
||||
if (info == nullptr) {
|
||||
text->setInvalidCardName(cardName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include "../board/arrow_item.h"
|
||||
#include "../board/counter_general.h"
|
||||
#include "../cards/card_database.h"
|
||||
#include "../cards/card_database_manager.h"
|
||||
#include "../cards/card_item.h"
|
||||
#include "../cards/card_list.h"
|
||||
#include "../game_scene.h"
|
||||
|
|
@ -1642,7 +1643,7 @@ void Player::actCreateToken()
|
|||
|
||||
lastTokenName = dlg.getName();
|
||||
lastTokenPT = dlg.getPT();
|
||||
CardInfoPtr correctedCard = db->guessCard(lastTokenName);
|
||||
CardInfoPtr correctedCard = CardDatabaseManager::getInstance()->guessCard(lastTokenName);
|
||||
if (correctedCard) {
|
||||
lastTokenName = correctedCard->getName();
|
||||
lastTokenTableRow = TableZone::clampValidTableRow(2 - correctedCard->getTableRow());
|
||||
|
|
@ -1680,7 +1681,7 @@ void Player::actCreateAnotherToken()
|
|||
void Player::actCreatePredefinedToken()
|
||||
{
|
||||
auto *action = static_cast<QAction *>(sender());
|
||||
CardInfoPtr cardInfo = db->getCard(action->text());
|
||||
CardInfoPtr cardInfo = CardDatabaseManager::getInstance()->getCard(action->text());
|
||||
if (!cardInfo) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -1706,7 +1707,7 @@ void Player::actCreateRelatedCard()
|
|||
* then let's allow it to be created via "create another token"
|
||||
*/
|
||||
if (createRelatedFromRelation(sourceCard, cardRelation) && cardRelation->getCanCreateAnother()) {
|
||||
CardInfoPtr cardInfo = db->getCard(cardRelation->getName());
|
||||
CardInfoPtr cardInfo = CardDatabaseManager::getInstance()->getCard(cardRelation->getName());
|
||||
setLastToken(cardInfo);
|
||||
}
|
||||
}
|
||||
|
|
@ -1786,7 +1787,7 @@ void Player::actCreateAllRelatedCards()
|
|||
* then assign the first to the "Create another" shortcut.
|
||||
*/
|
||||
if (cardRelation != nullptr && cardRelation->getCanCreateAnother()) {
|
||||
CardInfoPtr cardInfo = db->getCard(cardRelation->getName());
|
||||
CardInfoPtr cardInfo = CardDatabaseManager::getInstance()->getCard(cardRelation->getName());
|
||||
setLastToken(cardInfo);
|
||||
}
|
||||
}
|
||||
|
|
@ -1825,7 +1826,7 @@ void Player::createCard(const CardItem *sourceCard,
|
|||
CardRelation::AttachType attachType,
|
||||
bool persistent)
|
||||
{
|
||||
CardInfoPtr cardInfo = db->getCard(dbCardName);
|
||||
CardInfoPtr cardInfo = CardDatabaseManager::getInstance()->getCard(dbCardName);
|
||||
|
||||
if (cardInfo == nullptr || sourceCard == nullptr) {
|
||||
return;
|
||||
|
|
@ -3632,7 +3633,7 @@ void Player::addRelatedCardView(const CardItem *card, QMenu *cardMenu)
|
|||
bool atLeastOneGoodRelationFound = false;
|
||||
QList<CardRelation *> relatedCards = cardInfo->getAllRelatedCards();
|
||||
for (const CardRelation *cardRelation : relatedCards) {
|
||||
CardInfoPtr relatedCard = db->getCard(cardRelation->getName());
|
||||
CardInfoPtr relatedCard = CardDatabaseManager::getInstance()->getCard(cardRelation->getName());
|
||||
if (relatedCard != nullptr) {
|
||||
atLeastOneGoodRelationFound = true;
|
||||
break;
|
||||
|
|
@ -3672,7 +3673,7 @@ void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu)
|
|||
int index = 0;
|
||||
QAction *createRelatedCards = nullptr;
|
||||
for (const CardRelation *cardRelation : relatedCards) {
|
||||
CardInfoPtr relatedCard = db->getCard(cardRelation->getName());
|
||||
CardInfoPtr relatedCard = CardDatabaseManager::getInstance()->getCard(cardRelation->getName());
|
||||
if (relatedCard == nullptr)
|
||||
continue;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue