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

DbAccessObj.h

Go to the documentation of this file.
00001 //====================================================================
00002 //      DbCnv Base class definitions
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : DbCnv ( The LHCb Offline System)
00006 //      Author     : M.Frank
00007 //====================================================================
00008 #ifndef DBCNV_DBACCESSOBJ_H
00009 #define DBCNV_DBACCESSOBJ_H 1
00010 
00011 // Framework include files
00012 #include "GaudiDb/DbOOMs.h"
00013 #include <string>
00014 #include <vector>
00015 #ifdef _DEBUG_REFCOUNTS
00016   #include <iostream>
00017 #endif
00018 #ifdef WIN32
00019 #  define strcasecmp _stricmp
00020 #else
00021 #  include <strings.h>
00022 #endif
00023 
00047 template <class KEY, class TYPE> class DbAccessObj {
00048 public:
00050   typedef std::vector<TYPE*> Data;
00051   typedef std::vector<KEY>   Keys;
00052   typedef Data::iterator       iterator;
00053   typedef Data::const_iterator const_iterator;
00054 private:
00055   template <class THEKEY> bool isEqual(const THEKEY& a, const THEKEY& b)  const  {
00056     return a == b;
00057   }
00058 #ifndef sun
00059 #if defined(__GNUC__) &&  ( __GNUC__ == 2 &&  __GNUC_MINOR__ >= 95 )
00060   //template <>
00061 #else
00062         template <>
00063 #endif
00064 #endif
00065   bool isEqual(const std::string& a, const std::string& b)  const  {
00066     return ::strcasecmp(a.c_str(), b.c_str()) == 0;
00067   }
00068   long findKey(const KEY& key)    {
00069     long idx = 0;
00070     for ( Keys::iterator j = m_keys.begin(); j != m_keys.end(); idx++, j++ )    {
00071       if ( isEqual((*j), key) )    {
00072         return idx;
00073       }
00074     }
00075     return -1;
00076   }
00077 protected:
00079   mutable long  m_refCount;
00081   std::string   m_name;
00083   DbAccessMode  m_mode;
00085   unsigned char m_type;
00087   Data          m_data;
00089   Keys          m_keys;
00090 
00091 public:
00093   DbAccessObj() : m_refCount(0), m_mode(DbOOMs::READ), m_type(0)  {
00094   }
00096   virtual ~DbAccessObj()   {
00097   }
00099   const std::string& name()  const  {
00100     return m_name;
00101   }
00103   DbAccessMode mode()   const   {
00104     return m_mode;
00105   }
00106   // Inline functions
00107   unsigned char type()  const   {
00108     return m_type;
00109   }
00111   long addRef() const  {
00112 #ifdef _DEBUG_REFCOUNTS
00113     std::cout << typeid(*this).name() << "  " << m_name << " Refcount:" << m_refCount+1  << std::endl;
00114 #endif
00115     return ++m_refCount;
00116   }
00118   long release()  const   {
00119     long count = --m_refCount;
00120 #ifdef _DEBUG_REFCOUNTS
00121     std::cout << typeid(*this).name() << "  " << m_name << " Refcount:" << count  << std::endl;
00122 #endif
00123     if ( m_refCount <= 0 )    {
00124       delete this;
00125     }
00126     return count;
00127   }
00129   void clearEntries()   {
00130     Data::const_iterator i = m_data.begin();
00131     for ( ; i != m_data.end(); i++ )    {
00132       (*i)->release();
00133     }
00134     m_data.erase(m_data.begin(), m_data.end());
00135     m_keys.erase(m_keys.begin(), m_keys.end());
00136   }
00138   const TYPE* find(const KEY& key)  const   {
00139     Data::const_iterator i = m_data.begin();
00140     Keys::const_iterator j = m_keys.begin();
00141     for ( ; i != m_data.end(); i++, j++ )    {
00142       if ( isEqual((*j),key) )    {
00143         return (*i);
00144       }
00145     }
00146     return 0;
00147   }
00149   TYPE* find(const KEY& key)    {
00150     Data::iterator i = m_data.begin();
00151     Keys::iterator j = m_keys.begin();
00152     for ( ; i != m_data.end(); i++, j++ )    {
00153       if ( isEqual((*j),key) )    {
00154         return (*i);
00155       }
00156     }
00157     return 0;
00158   }
00160   bool add(const KEY& key, TYPE* val)   {
00161     if ( -1 == findKey(key) )    {
00162       m_data.push_back(val);
00163       m_keys.push_back(key);
00164       val->addRef();
00165       return true;
00166     }
00167     return false;
00168   }
00170   bool remove(TYPE* val)    {
00171     Data::iterator i = m_data.begin();
00172     Keys::iterator j = m_keys.begin();
00173     for ( ; i != m_data.end(); i++, j++ )    {
00174       if ( isEqual((*i),val) )    {
00175         m_data.erase(i);
00176         m_keys.erase(j);
00177         val->release();
00178         return true;
00179       }
00180     }
00181     return false;
00182   }
00183   iterator begin()    {
00184     return m_data.begin();
00185   }
00186   const_iterator begin()  const   {
00187     return m_data.begin();
00188   }
00189   iterator end()    {
00190     return m_data.end();
00191   }
00192   const_iterator end()  const   {
00193     return m_data.end();
00194   }
00195 };
00196 #endif // DBCNV_DBACCESSOBJ_H

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