summaryrefslogtreecommitdiff
path: root/src/storage.h
diff options
context:
space:
mode:
authorHuynh Tran <nthuynh75@gmail.com>2005-06-21 19:46:40 +0000
committerHuynh Tran <nthuynh75@gmail.com>2005-06-21 19:46:40 +0000
commit49d79a345c8c4929d9bb787af9f4f0090c888537 (patch)
tree387b3a8d738d570ad1493be0273751c7a145cf20 /src/storage.h
parent816d8acd16faeb20713c3d09466003a444940f6f (diff)
downloadmanaserv-49d79a345c8c4929d9bb787af9f4f0090c888537.tar.gz
manaserv-49d79a345c8c4929d9bb787af9f4f0090c888537.tar.bz2
manaserv-49d79a345c8c4929d9bb787af9f4f0090c888537.tar.xz
manaserv-49d79a345c8c4929d9bb787af9f4f0090c888537.zip
Improved Storage APIs and moved debug code to unit tests.
Diffstat (limited to 'src/storage.h')
-rw-r--r--src/storage.h205
1 files changed, 196 insertions, 9 deletions
diff --git a/src/storage.h b/src/storage.h
index e6c93ba7..5b695bb0 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -25,6 +25,8 @@
#define _TMWSERV_STORAGE_H_
+#include <map>
+
#include "object.h"
#include "account.h"
@@ -34,6 +36,83 @@ namespace tmwserv
/**
+ * Enumeration type for the account status:
+ *
+ * AS_NEW_ACCOUNT : set to an account that is added to the storage and
+ * hence not yet save to disk (file or database);
+ * accounts with this status will be saved to disk at
+ * the next flush.
+ * AS_ACC_TO_UPDATE: set to an account that was loaded from disk;
+ * accounts with this status will be updated at the next
+ * flush.
+ * AS_ACC_TO_DELETE: set to an account that was loaded from disk;
+ * accounts with this status will be deleted from disk at
+ * the next flush.
+ *
+ * Notes: an account that is requested to be deleted and still has the
+ * status AS_NEW_ACCOUNT (and therefore not yet saved to disk) will
+ * be immediately deleted from memory to save useless transactions
+ * to disk.
+ */
+typedef enum {
+ AS_NEW_ACCOUNT,
+ AS_ACC_TO_UPDATE,
+ AS_ACC_TO_DELETE
+} AccountStatus;
+
+
+/**
+ * Functor to be used as the sorting criterion of the map defined below.
+ */
+struct account_sort_by_name
+ : public std::binary_function<Account*, Account*, bool>
+{
+ bool
+ operator()(Account* acc1, Account* acc2) const
+ {
+ return (acc1->getName() < acc2->getName());
+ }
+};
+
+
+/**
+ * Data type for the list of accounts.
+ */
+typedef std::map<Account*, AccountStatus, account_sort_by_name> Accounts;
+
+
+/**
+ * Functor used to search an Account by name in the map defined above.
+ */
+class account_by_name
+{
+ public:
+ /**
+ * Constructor.
+ */
+ account_by_name(const std::string& name)
+ : mName(name)
+ {
+ // NOP
+ }
+
+
+ /**
+ * Operator().
+ */
+ bool
+ operator()(std::pair<Account*, AccountStatus> elem) const
+ {
+ return (elem.first)->getName() == mName;
+ }
+
+
+ private:
+ std::string mName; /**< the name to look for */
+};
+
+
+/**
* A storage to load and persist dynamic data.
*
* Notes:
@@ -45,14 +124,25 @@ class Storage
{
public:
/**
- * Create an instance of Storage.
+ * Create a named instance of Storage.
+ *
+ * @param name the name of the storage.
*
* @return the unique instance of Storage.
*
* @exception std::bad_alloc if the instance cannot be created.
+ *
+ * Notes:
+ * - it is up to the underlying implementation of Storage to
+ * decide about what to do with the name, it could serve as the
+ * name of the database or the name of the file into which the
+ * storage will be dumped to.
+ * - the name of the storage is saved only when it's the first
+ * invocation of instance() or only when instance() is invoked
+ * after destroy().
*/
static Storage&
- instance(void);
+ instance(const std::string& name);
/**
@@ -63,6 +153,92 @@ class Storage
/**
+ * Open the storage for read/write access.
+ *
+ * Depending on the underlying implementation of Storage, opening
+ * a storage would mean either opening a file or connecting to a
+ * database.
+ */
+ virtual void
+ open(void) = 0;
+
+
+ /**
+ * Close the storage.
+ *
+ * Depending on the underlying implementation of Storage, closing
+ * a storage would mean either closing a file or disconnecting from
+ * a database.
+ */
+ virtual void
+ close(void) = 0;
+
+
+ /**
+ * Check if the storage is open.
+ *
+ * @return true if the storage is open.
+ */
+ bool
+ isOpen(void) const;
+
+
+ /**
+ * Get the storage name.
+ *
+ * @return the storage name.
+ */
+ const std::string&
+ getName(void) const;
+
+
+ /**
+ * Set a user name for the storage.
+ *
+ * Depending on the underlying implementation of Storage, setting
+ * the user name may have no effect (e.g. DALStorage running on
+ * SQLite).
+ *
+ * @param userName the user name.
+ */
+ void
+ setUser(const std::string& userName);
+
+
+ /**
+ * Get the user name.
+ *
+ * @return the user name (it may be an empty string if not set
+ * previously).
+ */
+ const std::string&
+ getUser(void) const;
+
+
+ /**
+ * Set a user password for the storage.
+ *
+ * Depending on the underlying implementation of Storage, setting
+ * the user password may have no effect (e.g. DALStorage running on
+ * SQLite).
+ *
+ * @param password the user password.
+ */
+ void
+ setPassword(const std::string& password);
+
+
+ /**
+ * Get the user password.
+ *
+ * @return the user password (it may be an empty string if not set
+ * previously).
+ */
+ const std::string&
+ getPassword(void) const;
+
+
+ /**
* Get an account by user name.
*
* @param userName the owner of the account.
@@ -79,21 +255,23 @@ class Storage
* @param account the new account.
*/
virtual void
- addAccount(const Account* account) = 0;
+ addAccount(Account* account) = 0;
/**
- * Make sure any changes are saved.
+ * Delete an account.
+ *
+ * @param userName the owner of the account.
*/
virtual void
- flush(void) = 0;
+ delAccount(const std::string& userName) = 0;
/**
- * Account count (test function).
+ * Saves the changes permanently.
*/
- virtual unsigned int
- getAccountCount(void) = 0;
+ virtual void
+ flush(void) = 0;
protected:
@@ -125,8 +303,17 @@ class Storage
operator=(const Storage& rhs);
+ protected:
+ Accounts mAccounts; /**< list of accounts in memory */
+ Beings mCharacters; /**< the loaded characters */
+ bool mIsOpen; /**< flag is true if the storage is open */
+
+
private:
- static Storage* mInstance; /**< the unique instance of Storage */
+ static Storage* mInstance; /**< the unique instance of Storage */
+ static std::string mName; /**< the name of the storage */
+ static std::string mUser; /**< the user name */
+ static std::string mPassword; /**< the user password */
};