Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Group.c
Go to the documentation of this file.
1
5
6#include <stdlib.h>
7#include <memory.h>
8
9#include "Mediator_Group.h"
10
12// Group_Create()
14Group* Group_Create(const char* groupName)
15{
16 Group* group = NULL;
17
18 if (groupName != NULL)
19 {
20 group = calloc(1, sizeof(Group));
21 if (group != NULL)
22 {
23 group->Name = groupName;
25 }
26 }
27
28 return group;
29}
30
32// Group_Destroy()
34void Group_Destroy(Group* group)
35{
36 if (group != NULL)
37 {
38 StringList_Clear(&group->Users);
39 free(group);
40 }
41}
42
44// Group_AddUser()
46bool Group_AddUser(Group* group, const char* userName)
47{
48 bool added = false;
49 if (group != NULL && userName != NULL)
50 {
51 added = StringList_AddString(&group->Users, userName);
52 }
53
54 return added;
55}
56
58// Group_FindUser()
60int Group_FindUser(Group* group, const char* userName)
61{
62 int foundIndex = -1;
63
64 if (group != NULL && userName != NULL)
65 {
66 foundIndex = StringList_Find(&group->Users, userName);
67 }
68
69 return foundIndex;
70}
71
73// Group_RemoveUser()
75void Group_RemoveUser(Group* group, int removeIndex)
76{
77 if (group != NULL)
78 {
79 StringList_Remove(&group->Users, removeIndex);
80 }
81}
82
83
85// Group_GetAllUsers()
88{
89 bool usersGathered = true;
90
91 if (group != NULL && users != NULL)
92 {
93 for (size_t index = 0; index < group->Users.strings_count; index++)
94 {
95 if (!StringList_AddString(users, group->Users.strings[index]))
96 {
97 usersGathered = false;
98 break;
99 }
100 }
101 }
102
103 return usersGathered;
104}
void Group_Destroy(Group *group)
Release the memory of the specified Group object. After this function returns, the pointer to the Gro...
int Group_FindUser(Group *group, const char *userName)
Search the specified group for the specified user.
void Group_RemoveUser(Group *group, int removeIndex)
Remove a user from the specified group. Does nothing if the user is not in the group.
bool Group_AddUser(Group *group, const char *userName)
Add a user to the specified group. Does nothing if the user is already in the group.
Group * Group_Create(const char *groupName)
Create a new Group object with the specified name.
bool Group_GetAllUsers(Group *group, StringList *users)
Retrieve the names of all users in the group.
Declaration of the Group structure and the associated support functions as used in the Mediator Patte...
std::vector< std::string > StringList
Typedef for a vector of std::string.
void StringList_Clear(StringList *stringList)
Clear the specified string list. All strings in the list are released. The string list can then be us...
Definition: stringlist.c:30
int StringList_Find(StringList *stringList, const char *string)
Search the given string list for the given string. If found, return the index of the found string.
Definition: stringlist.c:157
void StringList_Initialize(StringList *stringList)
Initialize the given string list.
Definition: stringlist.c:17
void StringList_Remove(StringList *stringList, int removeIndex)
Remove the specified string from the given string list.
Definition: stringlist.c:137
bool StringList_AddString(StringList *stringList, const char *string)
Add a string to the given string list.
Definition: stringlist.c:49
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
StringList Users
The list of users in this group.
const char * Name
Name of this group.