From f83c7bc4ef12b0bbdc1cfa6bdce35d9665510e6c Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Sun, 11 Dec 2016 15:16:33 +0700 Subject: [PATCH] Account active is now stored as an integer to avoid weird loops to find the right id --- Source/Account.cpp | 62 ++++++++++++++++++++++++---------------------- Source/Account.h | 17 ++++++------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/Source/Account.cpp b/Source/Account.cpp index bae2abd..560592f 100644 --- a/Source/Account.cpp +++ b/Source/Account.cpp @@ -30,7 +30,7 @@ using namespace std; extern bool devtools; vector Account::accounts = vector(); -Account* Account::_active = nullptr; +int Account::i_active = -1; Account::Account(const string& name) : name(name), campaignProgress() { @@ -52,36 +52,47 @@ void Account::setCurrentCampaign(const string& name) void Account::add(const string& name) { accounts.push_back(new Account(name)); - _active = accounts.back(); + i_active = accounts.size() - 1; } Account* Account::get(int i) { - if ((i >= 0) && (i < int(accounts.size()))) { - return accounts[i]; - } else - return NULL; + return accounts.at(i); +} + +int Account::getNbAccounts() +{ + return accounts.size(); +} + +bool Account::hasActive() +{ + return (i_active >= 0); +} + +Account& Account::active() +{ + return *(accounts.at(i_active)); } void Account::setActive(int i) { - _active = get(i); + if ((i >= 0) && (i < int(accounts.size()))) { + i_active = i; + } else { + cerr << "Tried to set active account to " << i << " but there is not such account" << endl; + i_active = -1; + } } void Account::destroyActive() { - for (unsigned i = 0; i < accounts.size(); i++) { - if (accounts[i] == _active) { - accounts.erase(accounts.begin() + i); - _active = nullptr; - return; - } - } - cerr << "Unexpected error : User " << _active->getName() << " not found" << endl; - if (accounts.empty()) { - _active = nullptr; + if ((i_active >= 0) && (i_active < int(accounts.size()))) { + accounts.erase(accounts.begin() + i_active); + i_active = -1; } else { - _active = accounts.front(); + cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl; + i_active = -1; } } @@ -191,10 +202,10 @@ void Account::loadFile(string filename) } fclose(tfile); - _active = get(iactive); + setActive(iactive); } else { perror(("Couldn't load users from " + filename).c_str()); - _active = nullptr; + i_active = -1; } } @@ -206,7 +217,7 @@ void Account::saveFile(string filename) tfile = fopen(filename.c_str(), "wb" ); if (tfile) { fpackf(tfile, "Bi", getNbAccounts()); - fpackf(tfile, "Bi", indice(Account::_active)); + fpackf(tfile, "Bi", i_active); for (int i = 0; i < getNbAccounts(); i++) { Account* a = Account::get(i); @@ -255,12 +266,3 @@ void Account::saveFile(string filename) perror(("Couldn't save users in " + filename).c_str()); } } - -int Account::indice(Account* a) -{ - for (unsigned i = 0; i < accounts.size(); i++) { - if (accounts[i] == a) - return i; - } - return -1; -} diff --git a/Source/Account.h b/Source/Account.h index f9bc5e1..dcaba95 100644 --- a/Source/Account.h +++ b/Source/Account.h @@ -49,11 +49,10 @@ public: static Account* get(int i); static void loadFile(std::string filename); static void saveFile(std::string filename); - static int indice(Account* a); + static int getNbAccounts(); - static bool hasActive() { return (_active != nullptr); } - static Account& active() { return *_active; } - static Account* _active; + static bool hasActive(); + static Account& active(); void endGame(); void winCampaignLevel(int choice, float score, float time); @@ -109,10 +108,11 @@ public: }; void setCurrentCampaign(const std::string& name); - static int getNbAccounts() { - return accounts.size(); - }; private: + //statics + static std::vector accounts; + static int i_active; + Account(const std::string& name = ""); int difficulty; int progress; // progress in challenge levels @@ -124,9 +124,6 @@ private: std::string currentCampaign; std::map campaignProgress; - - //statics - static std::vector accounts; }; #endif -- 2.39.5