Implementation of the Handler Chain functions, HandlerChain_SendMessage(), HandlerChain_AddWindow(), HandlerChain_RemoveWindow(), and HandlerChain_ToString(), as used in the HandlerChain Pattern. More...
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "helpers/mutex.h"
#include "helpers/uintarray.h"
#include "HandlerChain_MessageWindow.h"
#include "HandlerChain_HandlerFunctions.h"
Go to the source code of this file.
Functions | |
static void | _DestroyMutex (void) |
An atexit() handler to destroy the mutex on program exit. | |
static bool | _CreateMutex (void) |
Helper function to create the mutex, if not already created. | |
static void | _LockMutex (void) |
Helper function to lock the mutex. | |
static void | _UnlockMutex (void) |
Helper function to unlock the mutex. | |
void | HandlerChain_SendMessage (int windowId, Message *message) |
Send a message to each of the handlers in the list, protected by a multi-threading lock. | |
bool | HandlerChain_AddWindow (int windowId) |
Add an instance of a MessageWindow to end of the list of windows, protected by a multi-threading lock. | |
void | HandlerChain_RemoveWindow (int windowId) |
Remove an instance of a MessageWindow from the list, protected by a multi-threading lock. | |
bool | HandlerChain_ToString (DynamicString *output) |
Convert the Handler Chain to a string, protected by a multi-threading lock. | |
Variables | |
static Mutex | _mutex |
A mutex used to protect the handler list from cross-thread corruption. | |
static bool | _mutex_initialized = false |
A flag to indicate whether the mutex has been initialized and is ready for use. | |
static UIntArray | _handleList = { 0 } |
List of IDs of MessageWindow objects that can be sent messages. | |
Implementation of the Handler Chain functions, HandlerChain_SendMessage(), HandlerChain_AddWindow(), HandlerChain_RemoveWindow(), and HandlerChain_ToString(), as used in the HandlerChain Pattern.
Definition in file HandlerChain_HandlerFunctions.c.
|
static |
Helper function to create the mutex, if not already created.
Definition at line 51 of file HandlerChain_HandlerFunctions.c.
References _DestroyMutex(), _mutex, _mutex_initialized, and mutex_create().
Referenced by _LockMutex(), and _UnlockMutex().
|
static |
An atexit() handler to destroy the mutex on program exit.
Definition at line 37 of file HandlerChain_HandlerFunctions.c.
References _mutex, _mutex_initialized, and mutex_destroy().
Referenced by _CreateMutex().
|
static |
Helper function to lock the mutex.
Definition at line 67 of file HandlerChain_HandlerFunctions.c.
References _CreateMutex(), _mutex, and mutex_lock().
Referenced by HandlerChain_AddWindow(), HandlerChain_RemoveWindow(), HandlerChain_SendMessage(), and HandlerChain_ToString().
|
static |
Helper function to unlock the mutex.
Definition at line 78 of file HandlerChain_HandlerFunctions.c.
References _CreateMutex(), _mutex, and mutex_unlock().
Referenced by HandlerChain_AddWindow(), HandlerChain_RemoveWindow(), HandlerChain_SendMessage(), and HandlerChain_ToString().
bool HandlerChain_AddWindow | ( | int | windowId | ) |
Add an instance of a MessageWindow to end of the list of windows, protected by a multi-threading lock.
If a MessageWindow is already in the list, it is not added again.
windowId | ID of the MessageWindow to add. |
Definition at line 136 of file HandlerChain_HandlerFunctions.c.
References _handleList, _LockMutex(), _UnlockMutex(), UIntArray_AddInt(), and UIntArray_Find().
Referenced by MessageWindow_Create().
void HandlerChain_RemoveWindow | ( | int | windowId | ) |
Remove an instance of a MessageWindow from the list, protected by a multi-threading lock.
If the MessageWindow is not in the list, the request to remove is ignored.
windowId | ID of the MessageWindow to remove. |
Definition at line 162 of file HandlerChain_HandlerFunctions.c.
References _handleList, _LockMutex(), _UnlockMutex(), UIntArray_Find(), and UIntArray_RemoveInt().
Referenced by _HandleDestroyMessage().
void HandlerChain_SendMessage | ( | int | windowId, |
Message * | message | ||
) |
Send a message to each of the handlers in the list, protected by a multi-threading lock.
windowId | ID of window to target message with. -1 to target all windows. |
message | The Message object to send to each handler. |
Definition at line 105 of file HandlerChain_HandlerFunctions.c.
References _handleList, _LockMutex(), _UnlockMutex(), UIntArray::data, UIntArray::length, MessageWindow_ProcessMessage(), UIntArray_Clear(), and UIntArray_Copy().
Referenced by _HandleButtonUpMessage(), _HandleChain_DestroyWindows(), _HandleCloseMessage(), and HandlerChain_Exercise().
bool HandlerChain_ToString | ( | DynamicString * | output | ) |
Convert the Handler Chain to a string, protected by a multi-threading lock.
output | Returns a string representation of the Handler Chain and all the handlers it contains. |
Definition at line 176 of file HandlerChain_HandlerFunctions.c.
References _handleList, _LockMutex(), _UnlockMutex(), UIntArray::data, DynamicString_Append(), DynamicString_Clear(), UIntArray::length, MessageWindow_ToString(), DynamicString::string, UIntArray_Clear(), and UIntArray_Copy().
Referenced by _ShowHandlerChain().
|
static |
List of IDs of MessageWindow objects that can be sent messages.
Definition at line 95 of file HandlerChain_HandlerFunctions.c.
Referenced by HandlerChain_AddWindow(), HandlerChain_RemoveWindow(), HandlerChain_SendMessage(), and HandlerChain_ToString().
|
static |
A mutex used to protect the handler list from cross-thread corruption.
Definition at line 21 of file HandlerChain_HandlerFunctions.c.
Referenced by _CreateMutex(), _DestroyMutex(), _LockMutex(), and _UnlockMutex().
|
static |
A flag to indicate whether the mutex has been initialized and is ready for use.
Definition at line 26 of file HandlerChain_HandlerFunctions.c.
Referenced by _CreateMutex(), and _DestroyMutex().