00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #pragma once
00012
00013
00014
00015 #include "windows.h"
00016
00017 namespace TestToolBox
00018 {
00019
00020 template <class T>
00021 class ObjectLockT
00022 {
00023 public:
00024 ObjectLockT(T* in_pObject)
00025 {
00026 if (in_pObject)
00027 {
00028 in_pObject->Lock();
00029 }
00030 m_pObject = (in_pObject);
00031 }
00032
00033 ~ObjectLockT()
00034 {
00035 if (m_pObject )
00036 m_pObject ->Unlock();
00037 }
00038 T* m_pObject;
00039 };
00040
00041 class AutoCriticalSection
00042 {
00043 public:
00044 AutoCriticalSection() {InitializeCriticalSection(&m_sec);}
00045 ~AutoCriticalSection() {DeleteCriticalSection(&m_sec);}
00046 private:
00047 void Lock() {EnterCriticalSection(&m_sec);}
00048 void Unlock() {LeaveCriticalSection(&m_sec);}
00049 CRITICAL_SECTION m_sec;
00050 friend class AutoLock;
00051 };
00052
00053 class AutoLock
00054 {
00055 public:
00056 AutoLock(AutoCriticalSection* in_pObject)
00057 {
00058 if (in_pObject)
00059 {
00060 in_pObject->Lock();
00061 }
00062 m_pObject = (in_pObject);
00063 }
00064
00065 ~AutoLock()
00066 {
00067 if (m_pObject )
00068 m_pObject ->Unlock();
00069 }
00070 AutoCriticalSection* m_pObject;
00071 };
00072
00073 }
00074
00075