/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #ifndef __EventDispatcher_H__ #define __EventDispatcher_H__ #ifdef __cplusplus extern "C" { #endif #include "stemobject/StemObject.h" #include "utilities/Atom.h" #include typedef struct EventDispatcher EventDispatcher; #define EventDispatcher_superclass StemObject // Called by dispatchEvent() once per registration for matching eventID. // eventID: Name of event that was dispatched. // eventData: Value of the eventData argument to the dispatchEvent() call that dispatched this event. // context: Value of the context argument passed to registerForEvent() when this callback was registered. // Return value: If any invoked callback returns true, dispatchEvent() returns true to its caller. typedef bool (* EventDispatcherCallback)(Atom eventID, void * eventData, void * context); struct EventTarget; #define EventDispatcher_ivars \ StemObject_ivars \ \ size_t targetCount; \ size_t private_ivar(targetAllocatedCount); \ struct EventTarget * targets; #define EventDispatcher_vtable(self_type) \ StemObject_vtable(self_type) stemobject_declare(EventDispatcher); EventDispatcher * EventDispatcher_create(); bool EventDispatcher_init(EventDispatcher * self); void EventDispatcher_dispose(EventDispatcher * self); // Registers callback to be called when dispatchEvent() is called with the value specified by eventID. void EventDispatcher_registerForEvent(EventDispatcher * self, Atom eventID, EventDispatcherCallback callback, void * context); // Dispatch an event to all registered listeners for the specified event ID. Returns true if any listener is // registered and returns true from its callback. bool EventDispatcher_dispatchEvent(EventDispatcher * self, Atom eventID, void * eventData); // Remove a previously registered event handler matching these arguments exactly. void EventDispatcher_unregisterForEvent(EventDispatcher * self, Atom eventID, EventDispatcherCallback callback, void * context); // Removes all registered event handlers the match the specified event ID, callback, or context pointer. void EventDispatcher_unregisterAllForEventID(EventDispatcher * self, Atom eventID); void EventDispatcher_unregisterAllForCallback(EventDispatcher * self, EventDispatcherCallback callback); void EventDispatcher_unregisterAllForContext(EventDispatcher * self, void * context); #ifdef __cplusplus } #endif #endif