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

DbHandle.h

Go to the documentation of this file.
00001 //====================================================================
00002 //      DbHandle and related class definitions
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package  : DbCnv ( The LHCb Offline System)
00006 //      Author   : M.Frank
00007 //====================================================================
00008 #ifndef DBCNV_DBHANDLE_H
00009 #define DBCNV_DBHANDLE_H 1
00010 
00011 // Forward declarations
00012 class PersistentObject;
00013 class DbContainer;
00014 class DbTypeInfo;
00015 class DbObject;
00016 class DbLink;
00017 template <class T> class dbHandle;
00018 template <class T> class dbObjectHandle;
00019 
00020 // Framework include files
00021 #include "GaudiDb/DbBase.h"
00022 #ifndef DBCNV_DBOBJECT_H
00023 #include "GaudiDb/DbObject.h"
00024 #endif
00025 #include "GaudiKernel/GenericLink.h"
00026 
00027 // STL include files
00028 #include <string>
00029 
00050 class DbObjectAccessor  {
00051 private:
00052 protected:
00054   void switchPtr(unsigned char* type, DbObject*& from, DbObject* to);
00055 public:
00057   DbObjectAccessor();
00059   virtual ~DbObjectAccessor();
00061   DbResult open(dbObjectHandle<DbObject>& objH, dbHandle<DbContainer>& cntH, const dbHandle<DbLink>& linkH, DbAccessMode mode = DbOOMs::READ);
00063   DbResult validateAssoc(const DbObject* objP, unsigned char typ,
00064          const dbHandle<DbLink>& lnk,std::string& fname,std::string& cname,std::string& oname);
00066   dbHandle<DbContainer>& containedIn(const DbObject* objP) const;
00068   static dbHandle<DbContainer>* getCntHdl(const DbObject* objP);
00069 };
00070 //##############################################################################
00071 
00092 template <class UserClass> class dbObjectHandle          
00093   : public dbHandleBase< UserClass >,           
00094     public DbObjectAccessor             
00095 {                       
00096   friend DbContainer;             
00097   friend dbHandle<DbObject>;            
00098   friend dbHandle<DbContainer>;           
00099 protected:                    
00101   void setType(unsigned char type) const  {
00102     m_type = type;
00103   }
00105   void setObject(const DbObject* obj)   {
00106     try {                   
00107       DbObjectAccessor::switchPtr(&m_type,(DbObject*&)m_ptr,(DbObject*)obj);
00108     }                     
00109     catch(...)  {                 
00110       printf("Illegal assignment to  dbHandle < UserClass >\n");  
00111       DbObjectAccessor::switchPtr(&m_type, (DbObject*&)m_ptr, 0); 
00112     }                     
00113   }                     
00114 public:                     
00116   virtual ~dbObjectHandle()   {
00117     setObject(0);                 
00118   }
00120   dbObjectHandle()  {
00121   }
00123   dbObjectHandle(unsigned short typ)  {
00124     setType(typ);
00125   }
00127   template <class T> dbObjectHandle(const dbObjectHandle<T>& copy)    
00128   : dbHandleBase<UserClass>(), DbObjectAccessor()
00129   {
00130     setObject(dynamic_cast<const UserClass*>(copy.ptr()));           
00131     setType(copy.type());               
00132   }
00134   dbObjectHandle(const dbObjectHandle<UserClass>& copy)    
00135     : dbHandleBase<UserClass>(), DbObjectAccessor()
00136   {
00137     setObject(dynamic_cast<const UserClass*>(copy.ptr()));           
00138     setType(copy.type());               
00139   }
00141   template <class T> dbObjectHandle<UserClass>& operator=(const dbObjectHandle<T>& copy)
00142   {
00143     setObject(dynamic_cast<const UserClass*>(copy.ptr()));
00144     setType(copy.type());
00145     return *this;
00146   }
00148   dbObjectHandle<UserClass>& operator=(const dbObjectHandle<UserClass>& copy)    {
00149     setObject(copy.ptr());
00150     setType(copy.type());
00151     return *this;
00152   }
00154   bool operator==(const dbObjectHandle<DbObject>& copy)   const  {
00155     return m_type == copy.m_type && m_ptr == copy.m_ptr;
00156   }
00158   long addRef()  {
00159     if ( m_ptr )  {
00160       return m_ptr->addRef();
00161     }
00162     return -1;
00163   }
00165   long release()  {
00166     if ( m_ptr )  {
00167       return m_ptr->release();
00168     }
00169     return -1;
00170   }
00172   dbHandle<DbContainer>& containedIn() const    {
00173     return DbObjectAccessor::containedIn(m_ptr);
00174   }
00176   DbResult validateAssoc(const dbHandle<DbLink>& lnk,std::string& fname,std::string& cname,std::string& oname)    {
00177     return DbObjectAccessor::validateAssoc(m_ptr, m_type, lnk, fname, cname, oname);
00178   }
00180   DbResult open(dbHandle<DbContainer>& cntH,const dbHandle<DbLink>& linkH,DbAccessMode mode = DbOOMs::READ)   {
00181     if ( m_ptr == 0 )    {
00182       dbObjectHandle<DbObject> objH(type());
00183       DbResult status = DbObjectAccessor::open(objH,cntH,linkH,mode);
00184       setObject(dynamic_cast<const UserClass*>(objH.ptr()));
00185       return status;
00186     }                     
00187     return DbOOMs::DbSuccess;             
00188   }                     
00189 };
00190 
00191 template <class UserClass> class dbHandle
00192   : public dbObjectHandle< UserClass >
00193 {                       
00194   friend DbContainer;             
00195   friend dbHandle<DbObject>;            
00196   friend dbHandle<DbContainer>;           
00197 protected:                    
00198 public:                     
00200   virtual ~dbHandle()   {
00201   }
00203   dbHandle()    {
00204   }
00206   dbHandle(unsigned char typ)
00207   : dbObjectHandle<UserClass>()
00208   {
00209     setType(typ);                 
00210   }
00212   dbHandle(const DbObject* obj)
00213   : dbObjectHandle<UserClass>()
00214   {
00215     setObject(dynamic_cast<const UserClass*>(obj));                 
00216   }
00218   dbHandle(const UserClass* obj)
00219   : dbObjectHandle<UserClass>()
00220   {
00221     setObject(obj);
00222   }
00224   template <class T> dbHandle(const dbObjectHandle<T>& copy)
00225   : dbObjectHandle<UserClass>()
00226   {
00227     setObject(dynamic_cast<const UserClass*>(copy.ptr()));           
00228     setType(copy.type());               
00229   }
00231   dbHandle(const dbObjectHandle<UserClass>& copy) 
00232   : dbObjectHandle<UserClass>()
00233   {
00234     setObject(copy.ptr());           
00235     setType(copy.type());               
00236   }
00238   template <class T> dbHandle<UserClass>& operator=(const dbObjectHandle<T>& copy)    {
00239     const UserClass* p = dynamic_cast<const UserClass*>(copy.ptr());
00240     setObject(p);
00241     setType(copy.type());
00242     return *this;
00243   }
00245   dbHandle<UserClass>& operator=(const dbObjectHandle<UserClass>& copy)    {
00246     setObject(copy.ptr());
00247     setType(copy.type());
00248     return *this;
00249   }
00251   operator dbObjectHandle<DbObject>()   {
00252     dbObjectHandle<DbObject> handle;
00253     handle.setObject(dynamic_cast<const DbObject*>(m_ptr));
00254     handle.setType(m_type);
00255     return handle;
00256   }
00258   operator const dbObjectHandle<DbObject>()   const   {
00259     dbObjectHandle<DbObject> handle;
00260     handle.setObject(dynamic_cast<const DbObject*>(m_ptr));
00261     handle.setType(m_type);
00262     return handle;
00263   }
00264 };
00265 
00286 template <> class dbHandle<DbObject> : public dbObjectHandle<DbObject>    {
00287   // Friend declarations
00288   friend DbContainer;
00289 public:
00291   dbHandle()  {
00292   }
00294   dbHandle(unsigned char type)  {
00295     setType(type);
00296   }
00298   virtual ~dbHandle()  {
00299     setObject(0);
00300   }
00302   dbHandle(DbObject* obj) {
00303     setObject(obj);
00304   }
00306   dbHandle(DbObject* obj, unsigned char type) {
00307     setType(type);
00308     setObject(obj);
00309   }
00311   template <class T> dbHandle(const dbHandle<T>& copy)
00312   : dbObjectHandle<DbObject>()
00313   {
00314     setObject(dynamic_cast<const DbObject*>(copy.ptr()));
00315     setType(copy.type());
00316   }
00318   dbHandle(const dbHandle<DbObject>& copy)
00319   : dbObjectHandle<DbObject>()
00320   {
00321     setObject(copy.ptr());
00322     setType(copy.type());
00323   }
00325   template <class T> dbHandle<DbObject>& operator=(const dbObjectHandle<T>& copy)    {
00326     setObject(dynamic_cast<const DbObject*>(copy.ptr()));
00327     setType(copy.type());
00328     return *this;
00329   }
00331   dbHandle<DbObject>& operator=(const dbObjectHandle<DbObject>& copy)    {
00332     setObject(copy.ptr());
00333     setType(copy.type());
00334     return *this;
00335   }
00337   DbResult prepare(const dbObjectHandle<DbObject>& objH, dbHandle<DbLink>& linkH, GenericLinkBase::GENERIC& gene);
00338 };
00339 
00340 
00341 inline 
00342 DbObjectAccessor::DbObjectAccessor()  {
00343 }
00344 
00345 inline 
00346 DbObjectAccessor::~DbObjectAccessor()  {
00347 }
00348 
00349 inline
00350 void DbObjectAccessor::switchPtr(unsigned char* pType, DbObject*& from, DbObject* to)  {
00351   try {
00352     DbObject* ptr = from;
00353     from = to;
00354     if ( from )  {
00355       from->addRef();
00356       *pType = from->storageType();
00357     }
00358     if ( ptr )  {
00359     ptr->release();
00360     }
00361   }
00362   catch(...)  {
00363     DbObject::badAssignMessage();
00364     from = 0;
00365   }
00366 }
00367 #endif  // DBCNV_DBHANDLE_H
00368 
00369 

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