///////////////////////////////////////////////////////////////////////////////
/// @file deSynchObject.cpp
///
/// @brief 
///
/// @author trajar
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Jun 2002
/// @author trajar
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#include "deSynchObject.hpp"

#ifdef	_DEBUG
 #include	<crtdbg.h> 
 #define new new(_NORMAL_BLOCK,__FILE__, __LINE__)
#endif

_inline deSynchObject::deSynchObject(void)
{
	InitializeCriticalSection( &m_csLock );
}

_inline deSynchObject::~deSynchObject(void)
{
	LeaveCriticalSection( &m_csLock );
	DeleteCriticalSection( &m_csLock );
}

_inline void deSynchObject::Lock( void * pOwner )
{
	EnterCriticalSection( &m_csLock );

	m_bLocked	= true;
	m_Owner		= pOwner;
}

_inline void deSynchObject::Unlock( void )
{
	LeaveCriticalSection( &m_csLock );

	m_bLocked	= false;
	m_Owner		= NULL;
}

_inline deSynchLocalObject::deSynchLocalObject( void )
{
	NET_LOG( "** Warning: Creating Local deSynchObject without setting parent/owner **\n" );

	m_Owner		= NULL;
	m_Object	= NULL;
	throw *this;
}

_inline deSynchLocalObject::deSynchLocalObject( void * pOwner, deSynchObject * pObj )
{
	deASSERT( pOwner );
	deASSERT( pObj );

	// set vars
	m_Owner		= pOwner;
	m_Object	= pObj;

	// lock it
	m_Object->Lock( m_Owner );
}

_inline deSynchLocalObject::~deSynchLocalObject( void )
{
	deASSERT( m_Object );

	// unlock it
	m_Object->Unlock();
}

_inline deSynchObject * deSynchLocalObject::GetParent( void )
{
	deASSERT( m_Object );
	return m_Object;
}