Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_HandlerFunctions.c
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <stdbool.h>
11
12#include "helpers/mutex.h"
13#include "helpers/uintarray.h"
14
17
22
26static bool _mutex_initialized = false;
27
28
29//#############################################################################
30//#############################################################################
31//#############################################################################
32
33
37static void _DestroyMutex(void)
38{
40 {
42 _mutex_initialized = false;
43 }
44}
45
51static bool _CreateMutex(void)
52{
54 {
57 {
58 atexit(_DestroyMutex);
59 }
60 }
61 return _mutex_initialized;
62}
63
67static void _LockMutex(void)
68{
69 if (_CreateMutex())
70 {
72 }
73}
74
78static void _UnlockMutex(void)
79{
80 if (_CreateMutex())
81 {
83 }
84}
85
86
87
88//#############################################################################
89//#############################################################################
90//#############################################################################
91
95static UIntArray _handleList = { 0 };
96
97//#############################################################################
98//#############################################################################
99//#############################################################################
100
101
103// HandlerChain_SendMessage()
105void HandlerChain_SendMessage(int windowId, Message* message)
106{
107 _LockMutex();
108 UIntArray localList = { 0 };
109 bool success = UIntArray_Copy(&_handleList, &localList);
110 _UnlockMutex();
111
112 if (success)
113 {
114 for (size_t index = 0; index < localList.length; index++)
115 {
116 if (windowId == -1 || windowId == (int)localList.data[index])
117 {
118 if (MessageWindow_ProcessMessage((int)localList.data[index], message))
119 {
120 // Processing terminated
121 break;
122 }
123 }
124 }
125 }
126 else
127 {
128 printf(" Error! Out of memory condition copying the handler chain list in HandlerChain_SendMessage()!\n'");
129 }
130 UIntArray_Clear(&localList);
131}
132
134// HandlerChain_AddWindow()
136bool HandlerChain_AddWindow(int windowId)
137{
138 bool success = false;
139
140 _LockMutex();
141 int foundIndex = UIntArray_Find(&_handleList, (uint32_t)windowId);
142 if (foundIndex == -1)
143 {
144 success = UIntArray_AddInt(&_handleList, (uint32_t)windowId);
145 if (!success)
146 {
147 printf(" Error! Out of memory condition adding window handle to handler chain in HandlerChain_AddWindow()!\n");
148 }
149 }
150 else
151 {
152 printf(" Error! Unable to find window handle %d in HandlerChain_AddWindow()!\n", windowId);
153 }
154 _UnlockMutex();
155
156 return success;
157}
158
160// HandlerChain_RemoveWindow()
163{
164 _LockMutex();
165 int foundIndex = UIntArray_Find(&_handleList, (uint32_t)windowId);
166 if (foundIndex != -1)
167 {
168 UIntArray_RemoveInt(&_handleList, foundIndex);
169 }
170 _UnlockMutex();
171}
172
174// HandlerChain_ToString()
177{
178 bool success = false;
179
180 if (output != NULL)
181 {
182 _LockMutex();
183 UIntArray localList = { 0 };
184 success = UIntArray_Copy(&_handleList, &localList);
185 _UnlockMutex();
186
187 if (success)
188 {
189 for (size_t index = 0; index < _handleList.length; index++)
190 {
191 DynamicString localOutput = { 0 };
192 success = MessageWindow_ToString((int)_handleList.data[index], &localOutput);
193 if (success)
194 {
195 success = DynamicString_Append(output, " ");
196 if (success)
197 {
198 success = DynamicString_Append(output, localOutput.string);
199 }
200 if (success)
201 {
202 success = DynamicString_Append(output, "\n");
203 }
204 }
205
206 DynamicString_Clear(&localOutput);
207
208 if (!success)
209 {
210 printf(" Error! Out of memory condition converting handler chain to a string!\n");
211 break;
212 }
213 }
214 }
215 else
216 {
217 printf(" Error! Out of memory condition copying the handler chain in HandlerChain_ToString()!\n");
218 }
219 UIntArray_Clear(&localList);
220 }
221
222 return success;
223}
bool HandlerChain_AddWindow(int windowId)
Add an instance of a MessageWindow to end of the list of windows, protected by a multi-threading lock...
static void _DestroyMutex(void)
An atexit() handler to destroy the mutex on program exit.
static bool _mutex_initialized
A flag to indicate whether the mutex has been initialized and is ready for use.
static void _LockMutex(void)
Helper function to lock the mutex.
bool HandlerChain_ToString(DynamicString *output)
Convert the Handler Chain to a string, protected by a multi-threading lock.
static bool _CreateMutex(void)
Helper function to create the mutex, if not already created.
static UIntArray _handleList
List of IDs of MessageWindow objects that can be sent messages.
static void _UnlockMutex(void)
Helper function to unlock the mutex.
static Mutex _mutex
A mutex used to protect the handler list from cross-thread corruption.
void HandlerChain_RemoveWindow(int windowId)
Remove an instance of a MessageWindow from the list, protected by a multi-threading lock.
void HandlerChain_SendMessage(int windowId, Message *message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
Declaration of the Handler Chain functions, HandlerChain_SendMessage(), HandlerChain_AddWindow(),...
bool MessageWindow_ProcessMessage(int windowId, Message *message)
Pass a Message object to a window for processing.
bool MessageWindow_ToString(int windowId, DynamicString *output)
Convert the specified window to a string representation.
Declaration of the MessageWindow support functions, MessageWindow_Create(), MessageWindow_Destroy(),...
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
bool DynamicString_Append(DynamicString *string, const char *s)
Append the specified string to the DynamicString object.
Definition: dynamicstring.c:39
bool mutex_destroy(Mutex *mutex)
Destroy a previously created mutex.
Definition: mutex.c:128
bool mutex_unlock(Mutex *mutex)
Unlock a previously locked mutex.
Definition: mutex.c:197
bool mutex_create(Mutex *mutex)
Create a new mutex, which is initially not owned.
Definition: mutex.c:71
bool mutex_lock(Mutex *mutex)
Lock a previously created and unlocked mutex. This will block if the mutex is already locked by some ...
Definition: mutex.c:160
Declaration of the Mutex structure and supporting functions for working with mutexes.
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17
Represents a message sent to the windows. A message contains a type and a position.
Represents a handle to a mutex. Call mutex_create() to create the mutex and mutex_destroy() to destro...
Definition: mutex.h:16
Represents an array of 32-bit unsigned integers. The data field points to a block of memory allocated...
Definition: uintarray.h:24
uint32_t * data
Pointer to array of 32-bit unsigned integers.
Definition: uintarray.h:25
size_t length
Number of 32-bit unsigned integers actually in the data array.
Definition: uintarray.h:26
int UIntArray_Find(UIntArray *array, uint32_t value)
Search the given UIntArray object for the specified value and return the index of that found value.
Definition: uintarray.c:93
bool UIntArray_Copy(UIntArray *sourceArray, UIntArray *destinationArray)
Copy the source UIntArray to the destination UIntArray. The destination UIntArray is erased before ge...
Definition: uintarray.c:116
void UIntArray_Clear(UIntArray *array)
Clear the given UIntArray object so it can be reused again. Releases the list of integers.
Definition: uintarray.c:26
bool UIntArray_AddInt(UIntArray *array, uint32_t value)
Add an unsigned 32-bit integer to the given UIntArray object.
Definition: uintarray.c:38
void UIntArray_RemoveInt(UIntArray *array, int removeIndex)
Remove the unsigned 32-bit integer from the given UIntArray object at the given index....
Definition: uintarray.c:75
Declaration of the UIntArray structure and the supporting functions that represents an array of 32-bi...