Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Users.c
Go to the documentation of this file.
1
5
6#include "Mediator_UserList.h"
7#include "Mediator_Users.h"
8
12static UserList _users = { 0 };
13
14//-----------------------------------------------------------------------------
15
17// Users_Clear()
19void Users_Clear(void)
20{
22}
23
25// Users_AddUser()
27UserErrorCode Users_AddUser(const char* userName)
28{
30
31 if (userName != NULL)
32 {
33 errorCode = UserErrorCode_No_Memory;
34 User* newUser = User_Create(userName);
35 if (newUser != NULL)
36 {
37 if (UserList_AddUser(&_users, newUser))
38 {
39 errorCode = UserErrorCode_No_Error;
40 }
41 }
42 }
43
44 return errorCode;
45}
46
48// Users_RemoveUser()
50UserErrorCode Users_RemoveUser(const char* userName)
51{
53
54 if (userName != NULL)
55 {
57 int foundIndex = UserList_FindUser(&_users, userName);
58 if (foundIndex != -1)
59 {
60 UserList_RemoveUser(&_users, foundIndex);
61 errorCode = UserErrorCode_No_Error;
62 }
63 }
64
65 return errorCode;
66}
67
69// Users_FindUser()
71User* Users_FindUser(const char* userName)
72{
73 User* user = NULL;
74
75 if (userName != NULL)
76 {
77 int foundIndex = UserList_FindUser(&_users, userName);
78 if (foundIndex != -1)
79 {
80 user = _users.users[foundIndex];
81 }
82 }
83
84 return user;
85}
86
87
89// Users_FindUser()
92{
94
95 if (userNames != NULL)
96 {
97 for (size_t index = 0; index < _users.users_count; index++)
98 {
99 User* user = _users.users[index];
100 if (!StringList_AddString(userNames, user->Name))
101 {
102 errorCode = UserErrorCode_No_Memory;
103 break;
104 }
105 }
106 }
107
108 return errorCode;
109}
User * User_Create(const char *userName)
Create a User object with the specified name.
Definition: Mediator_User.c:17
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...
Declaration of the UserList structure and the associated support functions as used in the Mediator Pa...
void Users_Clear(void)
Release all memory associated with the list of users.
User * Users_FindUser(const char *userName)
Find a user from the list of users given the user's name.
static UserList _users
The list of users.
UserErrorCode Users_RemoveUser(const char *userName)
Remove a user from the list of users.
UserErrorCode Users_GetAllUsers(StringList *userNames)
Retrieve a list of all users.
UserErrorCode Users_AddUser(const char *userName)
Add a user to the list of users.
The front end of the Users sub-system. Contains the declarations of the support functions for working...
UserErrorCode
Represents error codes that can be returned from the User functions used in the Mediator Pattern.
@ UserErrorCode_No_Error
Indicates success.
@ UserErrorCode_User_Does_Not_Exist
Indicates the user does not exist.
@ UserErrorCode_No_Memory
Indicates an out of memory condition.
@ UserErrorCode_Null_Argument
Indicates an argument is NULL.
std::vector< std::string > StringList
Typedef for a vector of std::string.
bool StringList_AddString(StringList *stringList, const char *string)
Add a string to the given string list.
Definition: stringlist.c:49
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.