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

DbObject.cpp

Go to the documentation of this file.
00001 //====================================================================
00002 //      DbObject implementation
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : System ( The LHCb Offline System)
00006 //
00007 //  Description: Generic data persistency
00008 //
00009 //      Author     : M.Frank
00010 //  History    :
00011 // +---------+----------------------------------------------+---------
00012 // |    Date |                 Comment                      | Who     
00013 // +---------+----------------------------------------------+---------
00014 // | 11/07/99| Initial version                              | MF
00015 // +---------+----------------------------------------------+---------
00016 //====================================================================
00017 #define DBCNV_DBOBJECT_CPP 1
00018 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiDb/src/Base/DbObject.cpp,v 1.1.1.1 2001/04/18 21:13:35 tlindner Exp $
00019 
00020 // STL include files
00021 #include <iostream>
00022 
00023 // Framework include files
00024 #include "GaudiKernel/System.h"
00025 #include "GaudiDb/IOODataBase.h"
00026 #include "GaudiDb/DbVArray.h"
00027 #include "GaudiDb/DbObject.h"
00028 #include "GaudiDb/DbHandle.h"
00029 #include "GaudiDb/DbPersistent.h"
00030 #include "GaudiDb/DbTypeInfo.h"
00031 #include "GaudiDb/DbContainer.h"
00032 #include "GaudiDb/DbDataBase.h"
00033 //##############################################
00034 
00035 static long s_numDbObjectInstances = 0;
00036 
00037 // Static object creator
00038 DbObject* DbObject::create(void* buffer)    {
00039   return ::new(buffer) DbObject();
00040 }
00041 
00042 // New operator
00043 void* DbObject::operator new(size_t size, const DbTypeInfo& type)   {
00044   dbHandle<DbContainer> cntH;
00045   return DbObject::operator new(size, cntH, type);
00046 }
00047 
00048 // New operator
00049 void* DbObject::operator new(size_t size)   {
00050   DbObject* ptr = (DbObject*)::operator new(size);
00051   ptr->m_internals.m_guard = DbPersistent::createGuard();
00052   return ptr;
00053 }
00054 
00055 void* DbObject::operator new(size_t /* size */ , dbHandle<DbContainer>& cntH, const DbTypeInfo& type)       {
00056   if ( cntH.isValid() && cntH.type() == type.type() )    {
00057     return DbPersistent::allocate(cntH, type);
00058   }
00059         throw std::bad_alloc();
00060 }
00061 
00062 void DbObject::operator delete(void* ptr)   {
00063   DbObject*          o = (DbObject*)ptr;
00064   DbPersistentGuard* g = o->m_internals.m_guard;
00065   DbPersistent*      p = g->persistent();
00066   if ( 0 != p )   {
00067     p->release();
00068   }
00069   ::delete (char*)ptr;
00070   ::delete g;
00071 }
00072 
00074 void DbObject::operator delete(void *ptr, const DbTypeInfo& /* info */ )   {
00075   DbObject::operator delete(ptr);
00076 }
00077 
00079 void DbObject::operator delete(void *ptr, dbHandle<DbContainer>& /* cntHdl */ , const DbTypeInfo& /* info */ )    {
00080   DbObject::operator delete(ptr);
00081 }
00082 
00084 bool DbObject::deleteObject()   {
00085   DbPersistentGuard* g = m_internals.m_guard;
00086   DbPersistent*      p = g->persistent();
00087   // Delete persistent representation or release memory
00088   if ( 0 != p )   {
00089     delete p;
00090   }
00091   // Destruct the object
00092   ::delete this;
00093   ::delete g;
00094   return true;
00095 }
00096 
00097 // Standard constructor
00098 DbObject::DbObject()   {
00099   increaseCounter();
00100 }
00101 
00102 // Standard destructor
00103 DbObject::~DbObject()   {
00104   decreaseCounter();
00105 }
00106 
00107 void DbObject::decreaseCounter()    {
00108   s_numDbObjectInstances--;
00109 }
00110 
00112 void DbObject::increaseCounter()    {
00113   s_numDbObjectInstances++;
00114 }
00115 
00117 StreamBuffer& DbObject::streamBuffer()     {
00118   return m_internals.m_guard->objBuffer();
00119 }
00120 
00122 StreamBuffer& DbObject::linkBuffer()     {
00123   return m_internals.m_guard->linkBuffer();
00124 }
00125 
00127 unsigned char DbObject::storageType()   const     {
00128   return m_internals.m_guard->type();
00129 }
00130 
00132 bool DbObject::isPersistent()   const   {
00133   return m_internals.m_guard->type() != TEST_StorageType;
00134 }
00135 
00137 long DbObject::numLink()    const      {
00138   return m_internals.m_guard->links().size();
00139 }
00140 
00142 dbVarray< dbHandle<DbLink> >& DbObject::links()   {
00143   return m_internals.m_guard->links();
00144 }
00145 
00147 const dbVarray< dbHandle<DbLink> >& DbObject::links()  const   {
00148   return m_internals.m_guard->links();
00149 }
00150 
00152 long DbObject::bufferSize() const  {
00153   return m_internals.m_guard->size();
00154 }
00156 long DbObject::objSize()  const   {
00157   return m_internals.m_guard->objectSize();
00158 }
00160 void DbObject::setObjSize(long length)    {
00161   m_internals.m_guard->setObjectSize(length);
00162 }
00163 
00164 long DbObject::numInstances()    {
00165   return s_numDbObjectInstances;
00166 }
00167 
00168 long DbObject::addRef()    {
00169   DbPersistentGuard* g = m_internals.m_guard;
00170   return g->addRef();
00171 }
00172 
00173 long DbObject::release()   {
00174   DbPersistentGuard* g = m_internals.m_guard;
00175   long cnt = g->release();
00176   if ( cnt <= 0 )   {
00177     delete this;
00178   }
00179   return cnt;
00180 }
00181 
00182 DbResult DbObject::addAssocEntry(const std::string& db, const std::string& cnt, const std::string& obj, dbHandle<DbLink>& to)   {
00183   dbHandle<DbObject> objH = this;
00184   dbHandle<DbDataBase> dbH = objH.containedIn().containedIn();
00185   return dbH.addAssocEntry(db, cnt, obj, to);
00186 }
00187 
00188 long DbObject::addLink(const std::string& db, const std::string& cnt, const std::string& obj, const dbHandle<DbLink>& to)    {
00189   dbHandle<DbLink> internal_to(to);
00190   //std::cout << "Add leaf:" << obj << " #Links:" << m_objLinks.size() << " #Leafs:" << m_objSymLinks.size() << std::endl;
00191   if ( addAssocEntry(db, cnt, obj, internal_to) )   {
00192     return links().extend(internal_to);
00193   }
00194   links().extend(internal_to);
00195   return DbOOMs::DbError;
00196 }
00197 
00198 long DbObject::addInvalidLink()    {
00199   GenericLinkBase lnk;
00200   links().extend(lnk);
00201   return DbOOMs::DbSuccess;
00202 }
00203 
00204 void DbObject::badAssignMessage()   {
00205     std::cout << System::moduleName() 
00206               << "--> DbCnv::DbObject> "
00207               << "Tried to assign bad Pointer to Accessor!" << std::endl;
00208 }

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