Minor tweaks to Mike's map of maps cache work on stats. Mostly these changes are stylistic changes that favor more terse code, but there was one bug where a double-delete was happening - GameStateDeckViewer used to allocate its own StatsWrapper, and deleted it as part of teardown - now that the allocation is owned by the deck stats instance, it's not allowed to do the deletion anymore.
I'll also note that the mods I did to getDeckMetaDataById() are completely unnecessary - pragmatically speaking, it's doing the same thing. The only difference is that I'm using std::find_if instead of brute iterator manipulation, and I'm using a predicate function. For a simple check like this, it's kind of pointless, but if you need to do more complex comparisons, predicate operators can become quite powerful.
This commit is contained in:
@@ -24,30 +24,35 @@ vector<DeckMetaData *> * DeckManager::getAIDeckOrderList()
|
||||
return &aiDeckOrderList;
|
||||
}
|
||||
|
||||
DeckMetaData * DeckManager::getDeckMetaDataById( int deckId, bool isAI )
|
||||
/*
|
||||
** Predicate helper for getDeckMetadataByID()
|
||||
*/
|
||||
struct DeckIDMatch
|
||||
{
|
||||
|
||||
DeckMetaData* currentDeck = NULL;
|
||||
vector<DeckMetaData *>::iterator currentPos, end;
|
||||
if ( isAI )
|
||||
DeckIDMatch(int id) : mID(id)
|
||||
{
|
||||
currentPos = aiDeckOrderList.begin();
|
||||
end = aiDeckOrderList.end();
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPos = playerDeckOrderList.begin();
|
||||
end = playerDeckOrderList.end();
|
||||
}
|
||||
|
||||
|
||||
for (; currentPos != end; ++currentPos)
|
||||
bool operator() (DeckMetaData* inPtr)
|
||||
{
|
||||
currentDeck = *currentPos;
|
||||
if (currentDeck->getDeckId() == deckId )
|
||||
return currentDeck;
|
||||
return inPtr->getDeckId() == mID;
|
||||
}
|
||||
return NULL;
|
||||
|
||||
int mID;
|
||||
};
|
||||
|
||||
DeckMetaData* DeckManager::getDeckMetaDataById( int deckId, bool isAI )
|
||||
{
|
||||
DeckMetaData* deck = NULL;
|
||||
std::vector<DeckMetaData *>& deckList = isAI ? aiDeckOrderList : playerDeckOrderList;
|
||||
|
||||
std::vector<DeckMetaData *>::iterator pos = find_if(deckList.begin(), deckList.end(), DeckIDMatch(deckId));
|
||||
if (pos != deckList.end())
|
||||
{
|
||||
deck = *pos;
|
||||
}
|
||||
|
||||
return deck;
|
||||
}
|
||||
|
||||
StatsWrapper * DeckManager::getExtendedStatsForDeckId( int deckId, MTGAllCards *collection, bool isAI )
|
||||
@@ -59,24 +64,22 @@ StatsWrapper * DeckManager::getExtendedStatsForDeckId( int deckId, MTGAllCards *
|
||||
|
||||
StatsWrapper * DeckManager::getExtendedDeckStats( DeckMetaData *selectedDeck, MTGAllCards *collection, bool isAI )
|
||||
{
|
||||
StatsWrapper * stats = NULL;
|
||||
map<string, StatsWrapper *> *statsMap = NULL;
|
||||
StatsWrapper* stats = NULL;
|
||||
|
||||
string deckName = selectedDeck->getFilename();
|
||||
int deckId = selectedDeck->getDeckId();
|
||||
|
||||
if (isAI)
|
||||
statsMap = &aiDeckStatsMap;
|
||||
else
|
||||
statsMap = &playerDeckStatsMap;
|
||||
|
||||
if (statsMap->empty() || (statsMap->find(deckName) == statsMap->end()))
|
||||
map<string, StatsWrapper*>* statsMap = isAI ? &aiDeckStatsMap : &playerDeckStatsMap;
|
||||
if (statsMap->find(deckName) == statsMap->end())
|
||||
{
|
||||
stats = NEW StatsWrapper(deckId);
|
||||
stats->updateStats( deckName, collection);
|
||||
statsMap->insert( make_pair( deckName, stats));
|
||||
statsMap->insert( make_pair(deckName, stats));
|
||||
}
|
||||
else
|
||||
{
|
||||
stats = statsMap->find(deckName)->second;
|
||||
}
|
||||
stats = statsMap->find(deckName) ->second;
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,6 @@ GameStateDeckViewer::~GameStateDeckViewer()
|
||||
SAFE_DELETE(myCollection->parent);
|
||||
SAFE_DELETE(myCollection);
|
||||
}
|
||||
SAFE_DELETE(stw);
|
||||
SAFE_DELETE(filterMenu);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +78,7 @@ void SimplePopup::Update(DeckMetaData* selectedDeck)
|
||||
mDeckInformation = selectedDeck;
|
||||
|
||||
// get the information from the cache, if it doesn't exist create an entry
|
||||
DeckManager *deckManager = DeckManager::GetInstance();
|
||||
mStatsWrapper = deckManager->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
|
||||
mStatsWrapper = DeckManager::GetInstance()->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user