// ************************************************************************** // * This file is part of the FreeFileSync project. It is distributed under * // * GNU General Public License: http://www.gnu.org/licenses/gpl.html * // * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de) * // ************************************************************************** // #ifndef SERIALIZE_H_INCLUDED #define SERIALIZE_H_INCLUDED #include "zstring.h" #include #include "file_error.h" #include #include #include namespace util { template T readNumber(wxInputStream& stream); template void writeNumber(wxOutputStream& stream, T number); Zstring readString(wxInputStream& stream); void writeString(wxOutputStream& stream, const Zstring& str); class ReadInputStream //throw (FileError) { protected: ReadInputStream(wxInputStream& stream, const wxString& errorObjName) : stream_(stream), errorObjName_(errorObjName) {} template T readNumberC() const; //throw (FileError), checked read operation Zstring readStringC() const; //throw (FileError), checked read operation typedef boost::shared_ptr > CharArray; //there's no guarantee std::string has a ref-counted implementation... so use this "thing" CharArray readArrayC() const; //throw (FileError) void check() const; wxInputStream& getStream() { return stream_; } private: wxInputStream& stream_; void throwReadError() const; //throw (FileError) const wxString& errorObjName_; //used for error text only }; class WriteOutputStream //throw (FileError) { protected: WriteOutputStream(const wxString& errorObjName, wxOutputStream& stream) : stream_(stream), errorObjName_(errorObjName) {} template void writeNumberC(T number) const; //throw (FileError), checked write operation void writeStringC(const Zstring& str) const; //throw (FileError), checked write operation void writeArrayC(const std::vector& buffer) const; //throw (FileError) void check() const; wxOutputStream& getStream() { return stream_; } private: wxOutputStream& stream_; void throwWriteError() const; //throw (FileError) const wxString& errorObjName_; //used for error text only! }; //---------------Inline Implementation--------------------------------------------------- template inline T readNumber(wxInputStream& stream) { T result = 0; stream.Read(&result, sizeof(T)); return result; } template inline void writeNumber(wxOutputStream& stream, T number) { stream.Write(&number, sizeof(T)); } inline Zstring readString(wxInputStream& stream) { const boost::uint32_t strLength = readNumber(stream); if (strLength <= 1000) { Zchar buffer[1000]; stream.Read(buffer, sizeof(Zchar) * strLength); return Zstring(buffer, strLength); } else { boost::scoped_array buffer(new Zchar[strLength]); stream.Read(buffer.get(), sizeof(Zchar) * strLength); return Zstring(buffer.get(), strLength); } } inline void writeString(wxOutputStream& stream, const Zstring& str) { writeNumber(stream, static_cast(str.length())); stream.Write(str.c_str(), sizeof(Zchar) * str.length()); } inline void ReadInputStream::check() const { if (stream_.GetLastError() != wxSTREAM_NO_ERROR) throwReadError(); } template inline T ReadInputStream::readNumberC() const //checked read operation { T output = readNumber(stream_); check(); return output; } inline Zstring ReadInputStream::readStringC() const //checked read operation { Zstring output = readString(stream_); check(); return output; } template inline void WriteOutputStream::writeNumberC(T number) const //checked write operation { writeNumber(stream_, number); check(); } inline void WriteOutputStream::writeStringC(const Zstring& str) const //checked write operation { writeString(stream_, str); check(); } inline void WriteOutputStream::check() const { if (stream_.GetLastError() != wxSTREAM_NO_ERROR) throwWriteError(); } } #endif // SERIALIZE_H_INCLUDED