Design Pattern Examples
Overview of object-oriented design patterns
Mediator_GroupList.c
Go to the documentation of this file.
1
5
6#include <string.h>
7
9
11// GroupList_Initialize()
14{
15 if (groupList != NULL)
16 {
17 groupList->groups = NULL;
18 groupList->groups_count = 0;
19 groupList->allocated_count = 0;
20 }
21}
22
24// GroupList_Initialize()
26void GroupList_Clear(GroupList* groupList)
27{
28 if (groupList != NULL)
29 {
30 for (size_t index = 0; index < groupList->groups_count; index++)
31 {
32 Group_Destroy(groupList->groups[index]);
33 }
34 free(groupList->groups);
35 GroupList_Initialize(groupList);
36 }
37}
38
40// GroupList_Initialize()
42bool GroupList_AddGroup(GroupList* groupList, Group* group)
43{
44 bool added = false;
45
46 if (groupList != NULL && group != NULL)
47 {
48 Group** new_list = NULL;
49 if (groupList->groups == NULL)
50 {
51 new_list = calloc(1, sizeof(Group*));
52 groupList->allocated_count = 1;
53 }
54 else if (groupList->groups_count < groupList->allocated_count)
55 {
56 new_list = groupList->groups;
57 }
58 else
59 {
60 size_t new_count = groupList->allocated_count + 1;
61 new_list = realloc(groupList->groups, new_count * sizeof(Group*));
62 groupList->allocated_count = new_count;
63 }
64 if (new_list != NULL)
65 {
66 groupList->groups = new_list;
67 groupList->groups[groupList->groups_count] = group;
68 groupList->groups_count++;
69 added = true;
70 }
71 }
72 return added;
73}
74
76// GroupList_Initialize()
78int GroupList_FindGroup(GroupList* groupList, const char* groupName)
79{
80 int foundIndex = -1;
81
82 if (groupList != NULL && groupName != NULL)
83 {
84 for (size_t index = 0; index < groupList->groups_count; index++)
85 {
86 if (strcmp(groupName, groupList->groups[index]->Name) == 0)
87 {
88 foundIndex = (int)index;
89 break;
90 }
91 }
92 }
93
94 return foundIndex;
95}
96
98// GroupList_Initialize()
100void GroupList_RemoveGroup(GroupList* groupList, int removeIndex)
101{
102 if (groupList != NULL && groupList->groups != NULL)
103 {
104 if (removeIndex >= 0 && (size_t)removeIndex < groupList->groups_count)
105 {
106 Group_Destroy(groupList->groups[removeIndex]);
107 for (size_t groupIndex = removeIndex; groupIndex < groupList->allocated_count - 1; groupIndex++)
108 {
109 groupList->groups[groupIndex] = groupList->groups[groupIndex + 1];
110 }
111 groupList->groups_count--;
112 }
113 }
114}
void Group_Destroy(Group *group)
Release the memory of the specified Group object. After this function returns, the pointer to the Gro...
bool GroupList_AddGroup(GroupList *groupList, Group *group)
Add the specified Group object to the specified GroupList object. The list takes ownership of the Gro...
void GroupList_RemoveGroup(GroupList *groupList, int removeIndex)
Remove the group at the specified index in the given GroupList object.
void GroupList_Initialize(GroupList *groupList)
Initialize the given GroupList object for initial use.
void GroupList_Clear(GroupList *groupList)
Clear the given GroupList object to release all associated resources.
int GroupList_FindGroup(GroupList *groupList, const char *groupName)
Search the given GroupList object for the specified group by name and return the index of the group i...
Declaration of the GroupList structure and the associated support functions as used in the Mediator P...
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
const char * Name
Name of this group.
Represents a list of groups. Call GroupList_Initialize() to start and GroupList_Clear() to release al...
size_t groups_count
Number of pointers in the groups list.
Group ** groups
Array of pointers to Group objects.
size_t allocated_count
Size of the allocated groups list.