00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbBase/CdbCPtr.hh"
00012
00013 #include <assert.h>
00014 #include <string.h>
00015 #include <iostream>
00016
00017 template < class P, class CLOSE_POLICY >
00018 CdbCPtr<P,CLOSE_POLICY>::CdbCPtr( P* ptr ) :
00019 _myCounter(0)
00020 {
00021 if( ptr ) _myCounter = new Counter( ptr );
00022 }
00023
00024 template < class P, class CLOSE_POLICY >
00025 CdbCPtr<P,CLOSE_POLICY>::~CdbCPtr( )
00026 {
00027 release( );
00028 }
00029
00030 template < class P, class CLOSE_POLICY >
00031 CdbCPtr<P,CLOSE_POLICY>::CdbCPtr( const CdbCPtr<P,CLOSE_POLICY>& thePtr )
00032 {
00033 acquire( thePtr._myCounter );
00034 }
00035
00036 template < class P, class CLOSE_POLICY >
00037 CdbCPtr<P,CLOSE_POLICY>&
00038 CdbCPtr<P,CLOSE_POLICY>::operator=( const CdbCPtr<P,CLOSE_POLICY>& thePtr )
00039 {
00040 if( this != &thePtr ) {
00041 release( );
00042 acquire( thePtr._myCounter );
00043 }
00044 return *this;
00045 }
00046
00047 template < class P, class CLOSE_POLICY >
00048 CdbCPtr<P,CLOSE_POLICY>&
00049 CdbCPtr<P,CLOSE_POLICY>::operator=( P* ptr )
00050 {
00051 release( );
00052 if( ptr ) _myCounter = new Counter( ptr );
00053 return *this;
00054 }
00055
00056 template < class P, class CLOSE_POLICY >
00057 P&
00058 CdbCPtr<P,CLOSE_POLICY>::operator*( ) const
00059 {
00060 return *_myCounter->_ptr;
00061 }
00062
00063 template < class P, class CLOSE_POLICY >
00064 P*
00065 CdbCPtr<P,CLOSE_POLICY>::operator->( ) const
00066 {
00067 return _myCounter->_ptr;
00068 }
00069
00070 template < class P, class CLOSE_POLICY >
00071 bool
00072 CdbCPtr<P,CLOSE_POLICY>::operator==( const CdbCPtr& thePtr ) const
00073 {
00074 return ( _myCounter ? _myCounter->_ptr : 0 ) ==
00075 ( thePtr._myCounter ? thePtr._myCounter->_ptr : 0 );
00076 }
00077
00078 template < class P, class CLOSE_POLICY >
00079 bool
00080 CdbCPtr<P,CLOSE_POLICY>::operator!=( const CdbCPtr& thePtr ) const
00081 {
00082 return ! operator==( thePtr );
00083 }
00084
00085 template < class P, class CLOSE_POLICY >
00086 bool
00087 CdbCPtr<P,CLOSE_POLICY>::operator==( const P* ptr ) const
00088 {
00089 return ( _myCounter ? _myCounter->_ptr : 0 ) == ptr;
00090 }
00091
00092 template < class P, class CLOSE_POLICY >
00093 bool
00094 CdbCPtr<P,CLOSE_POLICY>::operator!=( const P* ptr ) const
00095 {
00096 return ! operator==( ptr );
00097 }
00098
00099 template < class P, class CLOSE_POLICY >
00100 bool
00101 CdbCPtr<P,CLOSE_POLICY>::isNull( ) const
00102 {
00103 return operator==((const P*) 0 );
00104 }
00105
00106 template < class P, class CLOSE_POLICY >
00107 P*
00108 CdbCPtr<P,CLOSE_POLICY>::get( ) const
00109 {
00110 return _myCounter ? _myCounter->_ptr : 0;
00111 }
00112
00113 template < class P, class CLOSE_POLICY >
00114 bool
00115 CdbCPtr<P,CLOSE_POLICY>::unique( ) const
00116 {
00117 return _myCounter ? 1 == _myCounter->_count : true;
00118 }
00119
00120 template < class P, class CLOSE_POLICY >
00121 void
00122 CdbCPtr<P,CLOSE_POLICY>::acquire( CdbCPtr<P,CLOSE_POLICY>::Counter* theCounter )
00123 {
00124 _myCounter = theCounter;
00125 if( 0 != _myCounter ) ++(_myCounter->_count);
00126 }
00127
00128 template < class P, class CLOSE_POLICY >
00129 void
00130 CdbCPtr<P,CLOSE_POLICY>::release( )
00131 {
00132 if( _myCounter ) {
00133
00134 --(_myCounter->_count);
00135 if( 0 == _myCounter->_count ) {
00136
00137
00138
00139
00140 CLOSE_POLICY::close( _myCounter->_ptr );
00141
00142 deleteObject( _myCounter->_ptr );
00143 delete _myCounter;
00144 }
00145 _myCounter = 0;
00146 }
00147 }
00148
00149 template < class P, class CLOSE_POLICY >
00150 CdbCPtr<P,CLOSE_POLICY>::Counter::Counter( P* ptr, unsigned count ) :
00151 _ptr(ptr),
00152 _count(count)
00153 {
00154 }
00155
00156
00157
00158