[edit]
SORRY - this was supposed to be a reply to the previous thread...
[/edit]
First the INTERFACE
===================
Then, a real CLASS DEFINITION (still platform independent)
==========================================================
At last, a Win32 IMPLEMENTATION
===============================
SORRY - this was supposed to be a reply to the previous thread...
[/edit]
First the INTERFACE
===================
- Code:
class iCriticalSection // Pure virtual interface...
{
public:
virtual bool Init( void ) = 0;
virtual void Close( void ) = 0;
virtual bool Enter( bool bFailIf ) = 0;
virtual void Leave( void ) = 0;
};
Then, a real CLASS DEFINITION (still platform independent)
==========================================================
- Code:
class CriticalSection : public iCriticalSection
{
CriticalSection( iMemMgr* pMemMgr );
~CriticalSection( void );
public:
virtual bool Init( void );
virtual void Close( void );
virtual void Enter( void );
virtual void Leave( void );
protected:
iMemMgr* m_pMemMgr; // Any kind of mem-manager will do...
void* m_pCriticalSectionObj;
};
At last, a Win32 IMPLEMENTATION
===============================
- Code:
CriticalSection::CriticalSection( iMemMgr* pMemMgr )
:
iCriticalSection()
{
m_pMemMgr = pMemMgr;
m_pCriticalSectionObj = NULL;
}
CriticalSection::~CriticalSection( void )
{
if( m_pCriticalSectionObj )
m_pMemMgr->Free( m_pCriticalSectionObj );
}
bool CriticalSection::Init( void )
{
if( !m_pCriticalSectionObj )
{
myAssert( m_pMemMgr );
m_pCriticalSectionObj = m_pMemMgr->Alloc( sizeof( CRITICAL_SECTION ) );
}
myAssert( m_pCriticalSectionObj );
::InitializeCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
return true;
}
void CriticalSection::Close( void )
{
myAssert( m_pCriticalSectionObj );
::DeleteCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}
void CriticalSection::Enter( void )
{
myAssert( m_pCriticalSectionObj );
::EnterCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}
void CriticalSection::Leave( void )
{
myAssert( m_pCriticalSectionObj );
::LeaveCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}