00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef SERIALIZABLE_H
00019 #define SERIALIZABLE_H
00020
00021 #include <fstream>
00022 #include "Exceptions.h"
00023 #include "Writeable.h"
00024 #include "Readable.h"
00025
00027
00032 class Serializable : virtual public Writeable,
00033 virtual public Readable
00034 {
00035 public:
00037
00039 virtual void Save(const string& filename) const
00040 {
00041 ofstream fout;
00042 fout.open(filename.c_str());
00043 fout << ToString();
00044 fout.close();
00045 };
00046
00048
00051 virtual void Load(const string& filename)
00052 throw (FileNotFoundException, UnknownException)
00053 {
00054 ifstream fin;
00055 try {
00056 OpenFile(fin, filename);
00057 }
00058 catch (const FileNotFoundException& e)
00059 {
00060 throw (e);
00061 }
00062 catch (...)
00063 {
00064 throw (UnknownException("Unknown error occured while trying to open: " + filename));
00065 }
00066
00067 fin >> *this;
00068 fin.close();
00069 };
00070
00071 protected:
00073
00075 void OpenFile(ifstream& fin, const string& filename) throw(FileNotFoundException)
00076 {
00077 fin.open(filename.c_str());
00078 if (!fin)
00079 throw (FileNotFoundException(filename.c_str()));
00080 };
00081 };
00082 #endif