// Common, group WinWrap
// see CW Events
// Copyright Alexander Liss

#include "event.h"


// creates an event object or opens en existing one
Event::Event( BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName, 
			 LPSECURITY_ATTRIBUTES lpEventAttributes):
attributes(1) 
{
    handle = ::CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpName);

	if(bManualReset) attributes &=(DWORD)2;

    if (is_valid_handle(handle)) 
	{
        if (lpName)
		{
            DWORD g = GetLastError();

			if(g == ERROR_ALREADY_EXISTS)
				attributes &=~(DWORD)1;

		}
        
        status = NO_ERROR;
    }
    else 
        status = GetLastError();
    
}

// opens an existing named event
Event::Event( LPCTSTR lpName, BOOL bInheritHandle, DWORD dwDesiredAccess):
attributes(0)
{
    handle = ::OpenEvent( dwDesiredAccess, bInheritHandle, lpName);

    if (is_valid_handle(handle)) 
        status = NO_ERROR;
    else 
        status = GetLastError();
}


BOOL Event::set() 
{
	if(!is_valid_handle(handle)) return FALSE;
    return ::SetEvent(handle);
}

BOOL Event::reset() 
{
	if(!is_valid_handle(handle)) return FALSE;
    return ::ResetEvent(handle);
}

BOOL Event::pulse() 
{
	if(!is_valid_handle(handle)) return FALSE;
    return ::PulseEvent(handle);
}