X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FAccount.cpp;h=560592f62ebf7a0809b579d8b0b1a9757dcbd0e7;hb=f83c7bc4ef12b0bbdc1cfa6bdce35d9665510e6c;hp=ba91890d0de7d5173c2faa355e17354d1675ee62;hpb=8a32dc9e4e1011b10f009e999d7d008aa2711d8a;p=lugaru.git diff --git a/Source/Account.cpp b/Source/Account.cpp index ba91890..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; -}