-
Notifications
You must be signed in to change notification settings - Fork 246
/
13.37.cpp
274 lines (220 loc) · 6.45 KB
/
13.37.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <string>
#include <set>
#include <iostream>
/** Message.h **/
class Message {
friend class Folder;
friend void swap(Message &, Message &);
friend void swap(Folder &, Folder &);
public:
explicit Message(const std::string &s = "") : contents(s), folders() {}
Message(const Message &);
Message &operator=(const Message &);
~Message();
void save(Folder &);
void remove(Folder &);
void print();
private:
std::string contents;
std::set<Folder *> folders;
void add_to_folders(const Message &);
void remove_from_folders();
void addFdr(Folder *);
void remFdr(Folder *);
};
void swap(Message &, Message &);
/** Folder.h **/
class Folder {
friend class Message;
friend void swap(Message &, Message &);
friend void swap(Folder &, Folder &);
public:
Folder() = default;
Folder(const Folder &);
Folder &operator=(const Folder &);
~Folder();
void save(Message &);
void remove(Message &);
void print();
private:
std::set<Message *> messages;
void addMsg(Message *);
void remMsg(Message *);
};
void swap(Folder &, Folder &);
/** Message.cpp **/
void swap(Message &lhs, Message &rhs) {
using std::swap;
for (auto &fp : lhs.folders)
fp->remMsg(&lhs);
for (auto &fp : rhs.folders)
fp->remMsg(&rhs);
swap(lhs.contents, rhs.contents);
swap(lhs.folders, rhs.folders);
for (auto &fp : lhs.folders)
fp->addMsg(&lhs);
for (auto &fp : rhs.folders)
fp->addMsg(&rhs);
}
void Message::addFdr(Folder *fp) {
folders.insert(fp);
}
void Message::remFdr(Folder *fp) {
folders.erase(fp);
}
void Message::save(Folder &f) {
folders.insert(&f);
f.addMsg(this);
}
void Message::remove(Folder &f) {
folders.erase(&f);
f.remMsg(this);
}
void Message::add_to_folders(const Message &m) {
// This function can be without parameter.
for (auto &fp : m.folders)
fp->addMsg(this);
}
void Message::remove_from_folders() {
for (auto &fp : folders)
fp->remMsg(this);
folders.clear(); // This statement is not needed.
}
Message::Message(const Message &m) : contents(m.contents), folders(m.folders) {
add_to_folders(m);
}
Message &Message::operator=(const Message &rhs) {
remove_from_folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_folders(rhs);
return *this;
}
Message::~Message() {
remove_from_folders();
}
void Message::print() {
std::cout << "Message <" << this << ">:<" << contents << "> in "
<< folders.size() << " folders:\n";
for (const auto &fp : folders)
std::cout << "\t<" << fp << ">\n";
}
/** Folder.cpp **/
void swap(Folder &lhs, Folder &rhs){
using std::swap;
for (auto &mp : lhs.messages)
mp->remFdr(&lhs);
for (auto &mp : rhs.messages)
mp->remFdr(&rhs);
swap(lhs.messages, rhs.messages);
for (auto &mp : lhs.messages)
mp->addFdr(&lhs);
for (auto &mp : rhs.messages)
mp->addFdr(&rhs);
}
void Folder::addMsg(Message *mp) {
messages.insert(mp);
}
void Folder::remMsg(Message *mp) {
messages.erase(mp);
}
void Folder::save(Message &m) {
messages.insert(&m);
m.addFdr(this);
}
void Folder::remove(Message &m) {
messages.erase(&m);
m.remFdr(this);
}
Folder::Folder(const Folder &f) : messages(f.messages) {
for (auto &mp : messages)
mp->addFdr(this);
}
Folder &Folder::operator=(const Folder &rhs) {
for (auto &mp : messages)
mp->remFdr(this);
messages = rhs.messages;
for (auto &mp : messages)
mp->addFdr(this);
return *this;
}
Folder::~Folder() {
for (auto &mp : messages)
mp->remFdr(this);
}
void Folder::print() {
std::cout << "Folder <" << this << "> contains " << messages.size()
<< " messages:\n";
for (const auto &mp : messages)
std::cout << "\t<" << mp << ">:<" << mp->contents << ">\n";
}
/** Test.cpp **/
int main() {
Folder f1, f2;
Message m1("m1"), m2("m2"), m3("m3"), m4("m4"), m5("m5");
m1.save(f1); m2.save(f1); m5.save(f1);
m3.save(f2); m4.save(f2); m5.save(f2);
std::cout << "\n===== Message save =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
{
Message m6(m5);
std::cout << "\n===== Message copy-constructor =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m5.print(); m6.print();
m6 = m1;
std::cout << "\n===== Message copy-assignment operator =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m5.print(); m6.print();
}
std::cout << "\n===== Message destructor =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m5.print();
swap(m1, m3);
std::cout << "\n===== Message swap =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m3.print();
swap(m1, m3); // swap back
swap(m1, m1);
std::cout << "\n===== Message swap self =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print();
m1.remove(f1); m2.remove(f1); m5.remove(f1);
m3.remove(f2); m4.remove(f2); m5.remove(f2);
std::cout << "\n===== Message remove =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
f1.save(m1); f1.save(m2); f1.save(m5);
f2.save(m3); f2.save(m4); f2.save(m5);
std::cout << "\n===== Folder save =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
{
Folder f3(f1);
std::cout << "\n===== Folder copy-constructor =====\n";
f1.print(); f2.print(); f3.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
f3 = f2;
std::cout << "\n===== Folder copy-assignment operator =====\n";
f1.print(); f2.print(); f3.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
}
std::cout << "\n===== Folder destructor =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
swap(f1, f2);
std::cout << "\n===== Folder swap =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
swap(f1, f2); // swap back
swap(f1, f1);
std::cout << "\n===== Folder swap self =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
f1.remove(m1); f1.remove(m2); f1.remove(m5);
f2.remove(m3); f2.remove(m4); f2.remove(m5);
std::cout << "\n===== Folder remove =====\n";
f1.print(); f2.print(); std::cout << std::endl;
m1.print(); m2.print(); m3.print(); m4.print(); m5.print();
return 0;
}