00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __PANA_DATABASE_H__
00035 #define __PANA_DATABASE_H__
00036
00037 #include <string.h>
00038 #include "pana_rbtree.h"
00039
00044 template<class ARG>
00045 class PANA_SessionTree : public RbTreeTree
00046 {
00047 protected:
00048 class PANA_DbEntry : public RbTreeData {
00049 public:
00050 PANA_DbEntry(std::string &index) : m_Index(index) { }
00051 void operator()() { }
00052 int operator<(RbTreeData &cmp) {
00053 PANA_DbEntry &compare = reinterpret_cast<PANA_DbEntry&>(cmp);
00054 return (m_Index < compare.Index());
00055 }
00056 int operator==(RbTreeData &cmp) {
00057 PANA_DbEntry &compare = reinterpret_cast<PANA_DbEntry&>(cmp);
00058 return (m_Index == compare.Index());
00059 }
00060 void clear(void *userData = 0) { }
00061 std::string &Index() { return m_Index; }
00062 protected:
00063 std::string m_Index;
00064 };
00065
00066 public:
00067 PANA_SessionTree() { }
00068 virtual ~PANA_SessionTree() { }
00069
00070 void Add(std::string &index, ARG &arg) {
00071 PANA_DbEntry *newEntry;
00072 ACE_NEW_NORETURN(newEntry, PANA_DbEntry(index));
00073 if (newEntry) {
00074 newEntry->payload = reinterpret_cast<void*>(&arg);
00075 if (Insert(newEntry)) {
00076 return;
00077 }
00078 throw (PANA_Exception(PANA_Exception::DATABASE_ERROR,
00079 "Failed to insert new entry into rbtree"));
00080 }
00081 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00082 "Allocation error for new session entry"));
00083 }
00084 void Remove(std::string &index, ARG **arg = NULL) {
00085 PANA_DbEntry lookupEntry(index), *searchEntry;
00086 searchEntry = static_cast<PANA_DbEntry*>
00087 (RbTreeTree::Remove(&lookupEntry));
00088 if (searchEntry) {
00089 if (arg) {
00090 *arg = reinterpret_cast<ARG*>(searchEntry->payload);
00091 }
00092 return;
00093 }
00094 throw (PANA_Exception(PANA_Exception::DATABASE_ERROR,
00095 "Failed to delete entry from rbtree"));
00096 }
00097 ARG *Search(std::string &index) {
00098 PANA_DbEntry lookupEntry(index), *searchEntry;
00099 searchEntry = static_cast<PANA_DbEntry*>
00100 (RbTreeTree::Find(&lookupEntry));
00101 if (searchEntry) {
00102 return reinterpret_cast<ARG*>(searchEntry->payload);
00103 }
00104 throw (PANA_Exception(PANA_Exception::DATABASE_ERROR,
00105 "Could not find session entry"));
00106 }
00107 };
00108
00109 #endif // __PANA_DATABASE_H__