Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

DbIter.h

Go to the documentation of this file.
00001 //====================================================================
00002 //      dbIter definition
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : DbCnv/DbSvc ( The LHCb Offline System)
00006 //
00007 //  Description: Generic persistable object iterator
00008 //
00009 //      Author     : M.Frank
00010 //  History    :
00011 // +---------+----------------------------------------------+---------
00012 // |    Date |                 Comment                      | Who     
00013 // +---------+----------------------------------------------+---------
00014 // | 11/07/99| Initial version                              | MF
00015 // +---------+----------------------------------------------+---------
00016 //====================================================================
00017 #ifndef DBCNV_DBOBJECTITER_H
00018 #define DBCNV_DBOBJECTITER_H
00019 
00020 // Framework include files
00021 #include "GaudiDb/DbObject.h"
00022 #include "GaudiDb/DbHandle.h"
00023 #include "GaudiDb/IOODataBase.h"
00024 #include "DbContainer.h"
00025 
00049 template <class T> class dbIter  {
00051   dbHandle<DbContainer>      m_container;
00053   dbHandle<T>                m_object;
00055   IDbCursor*                 m_cursor;
00056   void releaseCursor()    {
00057     if ( m_cursor )   {
00058       m_cursor->release();
00059       m_cursor = 0;
00060     }
00061   }
00062 public:
00064   dbIter() {
00065     m_cursor = 0;
00066   }
00068   virtual ~dbIter() {
00069     releaseCursor();
00070   }
00072   DbResult scan(const dbHandle<DbContainer>& cntH)    {
00073     releaseCursor();
00074     m_object = dbHandle<T> (cntH.type());
00075     m_container = cntH;
00076     if ( m_container.isValid() )   {
00077       m_cursor = m_container.makeCursor();
00078     }
00079     return ( 0 == m_cursor ) ? DbOOMs::DbError : DbOOMs::DbSuccess;
00080   }
00082   DbResult next()   {
00083     if ( m_cursor )   {
00084       dbHandle<DbObject> objH(m_object.type());
00085       if ( DbOOMs::DbSuccess == m_cursor->next(m_container, objH) )    {
00086         if ( objH.isValid() )   {
00087           m_object = objH;
00088           return DbOOMs::DbSuccess;
00089         }
00090       }
00091     }
00092     return DbOOMs::DbError;
00093   }
00095   dbHandle<T>& operator->()    {
00096     return m_object;
00097   }
00099   bool operator==(const dbIter<T>& iter)   const     {
00100     return m_object.operator->() == iter.m_object.operator->();
00101   }
00102 };
00103 
00127 template <> class dbIter<DbObject>  {
00129   dbHandle<DbContainer>      m_container;
00131   dbHandle<DbObject>         m_object;
00133   IDbCursor*                 m_cursor;
00134   void releaseCursor()    {
00135     if ( m_cursor )   {
00136       m_cursor->release();
00137       m_cursor = 0;
00138     }
00139   }
00140 public:
00142   dbIter() {
00143     m_cursor = 0;
00144   }
00146   virtual ~dbIter() {
00147     releaseCursor();
00148   }
00150   DbResult scan(const dbHandle<DbContainer>& cntH)    {
00151     releaseCursor();
00152     m_object = dbHandle<DbObject> (cntH.type());
00153     m_container = cntH;
00154     if ( m_container.isValid() )   {
00155       m_cursor = m_container.makeCursor();
00156     }
00157     return ( 0 == m_cursor ) ? DbOOMs::DbError : DbOOMs::DbSuccess;
00158   }
00160   DbResult next()   {
00161     if ( m_cursor )   {
00162       if ( DbOOMs::DbSuccess == m_cursor->next(m_container, m_object) )    {
00163         if ( m_object.isValid() )   {
00164           return DbOOMs::DbSuccess;
00165         }
00166       }
00167     }
00168     return DbOOMs::DbError;
00169   }
00171   dbHandle<DbObject>& operator->()    {
00172     return m_object;
00173   }
00175   bool operator==(const dbIter<DbObject>& iter)   const     {
00176     return m_object.operator->() == iter.m_object.operator->();
00177   }
00178 };
00179 #endif // DBCNV_DBOBJECTITER_H

Generated at Wed Nov 21 12:22:19 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000