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:
wrenczes@gmail.com
2011-01-28 10:21:23 +00:00
parent f4f1fdcd3d
commit 3187487987
3 changed files with 32 additions and 31 deletions
+31 -28
View File
@@ -24,30 +24,35 @@ vector<DeckMetaData *> * DeckManager::getAIDeckOrderList()
return &aiDeckOrderList; return &aiDeckOrderList;
} }
DeckMetaData * DeckManager::getDeckMetaDataById( int deckId, bool isAI ) /*
** Predicate helper for getDeckMetadataByID()
*/
struct DeckIDMatch
{ {
DeckIDMatch(int id) : mID(id)
DeckMetaData* currentDeck = NULL;
vector<DeckMetaData *>::iterator currentPos, end;
if ( isAI )
{ {
currentPos = aiDeckOrderList.begin();
end = aiDeckOrderList.end();
}
else
{
currentPos = playerDeckOrderList.begin();
end = playerDeckOrderList.end();
} }
bool operator() (DeckMetaData* inPtr)
for (; currentPos != end; ++currentPos)
{ {
currentDeck = *currentPos; return inPtr->getDeckId() == mID;
if (currentDeck->getDeckId() == deckId )
return currentDeck;
} }
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 ) 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 * DeckManager::getExtendedDeckStats( DeckMetaData *selectedDeck, MTGAllCards *collection, bool isAI )
{ {
StatsWrapper * stats = NULL; StatsWrapper* stats = NULL;
map<string, StatsWrapper *> *statsMap = NULL;
string deckName = selectedDeck->getFilename(); string deckName = selectedDeck->getFilename();
int deckId = selectedDeck->getDeckId(); int deckId = selectedDeck->getDeckId();
if (isAI) map<string, StatsWrapper*>* statsMap = isAI ? &aiDeckStatsMap : &playerDeckStatsMap;
statsMap = &aiDeckStatsMap; if (statsMap->find(deckName) == statsMap->end())
else
statsMap = &playerDeckStatsMap;
if (statsMap->empty() || (statsMap->find(deckName) == statsMap->end()))
{ {
stats = NEW StatsWrapper(deckId); stats = NEW StatsWrapper(deckId);
stats->updateStats( deckName, collection); 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; return stats;
} }
-1
View File
@@ -71,7 +71,6 @@ GameStateDeckViewer::~GameStateDeckViewer()
SAFE_DELETE(myCollection->parent); SAFE_DELETE(myCollection->parent);
SAFE_DELETE(myCollection); SAFE_DELETE(myCollection);
} }
SAFE_DELETE(stw);
SAFE_DELETE(filterMenu); SAFE_DELETE(filterMenu);
} }
+1 -2
View File
@@ -78,8 +78,7 @@ void SimplePopup::Update(DeckMetaData* selectedDeck)
mDeckInformation = selectedDeck; mDeckInformation = selectedDeck;
// get the information from the cache, if it doesn't exist create an entry // get the information from the cache, if it doesn't exist create an entry
DeckManager *deckManager = DeckManager::GetInstance(); mStatsWrapper = DeckManager::GetInstance()->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
mStatsWrapper = deckManager->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
} }