www.pudn.com > ChatUseIOCP.rar > DtLocker.h
#pragma once
#define DTLOCKER
#ifndef _WINBASE_
#error "DtLocker, You must include windows.h or afx.h"
#endif
#if _WIN32_WINNT < 0x0400
#error "DtLocker can only be run on NT4 or later"
#endif
namespace Datatal
{
///
/// @class LockableObject
/// @version 1.0
/// @author Jonas Gauffin
/// @homepage http://www.gauffin.org/cpp
///
/// @brief Baseclass for a object that is lockable.
/// Should be inherited by objects that should be lockable.
///
/// @since 2003-02-30 jg File created
///
///
class DtLockableObject
{
public:
DtLockableObject(void) {}
virtual ~DtLockableObject(void) {}
/// Lock an object.
virtual void Lock() = 0;
/// Unlock an object.
virtual void Unlock() = 0;
};
///
/// @class CriticalSection
/// @version 3.0
/// @author Jonas Gauffin
/// @homepage http://www.gauffin.org/cpp
///
/// @brief A object that can be locked.
/// Inherits LockableObject
///
/// @since 2003-02-30 jg File created
/// @since 2003-06-02 jg Removed some bugs.
///
class DtCriticalSection : public DtLockableObject
{
public:
DtCriticalSection()
{
InitializeCriticalSection(&m_CritSect);
};
inline virtual ~DtCriticalSection()
{
DeleteCriticalSection(&m_CritSect);
};
/// Lock the critical section.
__inline virtual void Lock()
{
EnterCriticalSection(&m_CritSect);
};
/// Unlock the critical section.
__inline virtual void Unlock()
{
LeaveCriticalSection(&m_CritSect);
};
/// tries to enter the critical section, if not successful it returns false.
__inline virtual bool TryLock()
{
return TryEnterCriticalSection(&m_CritSect) != 0;
};
private:
CRITICAL_SECTION m_CritSect;
};
///
/// @class DtLock
/// @version 1.0
/// @author Jonas Gauffin
/// @homepage http://www.gauffin.org/cpp
///
/// @brief A helperclass that automaticly locks/unlocks a lockable object.
///
/// @since 2003-02-30 jg File created
/// @since 2003-06-02 jg Removed some bugs.
///
///
class DtLock
{
public:
/// The DtLock constructor automaticly locks a lockable object unless otherwise specified.
/// @param pLockable The object that we should lock
/// @param InitalLock Default true, set to false if you dont want to lock the object from start.
DtLock(DtLockableObject* pLockable, bool bInitialLock = true)
{
m_pLockable = pLockable;
//If we want to lock directly
if (bInitialLock)
m_pLockable->Lock();
};
/// The DtLock constructor automaticly locks a lockable object unless otherwise specified.
/// @param pLockable The object that we should lock
/// @param InitalLock Default true, set to false if you dont want to lock the object from start.
DtLock(DtLockableObject& Lockable, bool bInitialLock = true)
{
m_pLockable = &Lockable;
//If we want to lock directly
if (bInitialLock)
m_pLockable->Lock();
};
/// The destructor unlocks the object.
~DtLock()
{
m_pLockable->Unlock();
};
/// Lock the object.
__inline void Lock()
{
m_pLockable->Lock();
};
/// Unlock the object.
__inline void Unlock()
{
m_pLockable->Unlock();
};
private:
/// Pointer to the lockable object
DtLockableObject* m_pLockable;
};
}; //Namespace