00001 #ifndef CORELIB___GUARD__HPP
00002 #define CORELIB___GUARD__HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <corelib/ncbidbg.hpp>
00037
00038
00039 BEGIN_NCBI_SCOPE
00040
00041
00042
00043
00044
00045
00046
00047 template <class Resource>
00048 struct SSimpleLock
00049 {
00050 typedef Resource resource_type;
00051 void operator()(resource_type& resource) const
00052 {
00053 resource.Lock();
00054 }
00055 };
00056
00057
00058
00059
00060
00061
00062
00063 template <class Resource>
00064 struct SSimpleUnlock
00065 {
00066 typedef Resource resource_type;
00067 void operator()(resource_type& resource) const
00068 {
00069 resource.Unlock();
00070 }
00071 };
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 enum EEmptyGuard {
00094 eEmptyGuard
00095 };
00096
00097 template <class Resource,
00098 class Lock = SSimpleLock<Resource>,
00099 class Unlock = SSimpleUnlock<Resource> >
00100 class CGuard
00101 {
00102 public:
00103 typedef Resource resource_type;
00104 typedef resource_type* resource_ptr;
00105 typedef Lock lock_type;
00106 typedef Unlock unlock_type;
00107 typedef CGuard<Resource, Lock, Unlock> TThisType;
00108
00109 explicit CGuard(EEmptyGuard )
00110 {
00111 }
00112
00113
00114 explicit CGuard(resource_type& resource)
00115 {
00116 Guard(resource);
00117 }
00118
00119 explicit CGuard(EEmptyGuard ,
00120 const lock_type& lock,
00121 const unlock_type& unlock = unlock_type())
00122 : m_Data(lock, pair_base_member<unlock_type, resource_ptr>(unlock, 0))
00123 {
00124 }
00125
00126
00127 explicit CGuard(resource_type& resource,
00128 const lock_type& lock,
00129 const unlock_type& unlock = unlock_type())
00130 : m_Data(lock, pair_base_member<unlock_type, resource_ptr>(unlock, 0))
00131 {
00132 Guard(resource);
00133 }
00134
00135
00136 ~CGuard()
00137 {
00138 try {
00139 Release();
00140 } catch(std::exception& ) {
00141
00142 }
00143 }
00144
00145
00146 void Release()
00147 {
00148 if ( GetResource() ) {
00149 GetUnlock()(*GetResource());
00150 GetResource() = 0;
00151 }
00152 }
00153
00154
00155 void Guard(resource_type& resource)
00156 {
00157 Release();
00158 GetLock()(resource);
00159 GetResource() = &resource;
00160 }
00161
00162 protected:
00163 resource_ptr& GetResource(void)
00164 {
00165 return m_Data.second().second();
00166 }
00167 lock_type& GetLock(void)
00168 {
00169 return m_Data.first();
00170 }
00171 unlock_type& GetUnlock(void)
00172 {
00173 return m_Data.second().first();
00174 }
00175
00176 private:
00177
00178 pair_base_member<lock_type,
00179 pair_base_member<unlock_type,
00180 resource_ptr> > m_Data;
00181
00182 private:
00183
00184 CGuard(const CGuard<resource_type, lock_type, unlock_type>&);
00185 void operator=(const CGuard<resource_type, lock_type, unlock_type>&);
00186 };
00187
00188
00189
00190
00191
00192
00193
00194 class CNoLock
00195 {
00196 public:
00197 typedef CGuard<CNoLock> TReadLockGuard;
00198 typedef CGuard<CNoLock> TWriteLockGuard;
00199
00200 void Lock (void) const {}
00201 void Unlock(void) const {}
00202 };
00203
00204
00205 END_NCBI_SCOPE
00206
00207 #endif // CORELIB___GUARD__HPP
00208
00209