Skip to content

Commit

Permalink
improving 13_34_36_37(message and folder)
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed May 6, 2016
1 parent 712fc04 commit 2c3cf99
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 57 deletions.
12 changes: 6 additions & 6 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ Exercise 13.33:
Because these operations must also update the given `Folder`. Updating a `Folder` is a job that the `Folder` class controls through its `addMsg` and `remMsg` members, which will add or remove a pointer to a given `Message`, respectively.
Exercise 13.34 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp)
----------------------------------------------------------------
Exercise 13.34 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp) | [test](ex13_34_36_37_TEST.cpp)
-------------------------------------------------------------------------------------------------
Exercise 13.35:
---------------
Expand All @@ -269,11 +269,11 @@ Exercise 13.35:
some existing `Folders` will out of sync with the `Message` after assignment.
Exercise 13.36 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp)
----------------------------------------------------------------
Exercise 13.36 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp) | [test](ex13_34_36_37_TEST.cpp)
-------------------------------------------------------------------------------------------------
Exercise 13.37 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp)
----------------------------------------------------------------
Exercise 13.37 [hpp](ex13_34_36_37.h) | [cpp](ex13_34_36_37.cpp) | [test](ex13_34_36_37_TEST.cpp)
-------------------------------------------------------------------------------------------------
Exercise 13.38:
---------------
Expand Down
94 changes: 46 additions & 48 deletions ch13/ex13_34_36_37.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,108 @@
void swap(Message& lhs, Message& rhs)
{
using std::swap;
for (auto f : lhs.folders) f->remMsg(&lhs);
for (auto f : rhs.folders) f->remMsg(&rhs);
lhs.remove_from_Folders();
rhs.remove_from_Folders();

swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);

for (auto f : lhs.folders) f->addMsg(&lhs);
for (auto f : rhs.folders) f->addMsg(&rhs);
lhs.add_to_Folders(lhs);
rhs.add_to_Folders(rhs);
}

// Message Implementation

void Message::save(Folder& f)
Message::Message(const Message& m) : contents(m.contents), folders(m.folders)
{
folders.insert(&f);
f.addMsg(this);
add_to_Folders(m);
}

void Message::remove(Folder& f)
Message& Message::operator=(const Message& rhs)
{
folders.erase(&f);
f.remMsg(this);
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_Folders(rhs);
return *this;
}

void Message::add_to_Folders(const Message& m)
Message::~Message()
{
for (auto f : m.folders) f->addMsg(this);
remove_from_Folders();
}

Message::Message(const Message& m) : contents(m.contents), folders(m.folders)
void Message::save(Folder& f)
{
add_to_Folders(m);
addFldr(&f);
f.addMsg(this);
}

void Message::remove_from_Folders()
void Message::remove(Folder& f)
{
for (auto f : folders) f->remMsg(this);
remFldr(&f);
f.remMsg(this);
}

Message::~Message()
void Message::print_debug()
{
remove_from_Folders();
std::cout << contents << std::endl;
}

Message& Message::operator=(const Message& rhs)
void Message::add_to_Folders(const Message& m)
{
remove_from_Folders();
contents = rhs.contents;
folders = rhs.folders;
add_to_Folders(rhs);
return *this;
for (auto f : m.folders) f->addMsg(this);
}

void Message::print_debug()
void Message::remove_from_Folders()
{
std::cout << contents << std::endl;
for (auto f : folders) f->remMsg(this);
}

// Folder Implementation

void swap(Folder& lhs, Folder& rhs)
{
using std::swap;
for (auto m : lhs.msgs) m->remFldr(&lhs);

for (auto m : rhs.msgs) m->remFldr(&rhs);
lhs.remove_from_Messages();
rhs.remove_from_Messages();

swap(lhs.msgs, rhs.msgs);

for (auto m : lhs.msgs) m->addFldr(&lhs);

for (auto m : rhs.msgs) m->addFldr(&rhs);
lhs.add_to_Messages(lhs);
rhs.add_to_Messages(rhs);
}

void Folder::add_to_Message(const Folder& f)
Folder::Folder(const Folder& f) : msgs(f.msgs)
{
for (auto m : f.msgs) m->addFldr(this);
add_to_Messages(f);
}

Folder::Folder(const Folder& f) : msgs(f.msgs)
Folder& Folder::operator=(const Folder& rhs)
{
add_to_Message(f);
remove_from_Messages();
msgs = rhs.msgs;
add_to_Messages(rhs);
return *this;
}

void Folder::remove_to_Message()
Folder::~Folder()
{
for (auto m : msgs) m->remFldr(this);
remove_from_Messages();
}

Folder::~Folder()
void Folder::print_debug()
{
remove_to_Message();
for (auto m : msgs) std::cout << m->contents << " ";
std::cout << std::endl;
}

Folder& Folder::operator=(const Folder& rhs)
void Folder::add_to_Messages(const Folder& f)
{
remove_to_Message();
msgs = rhs.msgs;
add_to_Message(rhs);
return *this;
for (auto m : f.msgs) m->addFldr(this);
}

void Folder::print_debug()
void Folder::remove_from_Messages()
{
for (auto m : msgs) std::cout << m->contents << " ";
std::cout << std::endl;
for (auto m : msgs) m->remFldr(this);
}
6 changes: 3 additions & 3 deletions ch13/ex13_34_36_37.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#ifndef CP5_ex13_34_36_37_h
#define CP5_ex13_34_36_37_h

#include <string>
#include <set>
#include <string>

class Folder;

Expand Down Expand Up @@ -66,8 +66,8 @@ class Folder {
private:
std::set<Message*> msgs;

void add_to_Message(const Folder&);
void remove_to_Message();
void add_to_Messages(const Folder&);
void remove_from_Messages();

void addMsg(Message* m) { msgs.insert(m); }
void remMsg(Message* m) { msgs.erase(m); }
Expand Down
22 changes: 22 additions & 0 deletions ch13/ex13_34_36_37_TEST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Unit Test for exercise 13_34_36_37.

#include "ex13_34_36_37.h"

int main()
{
Message firstMail("hello");
Message signInMail("welcome to cppprimer");
Folder mailBox;

firstMail.print_debug(); // print: "hello"
firstMail.save(mailBox); // send to mailBox
mailBox.print_debug(); // print: "hello"

signInMail.print_debug(); // print "welcome to cppprimer"
signInMail.save(mailBox); // send to mailBox
mailBox.print_debug(); // print "welcome to cppprimer hello"

firstMail = firstMail; // test for assignment to self.
firstMail.print_debug(); // print "hello"
mailBox.print_debug(); // print "welcome to cppprimer hello"
}

0 comments on commit 2c3cf99

Please sign in to comment.