forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex13_34_36_37.cpp
111 lines (89 loc) · 1.84 KB
/
ex13_34_36_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
#include "ex13_34_36_37.h"
#include <iostream>
void swap(Message& lhs, Message& rhs)
{
using std::swap;
lhs.remove_from_Folders();
rhs.remove_from_Folders();
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
lhs.add_to_Folders(lhs);
rhs.add_to_Folders(rhs);
}
// Message Implementation
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::save(Folder& f)
{
addFldr(&f);
f.addMsg(this);
}
void Message::remove(Folder& f)
{
remFldr(&f);
f.remMsg(this);
}
void Message::print_debug()
{
std::cout << contents << std::endl;
}
void Message::add_to_Folders(const Message& m)
{
for (auto f : m.folders) f->addMsg(this);
}
void Message::remove_from_Folders()
{
for (auto f : folders) f->remMsg(this);
}
// Folder Implementation
void swap(Folder& lhs, Folder& rhs)
{
using std::swap;
lhs.remove_from_Messages();
rhs.remove_from_Messages();
swap(lhs.msgs, rhs.msgs);
lhs.add_to_Messages(lhs);
rhs.add_to_Messages(rhs);
}
Folder::Folder(const Folder& f) : msgs(f.msgs)
{
add_to_Messages(f);
}
Folder& Folder::operator=(const Folder& rhs)
{
remove_from_Messages();
msgs = rhs.msgs;
add_to_Messages(rhs);
return *this;
}
Folder::~Folder()
{
remove_from_Messages();
}
void Folder::print_debug()
{
for (auto m : msgs) std::cout << m->contents << " ";
std::cout << std::endl;
}
void Folder::add_to_Messages(const Folder& f)
{
for (auto m : f.msgs) m->addFldr(this);
}
void Folder::remove_from_Messages()
{
for (auto m : msgs) m->remFldr(this);
}