forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13_49_Message.cpp
205 lines (173 loc) · 4.18 KB
/
ex13_49_Message.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
#include "ex13_49_Message.h"
#include <iostream>
void swap(Message &lhs, Message &rhs)
{
using std::swap;
for (auto f : lhs.folders)
f->remMsg(&lhs);
std::cout << "Remove message from folders" << std::endl; // debug
for (auto f : rhs.folders)
f->remMsg(&rhs);
std::cout << "Remove message from folders" << std::endl; // debug
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
std::cout << "Message members swaped" << std::endl; // debug
for (auto f : lhs.folders)
f->addMsg(&lhs);
std::cout << "Added message to folders" << std::endl; // debug
for (auto f : rhs.folders)
f->addMsg(&rhs);
std::cout << "Added message to folders" << std::endl; // debug
}
Message::Message(const Message &m) : contents(m.contents), folders(m.folders)
{
add_to_Folders(m);
}
Message::~Message()
{
remove_from_Folders();
}
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)
{
for (auto f : m.folders)
f->addMsg(this);
std::cout << "Added message to folders" << std::endl; // debug
}
void Message::remove_from_Folders()
{
for (auto f : folders)
f->remMsg(this);
std::cout << "Remove message from folders" << std::endl; // debug
}
Message& Message::operator=(const Message &rhs)
{
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
std::cout << "Message members assgined" << std::endl; // debug
add_to_Folders(rhs);
return *this;
}
void Message::print_debug()
{
std::cout << contents << ": ";
for (auto f : folders)
std::cout << f->fldr() << " ";
std::cout << std::endl;
}
void Message::move_Folders(Message *m)
{
folders = std::move(m->folders);
for (auto f : folders)
{
f->remMsg(m);
f->addMsg(this);
}
m->folders.clear();
}
Message::Message(Message &&m) : contents(std::move(m.contents))
{
move_Folders(&m);
}
Message& Message::operator= (Message &&rhs)
{
if (this != &rhs)
{
remove_from_Folders();
contents = std::move(rhs.contents);
move_Folders(&rhs);
}
std::cout << "Message members moved" << std::endl; // debug
return *this;
}
// Folder Implementation
void swap(Folder &lhs, Folder &rhs)
{
using std::swap;
for (auto m : lhs.msgs)
m->remFldr(&lhs);
std::cout << "clear folder" << std::endl; // debug
for (auto m : rhs.msgs)
m->remFldr(&rhs);
std::cout << "clear folder" << std::endl; // debug
swap(lhs.name, rhs.name);
swap(lhs.msgs, rhs.msgs);
std::cout << "Folder members swaped" << std::endl; // debug
for (auto m : lhs.msgs)
m->addFldr(&lhs);
std::cout << "Added messages to folder" << std::endl; // debug
for (auto m : rhs.msgs)
m->addFldr(&rhs);
std::cout << "Added messages to folder" << std::endl; // debug
}
void Folder::add_to_Message(const Folder &f)
{
for (auto m : f.msgs)
m->addFldr(this);
std::cout << "Added messages to folder" << std::endl; // debug
}
Folder::Folder(const Folder &f) : name(f.name), msgs(f.msgs)
{
add_to_Message(f);
}
void Folder::remove_from_Message()
{
for (auto m : msgs)
m->remFldr(this);
std::cout << "clear folder" << std::endl; // debug
}
Folder::~Folder()
{
remove_from_Message();
}
Folder& Folder::operator =(const Folder &rhs)
{
remove_from_Message();
name = rhs.name;
msgs = rhs.msgs;
std::cout << "Folder members assigned" << std::endl; // debug
add_to_Message(rhs);
return *this;
}
void Folder::print_debug()
{
std::cout << name << ": ";
for (auto m : msgs)
std::cout << m->msg() << " ";
std::cout << std::endl;
}
void Folder::move_Messages(Folder *f)
{
msgs = std::move(f->msgs);
for (auto m : msgs)
{
m->remFldr(f);
m->addFldr(this);
}
f->msgs.clear();
}
Folder::Folder(Folder&& f) : name(std::move(f.name))
{
move_Messages(&f);
}
Folder& Folder::operator=(Folder &&rhs)
{
if (this != &rhs)
{
remove_from_Message();
name = std::move(rhs.name);
move_Messages(&rhs);
}
std::cout << "Message members moved" << std::endl; // debug
return *this;
}