Minor code cleanup - pass by reference instead of value; instead of creating temp char arrays on each deck loop while building deck metadata, use an ostringstream when needed.

This commit is contained in:
wrenczes@gmail.com
2010-11-07 03:55:56 +00:00
parent 416617fc0d
commit b14e3808db
4 changed files with 38 additions and 46 deletions
+5 -5
View File
@@ -66,19 +66,19 @@ class GameState
// it makes it easier to manipulate the deck information menus. // it makes it easier to manipulate the deck information menus.
// generate the Deck Meta Data and build the menu items of the menu given // generate the Deck Meta Data and build the menu items of the menu given
static vector<DeckMetaData *> fillDeckMenu(SimpleMenu * _menu, string path, string smallDeckPrefix = "", Player * statsPlayer = NULL); static vector<DeckMetaData *> fillDeckMenu(SimpleMenu * _menu, const string& path, const string& smallDeckPrefix = "", Player * statsPlayer = NULL);
// generate the Deck Meta Data and build the menu items of the menu given // generate the Deck Meta Data and build the menu items of the menu given
static vector<DeckMetaData *> fillDeckMenu(DeckMenu * _menu, string path, string smallDeckPrefix = "", Player * statsPlayer = NULL); static vector<DeckMetaData *> fillDeckMenu(DeckMenu * _menu, const string& path, const string& smallDeckPrefix = "", Player * statsPlayer = NULL);
// build a vector of decks with the information passsed in. // build a vector of decks with the information passsed in.
static vector<DeckMetaData *> getValidDeckMetaData(string path, string smallDeckPrefix = "", Player * statsPlayer = NULL); static vector<DeckMetaData *> getValidDeckMetaData(const string& path, const string& smallDeckPrefix = "", Player * statsPlayer = NULL);
// build menu items based on the vector<DeckMetaData *> // build menu items based on the vector<DeckMetaData *>
static void renderDeckMenu(SimpleMenu * _menu, vector<DeckMetaData *> deckMetaDataList); static void renderDeckMenu(SimpleMenu * _menu, const vector<DeckMetaData *>& deckMetaDataList);
// build menu items based on the vector<DeckMetaData *> // build menu items based on the vector<DeckMetaData *>
static void renderDeckMenu(DeckMenu * _menu, vector<DeckMetaData *> deckMetaDataList); static void renderDeckMenu(DeckMenu * _menu, const vector<DeckMetaData *>& deckMetaDataList);
}; };
bool sortByName( DeckMetaData * d1, DeckMetaData * d2 ); bool sortByName( DeckMetaData * d1, DeckMetaData * d2 );
+2 -5
View File
@@ -211,19 +211,17 @@ void DeckMenu::Add(int id, const char * text,string desc, bool forceFocus, DeckM
} }
} }
void DeckMenu::updateScroller() void DeckMenu::updateScroller()
{ {
// add all the items from the Tasks db. // add all the items from the Tasks db.
TaskList *taskList = NEW TaskList(); TaskList taskList;
scroller->Reset(); scroller->Reset();
for (vector<Task*>::iterator it = taskList->tasks.begin(); it!=taskList->tasks.end(); it++) for (vector<Task*>::iterator it = taskList.tasks.begin(); it!=taskList.tasks.end(); it++)
{ {
ostringstream taskDescription; ostringstream taskDescription;
taskDescription << "[ " << setw(4) << (*it)->getReward() << " / " << (*it)->getExpiration() << " ] " << (*it)->getDesc() << endl; taskDescription << "[ " << setw(4) << (*it)->getReward() << " / " << (*it)->getExpiration() << " ] " << (*it)->getDesc() << endl;
scroller->Add( taskDescription.str() ); scroller->Add( taskDescription.str() );
} }
SAFE_DELETE(taskList);
} }
@@ -241,5 +239,4 @@ void DeckMenu::destroy(){
DeckMenu::~DeckMenu() DeckMenu::~DeckMenu()
{ {
SAFE_DELETE(scroller); SAFE_DELETE(scroller);
} }
+16 -21
View File
@@ -13,7 +13,7 @@
// TODO: revise sorting strategy to allow other types of sorting. Currently, it is hardwired to use // TODO: revise sorting strategy to allow other types of sorting. Currently, it is hardwired to use
// sortByName to do the sorting. This was done since the menu item display is done in insertion order. // sortByName to do the sorting. This was done since the menu item display is done in insertion order.
vector<DeckMetaData *> GameState::fillDeckMenu( SimpleMenu * _menu, string path, string smallDeckPrefix, Player * statsPlayer){ vector<DeckMetaData *> GameState::fillDeckMenu( SimpleMenu * _menu, const string& path, const string& smallDeckPrefix, Player * statsPlayer){
bool translate = _menu->autoTranslate; bool translate = _menu->autoTranslate;
_menu->autoTranslate = false; _menu->autoTranslate = false;
vector<DeckMetaData *> deckMetaDataVector = getValidDeckMetaData( path, smallDeckPrefix, statsPlayer ); vector<DeckMetaData *> deckMetaDataVector = getValidDeckMetaData( path, smallDeckPrefix, statsPlayer );
@@ -23,7 +23,7 @@ vector<DeckMetaData *> GameState::fillDeckMenu( SimpleMenu * _menu, string path,
return deckMetaDataVector; return deckMetaDataVector;
} }
vector<DeckMetaData *> GameState::fillDeckMenu( DeckMenu * _menu, string path, string smallDeckPrefix, Player * statsPlayer){ vector<DeckMetaData *> GameState::fillDeckMenu( DeckMenu * _menu, const string& path, const string& smallDeckPrefix, Player * statsPlayer){
bool translate = _menu->autoTranslate; bool translate = _menu->autoTranslate;
_menu->autoTranslate = false; _menu->autoTranslate = false;
vector<DeckMetaData *> deckMetaDataVector = getValidDeckMetaData( path, smallDeckPrefix, statsPlayer ); vector<DeckMetaData *> deckMetaDataVector = getValidDeckMetaData( path, smallDeckPrefix, statsPlayer );
@@ -34,7 +34,7 @@ vector<DeckMetaData *> GameState::fillDeckMenu( DeckMenu * _menu, string path, s
} }
vector<DeckMetaData *> GameState::getValidDeckMetaData( string path, string smallDeckPrefix, Player * statsPlayer) vector<DeckMetaData *> GameState::getValidDeckMetaData( const string& path, const string& smallDeckPrefix, Player * statsPlayer)
{ {
vector<DeckMetaData*> retList; vector<DeckMetaData*> retList;
@@ -43,30 +43,25 @@ vector<DeckMetaData *> GameState::getValidDeckMetaData( string path, string smal
int nbDecks = 1; int nbDecks = 1;
while (found){ while (found){
found = 0; found = 0;
char buffer[512]; std::ostringstream filename;
char smallDeckName[512]; filename << path << "/deck" << nbDecks << ".txt";
char deckDesc[512]; DeckMetaData * meta = metas->get(filename.str(), statsPlayer);
sprintf(buffer, "%s/deck%i.txt",path.c_str(),nbDecks);
DeckMetaData * meta = metas->get(buffer, statsPlayer);
if (meta) if (meta)
{ {
found = 1; found = 1;
sprintf(smallDeckName, "%s_deck%i",smallDeckPrefix.c_str(),nbDecks);
if (statsPlayer){ if (statsPlayer){
string smallDeckNameStr = string(smallDeckName); std::ostringstream smallDeckName;
meta->loadStatsForPlayer( statsPlayer, smallDeckNameStr ); smallDeckName << smallDeckPrefix << "_deck" << nbDecks;
meta->loadStatsForPlayer( statsPlayer, smallDeckName.str());
} }
else else
{ {
char playerStatsDeckName[512]; std::ostringstream playerStatsDeckName;
playerStatsDeckName << "stats/player_deck" << nbDecks << ".txt";
sprintf(playerStatsDeckName, "stats/player_deck%i.txt", nbDecks); string deckstats = options.profileFile(playerStatsDeckName.str());
string deckstats = options.profileFile(playerStatsDeckName);
meta->loadStatsForPlayer( NULL, deckstats ); meta->loadStatsForPlayer( NULL, deckstats );
} }
deckDesc[16] = 0;
retList.push_back( meta ); retList.push_back( meta );
nbDecks++; nbDecks++;
} }
@@ -81,12 +76,12 @@ vector<DeckMetaData *> GameState::getValidDeckMetaData( string path, string smal
// build a menu with the given deck list and return a vector of the deck ids created. // build a menu with the given deck list and return a vector of the deck ids created.
void GameState::renderDeckMenu ( SimpleMenu * _menu, vector<DeckMetaData *> deckMetaDataList ) void GameState::renderDeckMenu ( SimpleMenu * _menu, const vector<DeckMetaData *>& deckMetaDataList )
{ {
int deckNumber = 1; int deckNumber = 1;
Translator * t = Translator::GetInstance(); Translator * t = Translator::GetInstance();
map<string,string>::iterator it; map<string,string>::iterator it;
for (vector<DeckMetaData *>::iterator i = deckMetaDataList.begin(); i != deckMetaDataList.end(); i++) for (vector<DeckMetaData *>::const_iterator i = deckMetaDataList.begin(); i != deckMetaDataList.end(); i++)
{ {
DeckMetaData * deckMetaData = *i; DeckMetaData * deckMetaData = *i;
string deckName = deckMetaData -> getName(); string deckName = deckMetaData -> getName();
@@ -102,12 +97,12 @@ void GameState::renderDeckMenu ( SimpleMenu * _menu, vector<DeckMetaData *> deck
// build a menu with the given deck list and return a vector of the deck ids created. // build a menu with the given deck list and return a vector of the deck ids created.
void GameState::renderDeckMenu ( DeckMenu * _menu, vector<DeckMetaData *> deckMetaDataList ) void GameState::renderDeckMenu ( DeckMenu * _menu, const vector<DeckMetaData *>& deckMetaDataList )
{ {
int deckNumber = 1; int deckNumber = 1;
Translator * t = Translator::GetInstance(); Translator * t = Translator::GetInstance();
map<string,string>::iterator it; map<string,string>::iterator it;
for (vector<DeckMetaData *>::iterator i = deckMetaDataList.begin(); i != deckMetaDataList.end(); i++) for (vector<DeckMetaData *>::const_iterator i = deckMetaDataList.begin(); i != deckMetaDataList.end(); i++)
{ {
DeckMetaData * deckMetaData = *i; DeckMetaData * deckMetaData = *i;
string deckName = deckMetaData -> getName(); string deckName = deckMetaData -> getName();