diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dalstorage.cpp | 96 | ||||
-rw-r--r-- | src/dalstorage.h | 31 | ||||
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/storage.cpp | 82 | ||||
-rw-r--r-- | src/storage.h | 122 |
5 files changed, 230 insertions, 109 deletions
diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 73e4f101..1d818fb0 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -110,49 +110,6 @@ DALStorage::~DALStorage() /** - * Save changes to the database permanently. - */ -void -DALStorage::flush(void) -{ - // this feature is not currently provided by DAL. -} - - -/** - * Get the number of Accounts saved in database. - */ -unsigned int -DALStorage::getAccountCount(void) -{ - // connect to the database (if not connected yet). - connect(); - - using namespace dal; - - unsigned int value = 0; - - try { - // query the database. - std::string sql("select count(*) from "); - sql += ACCOUNTS_TBL_NAME; - sql += ";"; - const RecordSet& rs = mDb->execSql(sql); - - // specialize the string_to functor to convert - // a string to an unsigned int. - string_to<unsigned int> toUint; - - value = toUint(rs(0, 0)); - } catch (const DbSqlQueryExecFailure& f) { - std::cout << "Get accounts count failed :'(" << std::endl; - } - - return value; -} - - -/** * Get an account by user name. */ Account* @@ -244,6 +201,59 @@ DALStorage::getAccount(const std::string& userName) /** + * Add a new account. + */ +void +DALStorage::addAccount(const Account* account) +{ + // TODO +} + + +/** + * Save changes to the database permanently. + */ +void +DALStorage::flush(void) +{ + // this feature is not currently provided by DAL. +} + + +/** + * Get the number of Accounts saved in database. + */ +unsigned int +DALStorage::getAccountCount(void) +{ + // connect to the database (if not connected yet). + connect(); + + using namespace dal; + + unsigned int value = 0; + + try { + // query the database. + std::string sql("select count(*) from "); + sql += ACCOUNTS_TBL_NAME; + sql += ";"; + const RecordSet& rs = mDb->execSql(sql); + + // specialize the string_to functor to convert + // a string to an unsigned int. + string_to<unsigned int> toUint; + + value = toUint(rs(0, 0)); + } catch (const DbSqlQueryExecFailure& f) { + std::cout << "Get accounts count failed :'(" << std::endl; + } + + return value; +} + + +/** * Connect to the database and initialize it if necessary. */ void diff --git a/src/dalstorage.h b/src/dalstorage.h index a29739e8..d53ef505 100644 --- a/src/dalstorage.h +++ b/src/dalstorage.h @@ -51,6 +51,26 @@ class DALStorage: public Storage public: /** + * Get an account by user name. + * + * @param userName the owner of the account. + * + * @return the account associated to the user name. + */ + Account* + getAccount(const std::string& userName); + + + /** + * Add a new account. + * + * @param account the new account. + */ + void + addAccount(const Account* account); + + + /** * Save changes to the database permanently. */ void @@ -66,17 +86,6 @@ class DALStorage: public Storage getAccountCount(void); - /** - * Get an account by user name. - * - * @param userName the owner of the account. - * - * @return the account associated to the user name. - */ - Account* - getAccount(const std::string& userName); - - private: /** * Constructor. diff --git a/src/main.cpp b/src/main.cpp index 60f67ff9..dbde8cad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -135,7 +135,7 @@ void deinitialize() #endif // Get rid of persistent data storage - Storage::deleteInstance(); + tmwserv::Storage::destroy(); delete logger; } @@ -172,13 +172,13 @@ int main(int argc, char *argv[]) session->startListen(connectionHandler, SERVER_PORT); logger->log("Listening on port %d...", SERVER_PORT); - Storage *store = Storage::getInstance(); + tmwserv::Storage& store = tmwserv::Storage::instance(); - std::cout << "Number of accounts on server: " << store->getAccountCount() << std::endl; + std::cout << "Number of accounts on server: " << store.getAccountCount() << std::endl; // Test the database retrieval code std::cout << "Attempting to retrieve account with username 'nym'" << std::endl; - Account* acc = store->getAccount("nym"); + Account* acc = store.getAccount("nym"); if (acc) { std::cout << "Account name: " << acc->getName() << std::endl diff --git a/src/storage.cpp b/src/storage.cpp index bb2bd6aa..b5476426 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -4,52 +4,86 @@ * * This file is part of The Mana World. * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id$ */ + +#include <stdexcept> + #include "storage.h" #include "dalstorage.h" -//#include "sqlitestorage.h" -Storage *Storage::instance = NULL; +namespace tmwserv +{ + + +// initialize the static attribute. +Storage* Storage::mInstance = 0; -Storage::Storage() + +/** + * Constructor. + */ +Storage::Storage(void) + throw() { + // NOOP } -Storage::~Storage() + +/** + * Destructor. + */ +Storage::~Storage(void) + throw() { + // NOOP } -Storage *Storage::getInstance() + +/** + * Create an instance of Storage. + */ +Storage& +Storage::instance(void) { - if (instance == NULL) - { - instance = new tmwserv::DALStorage(); //SQLiteStorage(); + if (mInstance == 0) { + mInstance = new DALStorage(); + } + + // check that the instance has been created. + if (mInstance == 0) { + throw std::bad_alloc(); } - return instance; + return (*mInstance); } -void Storage::deleteInstance() + +/** + * Delete the instance. + */ +void +Storage::destroy(void) { - if (instance != NULL) - { - delete instance; + if (mInstance != 0) { + delete mInstance; } } + + +} // namespace tmwserv diff --git a/src/storage.h b/src/storage.h index e5be589d..e6c93ba7 100644 --- a/src/storage.h +++ b/src/storage.h @@ -4,65 +4,133 @@ * * This file is part of The Mana World. * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id$ */ -#ifndef TMWSERV_STORAGE_H -#define TMWSERV_STORAGE_H + + +#ifndef _TMWSERV_STORAGE_H_ +#define _TMWSERV_STORAGE_H_ + #include "object.h" #include "account.h" + +namespace tmwserv +{ + + /** - * A storage of persistent dynamic data. + * A storage to load and persist dynamic data. + * + * Notes: + * - this class implements the singleton design pattern. + * - destroy() must be called at least once before the application + * exits or else there will be a memory leak. */ class Storage { public: - static Storage *getInstance(); - static void deleteInstance(); + /** + * Create an instance of Storage. + * + * @return the unique instance of Storage. + * + * @exception std::bad_alloc if the instance cannot be created. + */ + static Storage& + instance(void); + /** - * Make sure any changes are saved. + * Delete the storage. */ - virtual void flush() = 0; + static void + destroy(void); + /** - * Account count (test function). + * Get an account by user name. + * + * @param userName the owner of the account. + * + * @return the account associated to the user name. + */ + virtual Account* + getAccount(const std::string& userName) = 0; + + + /** + * Add a new account. + * + * @param account the new account. */ - virtual unsigned int getAccountCount() = 0; + virtual void + addAccount(const Account* account) = 0; + /** - * Get account by username. + * Make sure any changes are saved. */ - virtual Account *getAccount(const std::string &username) = 0; + virtual void + flush(void) = 0; + + + /** + * Account count (test function). + */ + virtual unsigned int + getAccountCount(void) = 0; + protected: /** - * Constructor. + * Default constructor. */ - Storage(); + Storage(void) + throw(); + /** * Destructor. */ - virtual ~Storage(); + virtual + ~Storage(void) + throw(); + + + /** + * Copy constructor. + */ + Storage(const Storage& rhs); + + + /** + * Assignment operator. + */ + Storage& + operator=(const Storage& rhs); + private: - static Storage *instance; + static Storage* mInstance; /**< the unique instance of Storage */ }; -#endif /* TMWSERV_STORAGE_H */ + +} // namespace tmwserv + + +#endif // _TMWSERV_STORAGE_H_ |