Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pana_database.h

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
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__

Generated on Fri Jun 25 19:18:29 2004 for PANA by doxygen 1.3.5