Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_HandlerFunctions.c File Reference

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"
Include dependency graph for HandlerChain_HandlerFunctions.c:

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.
 

Detailed Description

Function Documentation

◆ _CreateMutex()

static bool _CreateMutex ( void  )
static

Helper function to create the mutex, if not already created.

Returns
Returns true if the mutex was created or already exists; otherwise, returns false, the mutex is not usable.

Definition at line 51 of file HandlerChain_HandlerFunctions.c.

References _DestroyMutex(), _mutex, _mutex_initialized, and mutex_create().

Referenced by _LockMutex(), and _UnlockMutex().

◆ _DestroyMutex()

static void _DestroyMutex ( void  )
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().

◆ _LockMutex()

static void _LockMutex ( void  )
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().

◆ _UnlockMutex()

static void _UnlockMutex ( void  )
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().

◆ HandlerChain_AddWindow()

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.

Parameters
windowIdID of the MessageWindow to add.
Returns
Returns true if the handler chain was successfully added to the list; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 136 of file HandlerChain_HandlerFunctions.c.

References _handleList, _LockMutex(), _UnlockMutex(), UIntArray_AddInt(), and UIntArray_Find().

Referenced by MessageWindow_Create().

◆ HandlerChain_RemoveWindow()

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.

Parameters
windowIdID 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().

◆ HandlerChain_SendMessage()

void HandlerChain_SendMessage ( int  windowId,
Message message 
)

Send a message to each of the handlers in the list, protected by a multi-threading lock.

Parameters
windowIdID of window to target message with. -1 to target all windows.
messageThe 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().

◆ HandlerChain_ToString()

bool HandlerChain_ToString ( DynamicString output)

Convert the Handler Chain to a string, protected by a multi-threading lock.

Parameters
outputReturns a string representation of the Handler Chain and all the handlers it contains.
Returns
Returns true if the handler chain was successfully converted to a string; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

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().

Variable Documentation

◆ _handleList

UIntArray _handleList = { 0 }
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().

◆ _mutex

Mutex _mutex
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().

◆ _mutex_initialized

bool _mutex_initialized = false
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().