// Copyright (c) 2013 by István Váradi // This file is part of VSCPL, a simple cross-platform utility library // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. #ifndef HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H #define HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H //------------------------------------------------------------------------------ #include "EventFailable.h" #include //------------------------------------------------------------------------------ namespace hu { namespace varadiistvan { namespace scpl { namespace io { //------------------------------------------------------------------------------ class Waiter; /** * Wrapper for an event. */ class Event { private: /** * The object receiving the failure codes. */ EventFailable& eventFailable; /** * The handle of the event. */ HANDLE handle; /** * The object waiting for the event. */ Waiter* waiter; public: /** * Construct the event. */ Event(EventFailable& eventFailable); /** * Destroy the event. It will be removed from the waiter if it is * in one. */ ~Event(); /** * Get the handle of the event. */ HANDLE getHandle() const; /** * Get the waiter. */ Waiter* getWaiter() const; /** * Register the event with the given waiter. If it is already * registered with a waiter, it will be removed from that. */ void addTo(Waiter& w); /** * Remove the object from the waiter it is registered with. */ void removeFromWaiter(); /** * Determine if the event is being waited for, i.e. it is * associated with a waiter. */ bool isWaited() const; /** * Set the event. */ void fire(); /** * Determine if the event is fired or not. */ bool isFired() const; /** * Clear the event. * * @return if clearing succeeded. */ bool clear(); protected: /** * Set the error code on the failable object. */ void setErrorCode(errorCode_t errorCode); }; //------------------------------------------------------------------------------ // Inline definitions //------------------------------------------------------------------------------ inline Event::Event(EventFailable& eventFailable) : eventFailable(eventFailable), handle(CreateEvent(0, true, false, 0)), waiter(0) { if (handle==0) eventFailable.setErrorCode(GetLastError()); } //------------------------------------------------------------------------------ inline Event::~Event() { removeFromWaiter(); CloseHandle(handle); } //------------------------------------------------------------------------------ inline HANDLE Event::getHandle() const { return handle; } //------------------------------------------------------------------------------ inline Waiter* Event::getWaiter() const { return waiter; } //------------------------------------------------------------------------------ inline bool Event::isWaited() const { return waiter!=0; } //------------------------------------------------------------------------------ inline void Event::fire() { if (!SetEvent(handle)) { eventFailable.setErrorCode(GetLastError()); } } //------------------------------------------------------------------------------ inline bool Event::clear() { if (!ResetEvent(handle)) { eventFailable.setErrorCode(GetLastError()); return false; } else { return true; } } //------------------------------------------------------------------------------ inline void Event::setErrorCode(errorCode_t errorCode) { eventFailable.setErrorCode(errorCode); } //------------------------------------------------------------------------------ } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */ //------------------------------------------------------------------------------ #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_EVENT_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: