Design Pattern Examples
Overview of object-oriented design patterns
Mediator_UserList.c
Go to the documentation of this file.
1
5
6#include <memory.h>
7#include <string.h>
8
9#include "Mediator_UserList.h"
10
12// UserList_Initialize()
15{
16 if (userList != NULL)
17 {
18 userList->users = NULL;
19 userList->users_count = 0;
20 userList->allocated_count = 0;
21 }
22}
23
25// UserList_Initialize()
27void UserList_Clear(UserList* userList)
28{
29 if (userList != NULL)
30 {
31 for (size_t index = 0; index < userList->users_count; index++)
32 {
33 User_Destroy(userList->users[index]);
34 }
35 free(userList->users);
36 UserList_Initialize(userList);
37 }
38}
39
41// UserList_Initialize()
43bool UserList_AddUser(UserList* userList, User* user)
44{
45 bool added = false;
46
47 if (userList != NULL && user != NULL)
48 {
49 User** new_list = NULL;
50 if (userList->users == NULL)
51 {
52 new_list = calloc(1, sizeof(User*));
53 userList->allocated_count = 1;
54 }
55 else if (userList->users_count < userList->allocated_count)
56 {
57 new_list = userList->users;
58 }
59 else
60 {
61 size_t new_count = userList->allocated_count + 1;
62 new_list = realloc(userList->users, new_count * sizeof(User*));
63 userList->allocated_count = new_count;
64 }
65 if (new_list != NULL)
66 {
67 userList->users = new_list;
68 userList->users[userList->users_count] = user;
69 userList->users_count++;
70 added = true;
71 }
72 }
73 return added;
74}
75
77// UserList_Initialize()
79int UserList_FindUser(UserList* userList, const char* userName)
80{
81 int foundIndex = -1;
82
83 if (userList != NULL && userName != NULL)
84 {
85 for (size_t index = 0; index < userList->users_count; index++)
86 {
87 if (strcmp(userName, userList->users[index]->Name) == 0)
88 {
89 foundIndex = (int)index;
90 break;
91 }
92 }
93 }
94
95 return foundIndex;
96}
97
99// UserList_Initialize()
101void UserList_RemoveUser(UserList* userList, int removeIndex)
102{
103 if (userList != NULL && userList->users != NULL)
104 {
105 if (removeIndex >= 0 && (size_t)removeIndex < userList->users_count)
106 {
107 User_Destroy(userList->users[removeIndex]);
108 for (size_t userIndex = removeIndex; userIndex < userList->allocated_count - 1; userIndex++)
109 {
110 userList->users[userIndex] = userList->users[userIndex + 1];
111 }
112 userList->users_count--;
113 }
114 }
115}
void User_Destroy(User *user)
Destroy the given User object, releasing any associated memory resources. After this function returns...
Definition: Mediator_User.c:41
void UserList_RemoveUser(UserList *userList, int removeIndex)
Remove the user at the specified index in the given UserList object.
bool UserList_AddUser(UserList *userList, User *user)
Add the specified User object to the specified UserList object. The list takes ownership of the User ...
void UserList_Clear(UserList *userList)
Clear the given UserList object to release all associated resources.
int UserList_FindUser(UserList *userList, const char *userName)
Search the given UserList object for the specified user by name and return the index of the user if f...
void UserList_Initialize(UserList *userList)
Initialize the given UserList object for initial use.
Declaration of the UserList structure and the associated support functions as used in the Mediator Pa...
Represents a user with a name.
Definition: Mediator_User.h:14
const char * Name
The name of the user.
Definition: Mediator_User.h:18
Represents a list of users. Call UserList_Initialize() to start and UserList_Clear() to release all r...
User ** users
Array of pointers to User objects.
size_t users_count
Number of pointers in the users list.
size_t allocated_count
Size of the allocated users list.