Design Pattern Examples
Overview of object-oriented design patterns
Observer_NumberChangedFunctionList.c
Go to the documentation of this file.
1
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <memory.h>
12
14
16// NumberChangedFunctionList_Initialize()
19{
20 if (functionList != NULL)
21 {
22 functionList->functions = NULL;
23 functionList->functions_count = 0;
24 functionList->allocation_count = 0;
25 }
26}
27
29// NumberChangedFunctionList_Clear()
32{
33 if (functionList != NULL)
34 {
35 free(functionList->functions);
37 }
38}
39
41// NumberChangedFunctionList_Add()
44{
45 bool added = false;
46
47 if (functionList != NULL && function != NULL)
48 {
49 NumberChangedFunction* new_list = NULL;
50
51 if (functionList->functions == NULL)
52 {
53 new_list = malloc(sizeof(NumberChangedFunction));
54 functionList->allocation_count = 1;
55 }
56 else if (functionList->functions_count < functionList->allocation_count)
57 {
58 new_list = functionList->functions;
59 }
60 else
61 {
62 size_t new_count = functionList->allocation_count + 1;
63 new_list = realloc(functionList->functions, new_count * sizeof(NumberChangedFunction));
64 functionList->allocation_count = new_count;
65 }
66 if (new_list != NULL)
67 {
68 functionList->functions = new_list;
69 functionList->functions[functionList->functions_count] = function;
70 functionList->functions_count++;
71 added = true;
72 }
73 else
74 {
75 printf(" Error! Out of memory while allocating or reallocating space for the function list!\n");
76 }
77 }
78
79 return added;
80}
81
83// NumberChangedFunctionList_Find()
86{
87 int foundIndex = -1;
88
89 if (functionList != NULL && function != NULL)
90 {
91 for (size_t index = 0; index < functionList->functions_count; index++)
92 {
93 if (functionList->functions[index] == function)
94 {
95 foundIndex = (int)index;
96 break;
97 }
98 }
99 }
100
101 return foundIndex;
102}
103
105// NumberChangedFunctionList_Remove()
108{
109 if (functionList != NULL && functionIndex >= 0 && (size_t)functionIndex < functionList->functions_count)
110 {
111 for (size_t index = functionIndex; index < functionList->allocation_count - 1; index++)
112 {
113 functionList->functions[index] = functionList->functions[index + 1];
114 }
115 functionList->functions[functionList->allocation_count - 1] = NULL;
116 }
117}
118
120// NumberChangedFunctionList_Copy()
123{
124 bool copied = false;
125
126 if (sourceList != NULL && destinationList != NULL)
127 {
128 size_t arraySize = sourceList->allocation_count * sizeof(NumberChangedFunction);
129 destinationList->functions = calloc(1, arraySize);
130 if (destinationList->functions != NULL)
131 {
132 memcpy(destinationList->functions, sourceList->functions, arraySize);
133 destinationList->allocation_count = sourceList->allocation_count;
134 destinationList->functions_count = sourceList->functions_count;
135 copied = true;
136 }
137 else
138 {
139 printf(" Error! Out of memory copying a NumberChangedFunctionList!\n");
140 }
141 }
142 return copied;
143}
void NumberChangedFunctionList_Initialize(NumberChangedFunctionList *functionList)
Initialize the given function pointer list.
bool NumberChangedFunctionList_Add(NumberChangedFunctionList *functionList, NumberChangedFunction function)
Add a function pointer to the given function pointer list.
bool NumberChangedFunctionList_Copy(NumberChangedFunctionList *sourceList, NumberChangedFunctionList *destinationList)
Duplicate the given source function pointer list into the destination function pointer list.
void NumberChangedFunctionList_Remove(NumberChangedFunctionList *functionList, int functionIndex)
Removed the function pointer at the given index from the function pointer list.
int NumberChangedFunctionList_Find(NumberChangedFunctionList *functionList, NumberChangedFunction function)
Search the function list for the specified function pointer.
void NumberChangedFunctionList_Clear(NumberChangedFunctionList *functionList)
Clear the given function pointer list, releasing all associated memory. The function list can then be...
Declaration of the NumberChangedFunctionList structure along with its support functions,...
void(* NumberChangedFunction)(uint32_t)
Alias for a function that receives notifications about a number change.
Represents a dynamic list of function pointers of the type NumberChangedFunction.
size_t functions_count
Number of active function pointers in the functions array.
size_t allocation_count
The number of function pointers that can be held in the functions array.
NumberChangedFunction * functions
Array of function pointers.