Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Group_Classes.h
Go to the documentation of this file.
1
6
7#pragma once
8#ifndef __MEDIATOR_GROUP_CLASSES_H__
9#define __MEDIATOR_GROUP_CLASSES_H__
10
11#include <algorithm>
12#include <string>
13#include <vector>
14
16#include "helpers/stricmp.h"
17#include "helpers/stringlist.h"
18
20{
21
26 class Group
27 {
28 private:
32 std::string _groupName;
33
38
43 StringList::iterator _SearchForUser(const std::string& name)
44 {
45 return std::find(std::begin(_users), std::end(_users), name);
46 }
47
48 public:
53 {}
54
59 Group(std::string name)
60 : _groupName(name)
61 {
62 }
63
67 std::string Name()
68 {
69 return _groupName;
70 }
71
76 {
77 return _users;
78 }
79
86 bool ContainsUser(std::string name)
87 {
88 StringList::iterator foundIter = std::find(std::begin(_users), std::end(_users), name);
89 return foundIter != std::end(_users);
90 }
91
98 void AddUser(std::string name)
99 {
100 if (name.empty())
101 {
102 throw new Helpers::argumentnull_error("name",
103 "Must specify a user name to add it to the group.");
104 }
105
106 if (!ContainsUser(name))
107 {
108 _users.push_back(name);
109 }
110 }
111
112
118 void RemoveUser(std::string name)
119 {
120 StringList::iterator foundIter = std::find(std::begin(_users), std::end(_users), name);
121 if (foundIter != std::end(_users))
122 {
123 _users.erase(foundIter);
124 }
125 }
126
133 bool Equals(const Group& group) const
134 {
135 return (group._groupName == _groupName);
136 }
137
143 bool Equals(const std::string& name) const
144 {
145 return (name == _groupName);
146 }
147 };
148
149
150 //########################################################################
151 //########################################################################
152
153
160 {
161 private:
165 std::vector<Group> _groups;
166
167 private:
172 std::vector<Group>::iterator _SearchForGroup(const std::string& name)
173 {
174 return std::find_if(std::begin(_groups), std::end(_groups),
175 [name](const Group& g)
176 {
177 return g.Equals(name);
178 }
179 );
180 }
181
182 public:
188 {
189 StringList groupNames;
190
191 for (Group group : _groups)
192 {
193 groupNames.push_back(group.Name());
194 }
195 std::sort(std::begin(groupNames), std::end(groupNames),
196 [](const std::string& first, const std::string& second)
197 {
198 return Helpers::stricmp(first, second) < 0;
199 }
200 );
201 return groupNames;
202 }
203
204
211 Group* FindGroup(std::string name)
212 {
213 Group* foundGroup = nullptr;
214 std::vector<Group>::iterator foundIter = _SearchForGroup(name);
215 if (foundIter != std::end(_groups))
216 {
217 foundGroup = &(*foundIter);
218 }
219 return foundGroup;
220 }
221
222
229 void AddGroup(std::string name)
230 {
231 if (name.empty())
232 {
233 throw new Helpers::argumentnull_error ("name",
234 "Must specify a group name to add it to the group list.");
235 }
236 if (FindGroup(name) == nullptr)
237 {
238 _groups.push_back(Group(name));
239 }
240 }
241
242
248 void RemoveGroup(std::string name)
249 {
250 std::vector<Group>::iterator foundIter = _SearchForGroup(name);
251 if (foundIter != std::end(_groups))
252 {
253 _groups.erase(foundIter);
254 }
255 }
256 };
257
258} // end namespace
259
260#endif // __MEDIATOR_GROUP_CLASSES_H__
261
Implementation of the argumentnull_error exception.
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
bool Equals(const std::string &name) const
Determine if the given name matches this Group's name.
void RemoveUser(std::string name)
Remove a user from this group. If the user is not in the group then the operation is ignored.
void AddUser(std::string name)
Add the specified user to this group. If the user is already in the group, the operation is ignored.
std::string _groupName
Name of this group.
bool Equals(const Group &group) const
Determine if the name of the specified group matches this Group's name.
Group(std::string name)
Constructor.
bool ContainsUser(std::string name)
Determine if the specified user is in this group. This is a case- sensitive search.
StringList _users
The list of users in this group.
StringList::iterator _SearchForUser(const std::string &name)
Get an iterator pointing to the user with the specified name. Returns std::end(_users) if the user wa...
StringList Users() const
The names of users in this group (read-only).
std::string Name()
The name of the group (read-only).
void AddGroup(std::string name)
Add a group to the list using the given group name. Operation ignored if the group is already in the ...
StringList GroupNames()
The names of all groups contained in this list (read-only). The list is always sorted.
std::vector< Group > _groups
The list of groups.
void RemoveGroup(std::string name)
Remove the specified group from the list. Operation ignored if the group is not in the list.
Group * FindGroup(std::string name)
Retrieve the Group instance for the specified group name. The found group may be altered so it must p...
std::vector< Group >::iterator _SearchForGroup(const std::string &name)
Get an iterator pointing to the group with the specified name. Returns std::end(_groups) if the group...
Exception for arguments that are null.
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
int stricmp(const char *first, const char *second)
Compare two strings in a case-insensitive manner to determine their alphabetical order relative to ea...
Definition: stricmp.cpp:16
Declaration of the stricmp function, case-insensitive string comparison for narrow character strings.