Replace CardInfo* and Carset* with smart pointers (#3053)

* Replace CardInfo* and Carset* with smart pointers

* fixes to help memory & c++11 stuff
This commit is contained in:
ctrlaltca 2018-02-02 18:52:47 +01:00 committed by Zach H
parent 3e418ba3c6
commit 517420cccb
37 changed files with 409 additions and 342 deletions

View file

@ -20,6 +20,7 @@
#include <QSvgRenderer>
#include <QThread>
#include <QUrl>
#include <utility>
// never cache more than 300 cards at once for a single deck
#define CACHED_CARD_PER_DECK_MAX 300
@ -32,29 +33,17 @@ public:
* Enabled sets have priority over disabled sets
* Both groups follows the user-defined order
*/
inline bool operator()(CardSet *a, CardSet *b) const
inline bool operator()(const CardSetPtr &a, const CardSetPtr &b) const
{
if (a->getEnabled()) {
if (b->getEnabled()) {
// both enabled: sort by key
return a->getSortKey() < b->getSortKey();
} else {
// only a enabled
return true;
}
return !b->getEnabled() || a->getSortKey() < b->getSortKey();
} else {
if (b->getEnabled()) {
// only b enabled
return false;
} else {
// both disabled: sort by key
return a->getSortKey() < b->getSortKey();
}
return !b->getEnabled() && a->getSortKey() < b->getSortKey();
}
}
};
PictureToLoad::PictureToLoad(CardInfo *_card) : card(_card), setIndex(0)
PictureToLoad::PictureToLoad(CardInfoPtr _card) : card(std::move(_card)), setIndex(0)
{
if (card) {
sortedSets = card->getSets();
@ -78,18 +67,18 @@ QString PictureToLoad::getSetName() const
return QString("");
}
CardSet *PictureToLoad::getCurrentSet() const
CardSetPtr PictureToLoad::getCurrentSet() const
{
if (setIndex < sortedSets.size())
return sortedSets[setIndex];
else
return 0;
return {};
}
QStringList PictureLoaderWorker::md5Blacklist =
QStringList() << "db0c48db407a907c16ade38de048a441"; // card back returned by gatherer when card is not found
PictureLoaderWorker::PictureLoaderWorker() : QObject(0), downloadRunning(false), loadQueueRunning(false)
PictureLoaderWorker::PictureLoaderWorker() : QObject(nullptr), downloadRunning(false), loadQueueRunning(false)
{
picsPath = settingsCache->getPicsPath();
customPicsPath = settingsCache->getCustomPicsPath();
@ -140,7 +129,7 @@ void PictureLoaderWorker::processLoadQueue()
if (picDownload) {
qDebug() << "Picture NOT found, trying to download (set: " << setName << " card: " << cardName << ")";
cardsToDownload.append(cardBeingLoaded);
cardBeingLoaded = 0;
cardBeingLoaded.clear();
if (!downloadRunning)
startNextPicDownload();
} else {
@ -149,7 +138,7 @@ void PictureLoaderWorker::processLoadQueue()
<< " card: " << cardName << ")";
mutex.lock();
loadQueue.prepend(cardBeingLoaded);
cardBeingLoaded = 0;
cardBeingLoaded.clear();
mutex.unlock();
} else {
qDebug() << "Picture NOT found, download disabled, no more sets to try: BAILING OUT (oldset: "
@ -213,8 +202,8 @@ QString PictureLoaderWorker::getPicUrl()
if (!picDownload)
return QString();
CardInfo *card = cardBeingDownloaded.getCard();
CardSet *set = cardBeingDownloaded.getCurrentSet();
CardInfoPtr card = cardBeingDownloaded.getCard();
CardSetPtr set = cardBeingDownloaded.getCurrentSet();
QString picUrl = QString("");
// if sets have been defined for the card, they can contain custom picUrls
@ -258,7 +247,7 @@ QString PictureLoaderWorker::getPicUrl()
void PictureLoaderWorker::startNextPicDownload()
{
if (cardsToDownload.isEmpty()) {
cardBeingDownloaded = 0;
cardBeingDownloaded.clear();
downloadRunning = false;
return;
}
@ -292,7 +281,7 @@ void PictureLoaderWorker::picDownloadFailed()
qDebug() << "Picture NOT found, download failed, no more sets to try: BAILING OUT (oldset: "
<< cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getName() << ")";
imageLoaded(cardBeingDownloaded.getCard(), QImage());
cardBeingDownloaded = 0;
cardBeingDownloaded.clear();
}
emit startLoadQueue();
}
@ -364,7 +353,7 @@ void PictureLoaderWorker::picDownloadFinished(QNetworkReply *reply)
startNextPicDownload();
}
void PictureLoaderWorker::enqueueImageLoad(CardInfo *card)
void PictureLoaderWorker::enqueueImageLoad(CardInfoPtr card)
{
QMutexLocker locker(&mutex);
@ -405,8 +394,8 @@ PictureLoader::PictureLoader() : QObject(0)
connect(settingsCache, SIGNAL(picsPathChanged()), this, SLOT(picsPathChanged()));
connect(settingsCache, SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
connect(worker, SIGNAL(imageLoaded(CardInfo *, const QImage &)), this,
SLOT(imageLoaded(CardInfo *, const QImage &)));
connect(worker, SIGNAL(imageLoaded(CardInfoPtr, const QImage &)), this,
SLOT(imageLoaded(CardInfoPtr, const QImage &)));
}
PictureLoader::~PictureLoader()
@ -424,7 +413,7 @@ void PictureLoader::getCardBackPixmap(QPixmap &pixmap, QSize size)
}
}
void PictureLoader::getPixmap(QPixmap &pixmap, CardInfo *card, QSize size)
void PictureLoader::getPixmap(QPixmap &pixmap, CardInfoPtr card, QSize size)
{
if (card == nullptr)
return;
@ -447,7 +436,7 @@ void PictureLoader::getPixmap(QPixmap &pixmap, CardInfo *card, QSize size)
getInstance().worker->enqueueImageLoad(card);
}
void PictureLoader::imageLoaded(CardInfo *card, const QImage &image)
void PictureLoader::imageLoaded(CardInfoPtr card, const QImage &image)
{
if (image.isNull()) {
QPixmapCache::insert(card->getPixmapCacheKey(), QPixmap());
@ -463,7 +452,7 @@ void PictureLoader::imageLoaded(CardInfo *card, const QImage &image)
card->emitPixmapUpdated();
}
void PictureLoader::clearPixmapCache(CardInfo *card)
void PictureLoader::clearPixmapCache(CardInfoPtr card)
{
if (card)
QPixmapCache::remove(card->getPixmapCacheKey());
@ -474,12 +463,12 @@ void PictureLoader::clearPixmapCache()
QPixmapCache::clear();
}
void PictureLoader::cacheCardPixmaps(QList<CardInfo *> cards)
void PictureLoader::cacheCardPixmaps(QList<CardInfoPtr> cards)
{
QPixmap tmp;
int max = qMin(cards.size(), CACHED_CARD_PER_DECK_MAX);
for (int i = 0; i < max; ++i) {
CardInfo *card = cards.at(i);
CardInfoPtr card = cards.at(i);
if (!card)
continue;