Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Serializable.h

00001 // Copyright (C) 2001 Kevin Duffy <kevin@buhbird.com>
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License, or (at your option) any later version.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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

Generated at Wed May 2 00:24:41 2001 for API by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001