Design Pattern Examples
Overview of object-oriented design patterns
Mediator_User_Classes.h
Go to the documentation of this file.
1
6
7
8#pragma once
9#ifndef __MEDIATOR_USER_CLASSES_H__
10#define __MEDIATOR_USER_CLASSES_H__
11
12#include <algorithm>
13#include <string>
14#include <vector>
15
17#include "helpers/stringlist.h"
18
20{
21
25 class User
26 {
27 private:
28 std::string _name;
29
30 public:
35 {
36 }
37
42 User(std::string name)
43 : _name(name)
44 {
45 }
46
47
51 std::string Name()
52 {
53 return _name;
54 }
55
61 bool Equals(const std::string& s) const
62 {
63 return s == _name;
64 }
65
71 bool Equals(const User& user) const
72 {
73 return user._name == _name;
74 }
75 };
76
77
78 //########################################################################
79 //########################################################################
80
81
88 {
89 private:
93 std::vector<User> _users;
94
95 private:
100 std::vector<User>::iterator _SearchForUser(const std::string& name)
101 {
102 return std::find_if(std::begin(_users), std::end(_users),
103 [name](const User& u)
104 {
105 return u.Equals(name);
106 }
107 );
108 }
109
110 public:
116 {
117 StringList userNames;
118 for (User& user : _users)
119 {
120 userNames.push_back(user.Name());
121 }
122 std::sort(std::begin(userNames), std::end(userNames));
123 return userNames;
124 }
125
126
133 User* FindUser(std::string name)
134 {
135 User* foundUser = nullptr;
136
137 std::vector<User>::iterator foundIter = _SearchForUser(name);
138 if (foundIter != std::end(_users))
139 {
140 foundUser = &(*foundIter);
141 }
142 return foundUser;
143 }
144
145
152 void AddUser(std::string name)
153 {
154 if (name.empty())
155 {
156 throw new Helpers::argumentnull_error("name",
157 "Must specify a user name to add it to the user list.");
158 }
159
160 if (FindUser(name) == nullptr)
161 {
162 _users.push_back(User(name));
163 }
164 }
165
166
172 void RemoveUser(std::string name)
173 {
174 std::vector<User>::iterator foundIter = _SearchForUser(name);
175 if (foundIter != std::end(_users))
176 {
177 _users.erase(foundIter);
178 }
179 }
180 };
181
182} // end namespace
183
184#endif // __MEDIATOR_USER_CLASSES_H__
185
Implementation of the argumentnull_error exception.
Represents a user with a name.
bool Equals(const std::string &s) const
Compare a string to this User.
User(std::string name)
Constructor.
std::string Name()
The name of the user (read-only).
bool Equals(const User &user) const
Compare another User name to this User name.
void RemoveUser(std::string name)
Remove the specified user name as a user. Operation ignored if user is not in the list.
User * FindUser(std::string name)
Retrieve the User instance for the specified user name. The found user may be altered so it must poin...
void AddUser(std::string name)
Add the specified user name as a user. Operation ignored if user is already in the list.
std::vector< User > _users
The list of users.
StringList UserNames()
The user names contained in this list (read-only). The list is always sorted.
std::vector< User >::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...
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++.