Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbCondRemote/BdbCondRCacheNode.cc

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // File and Version Information:
00004 //      $Id: BdbCondRCacheNode.cc,v 1.2 2002/06/21 18:49:54 ryd Exp $
00005 //
00006 // Description:
00007 //      This class provides a basic functionality for nodes of the hierarchical
00008 //      cache of remote servers from this package.
00009 //
00010 //      This objects of this class could not be constructed on its own.
00011 //      They must always be used as a base for the actual nodes.
00012 //
00013 // Environment:
00014 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00015 //
00016 // Author List:
00017 //      Igor A. Gaponenko       Original Author
00018 //
00019 // Copyright Information:
00020 //      Copyright (C) 2000      Lawrence Berkeley Laboratory
00021 //
00022 //-----------------------------------------------------------------------------
00023 
00024 // -------------------------
00025 // -- This Class's Header --
00026 // -------------------------
00027 
00028 #include "BdbCondRemote/BdbCondRCacheNode.hh"
00029 #include <string>
00030 using std::string;
00031 #include <vector>
00032 using std::vector;
00033 #include <map>
00034 using std::map;
00035 
00036 
00037 // -------------------------
00038 // -- Objectivity Headers --
00039 // -------------------------
00040 
00041 // -------------------
00042 // -- BaBar Headers --
00043 // -------------------
00044 
00045 // ----------------------------
00046 // -- BaBar Database Headers --
00047 // ----------------------------
00048 
00049 // ---------------
00050 // -- C Headers --
00051 // ---------------
00052 
00053 // ---------------------------------
00054 // -- Collaborating Class Headers --
00055 // ---------------------------------
00056 
00057 // --------------------------------------------------------------------------
00058 // -- Local Macros, Typedefs, Structures, Unions, and Forward Declarations --
00059 // --------------------------------------------------------------------------
00060 
00061 // --------------------
00062 // -- Static methods --
00063 // --------------------
00064 
00065 // ------------------
00066 // -- Constructors --
00067 // ------------------
00068 
00069 template<class Object>
00070 BdbCondRCacheNode<Object>::BdbCondRCacheNode( )
00071 {
00072     _cache = new map<string, Object*>;
00073 }
00074 
00075 template<class Object>
00076 BdbCondRCacheNode<Object>::BdbCondRCacheNode( const BdbCondRCacheNode<Object>& theNode )
00077 {
00078     _cache = theNode._cache;
00079 }
00080 
00081 // ----------------
00082 // -- Destructor --
00083 // ----------------
00084 
00085 template<class Object>
00086 BdbCondRCacheNode<Object>::~BdbCondRCacheNode( )
00087 {
00088   // Delete all objects from the cache following their pointers
00089   // and clear the cache.
00090 
00091     deleteObjects( );
00092 
00093   // Then delete the hash dictionary itself.
00094 
00095     delete _cache;
00096 }
00097 
00098 // ---------------
00099 // -- Operators --
00100 // ---------------
00101 
00102 template<class Object>
00103 BdbCondRCacheNode<Object>&
00104 BdbCondRCacheNode<Object>::operator=( const BdbCondRCacheNode<Object>& theNode )
00105 {
00106     if( this == &theNode ) return *this;
00107 
00108     _cache = theNode._cache;
00109 
00110     return *this;
00111 }
00112 
00113 // ---------------
00114 // -- Accessors --
00115 // ---------------
00116 
00117 // ----------------
00118 // -- Operations --
00119 // ----------------
00120 
00121 template<class Object>
00122 void
00123 BdbCondRCacheNode<Object>::keys( vector<string>& theList )
00124 {
00125   // The input list is always cleared.
00126 
00127     theList.clear( );
00128 
00129   // Traverse the cache and put all found keys into the vector.
00130 
00131     string theKeyString;
00132 
00133     map<string, Object*>::iterator theItr=_cache->begin();
00134     while( theItr!=_cache->end()) {
00135         theKeyString = theItr->first;
00136         theList.push_back( theKeyString );
00137     }
00138 }
00139 
00140 template<class Object>
00141 Object*
00142 BdbCondRCacheNode<Object>::find( const char* theKey )
00143 {
00144   // The "null is always null". We can't proceed safely for the null
00145   // pointers to the names used as keys.
00146 
00147     if( 0 == theKey ) return 0;
00148 
00149   // The default result is a null pointer.
00150 
00151     Object* theObjectPtr = 0;
00152 
00153   // Find the object pointer in the cache.
00154   //
00155   // NOTE: This operation (according to RW documentation) is expected
00156   //       to leave untouched the previous value of the pointer
00157   //       if the key was not found.
00158 
00159     string theKeyString( theKey );
00160     theObjectPtr=_cache->find( theKeyString)->second;
00161 
00162   // Return what we have found (if any).
00163 
00164     return theObjectPtr;
00165 }
00166 
00167 template<class Object>
00168 Object*
00169 BdbCondRCacheNode<Object>::add( const char* theKey,
00170                                 Object*     theObjectPtr )
00171 {
00172   // The "null is always null". We can't proceed safely for the null
00173   // pointers to the names used as keys.
00174 
00175     if( 0 == theKey ) return 0;
00176 
00177   // Find the old object pointer for this key if any.
00178 
00179     Object* theOldObjectPtr = find( theKey );
00180 
00181   // Stop right here and return null pointer if the new object pointer
00182   // is equal to old one. In that case there is no reason to continue.
00183 
00184     if(( 0 != theOldObjectPtr ) && ( theOldObjectPtr == theObjectPtr )) return 0;
00185 
00186   // Set/replace the objects pointer for specified key.
00187 
00188     string theKeyString( theKey );
00189     (*_cache)[theKeyString] = theObjectPtr;
00190 
00191   // And finally return the previous object (if any).
00192 
00193     return theOldObjectPtr;
00194 }
00195 
00196 template<class Object>
00197 void
00198 BdbCondRCacheNode<Object>::remove( const char* theKey )
00199 {
00200   // The "null is always null". We can't proceed safely for the null names
00201   // used as keys.
00202 
00203     if( 0 == theKey ) return;
00204 
00205   // Find and delete the corresponding object pointer and the key from
00206   // the cache.
00207 
00208     string theKeyString( theKey );
00209     Object*   theObjectPtr;
00210     
00211     map<string, Object*>::iterator theItr=_cache->find(theKeyString);
00212 
00213     if( theItr!=_cache->end() ) {
00214         delete theItr->second;
00215         _cache->erase( theKeyString );
00216     }
00217 }
00218 
00219 template<class Object>
00220 void
00221 BdbCondRCacheNode<Object>::clear( )
00222 {
00223   // Delete all objects from the cache following their pointers
00224   // and clear the cache.
00225 
00226     deleteObjects( );
00227 }
00228 
00229 template<class Object>
00230 void
00231 BdbCondRCacheNode<Object>::deleteObjects( )
00232 {
00233   // Delete the objects for all the pointers found in the cache.
00234 
00235     Object* theObjectPtr;
00236 
00237     map<string, Object*>::iterator theItr=_cache->begin();
00238     while( theItr!=_cache->end()) {
00239         delete theItr->second;
00240     }
00241 
00242   // Reset the cache itself.
00243 
00244     _cache->clear( );
00245 }
00246 
00247 // ---------------
00248 // -- Accessors --
00249 // ---------------
00250 
00251 /////////////////
00252 // End Of File //
00253 /////////////////

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002