Messagesの例
8328 ワード
13.33 13.36 13.37
Message.h
Message.cpp
Folder.h
Folder.cpp
Message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include<iostream>
#include<string>
#include<set>
#include"Folder.h"
using namespace std;
class Folder;
class Message
{
friend class Folder;
public:
explicit Message(const string &str=""):contents(str) {}
Message(const Message&);
Message& operator=(const Message&);
~Message();
void save(Folder&);
void remove(Folder&);
void addFolder(Folder*);
void remFolder(Folder*);
void swap(Message&,Message&);
private:
string contents;
set<Folder*> folders;
void add_to_Folders(const Message&);
void remove_to_Folders();
};
#endif
Message.cpp
#include"Message.h"
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);
}
void Message::remove_to_Folders()
{
for(auto f:folders)
f->remMsg(this);
}
Message::Message(const Message &m):contents(m.contents),folders(m.folders)
{
add_to_Folders(m);
}
Message& Message::operator=(const Message& m)
{
remove_to_Folders();
contents=m.contents;
folders=m.folders;
add_to_Folders(m);
return *this;
}
Message::~Message()
{
remove_to_Folders();
}
void Message::addFolder(Folder* f)
{
folders.insert(f);
}
void Message::remFolder(Folder* f)
{
folders.erase(f);
}
void Message::swap(Message &lhs,Message &rhs)
{
using std::swap;
for(auto f:lhs.folders)
f->remMsg(&lhs);
for(auto f:rhs.folders)
f->remMsg(&rhs);
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);
}
Folder.h
#ifndef FOLDER_H
#define FOLDER_H
#include<iostream>
#include<set>
#include<string>
using namespace std;
class Message;
class Folder
{
friend class Message;
private:
set<Message*> msg;
public:
void addMsg(Message*);
void remMsg(Message*);
};
#endif // FOLDER_H
Folder.cpp
#include"Folder.h"
#include"Message.h"
void Folder::addMsg(Message *m)
{
msg.insert(m);
}
void Folder::remMsg(Message *m)
{
msg.erase(m);
}