Hydra 0.20
|
00001 00002 /* 00003 * Copyright (c) 2009 Aleksander B. Demko 00004 * This source code is distributed under the MIT license. 00005 * See the accompanying file LICENSE.MIT.txt for details. 00006 */ 00007 00008 #ifndef __INCLUDED_HYDRA_RECORD_H__ 00009 #define __INCLUDED_HYDRA_RECORD_H__ 00010 00011 #include <QDataStream> 00012 00013 namespace hydra 00014 { 00015 class Record; 00016 } 00017 00018 // forward, for the friendship thing 00019 QDataStream & operator << (QDataStream & stream, const hydra::Record &rec); 00020 QDataStream & operator >> (QDataStream & stream, hydra::Record &rec); 00021 00022 /** 00023 * Something that is saved/load from the db. 00024 * 00025 * @author Aleksander Demko 00026 */ 00027 class hydra::Record 00028 { 00029 public: 00030 class Exception : public std::exception 00031 { 00032 public: 00033 #ifdef HYDRA_ASSERT_RECORD_ERRORS 00034 Exception(void) { assert(false && "[Record::Exception thrown]\n"); } // thrown during reads or writes 00035 #endif 00036 virtual const char* what(void) const throw(); 00037 }; 00038 class IOException : public Exception { }; // raw IO error 00039 class FormatException : public Exception { }; // stream format error 00040 class VersionException : public Exception { }; // bad version of objects on stream 00041 00042 public: 00043 virtual ~Record() { } 00044 00045 protected: 00046 /// saver, throws exceptions on errors 00047 virtual void save(QDataStream &out) const = 0; 00048 /// loader, throws exceptions on errors 00049 virtual void load(QDataStream &in) = 0; 00050 00051 private: 00052 friend QDataStream & ::operator << (QDataStream & stream, const hydra::Record &rec); 00053 friend QDataStream & ::operator >> (QDataStream & stream, hydra::Record &rec); 00054 }; 00055 00056 // now declare their bodies as theyre inline 00057 inline QDataStream & operator << (QDataStream & stream, const hydra::Record &rec) { rec.save(stream); return stream; } 00058 inline QDataStream & operator >> (QDataStream & stream, hydra::Record &rec) { rec.load(stream); return stream; } 00059 00060 #endif 00061