summaryrefslogtreecommitdiff
path: root/shared/serialize.h
diff options
context:
space:
mode:
Diffstat (limited to 'shared/serialize.h')
-rw-r--r--shared/serialize.h198
1 files changed, 198 insertions, 0 deletions
diff --git a/shared/serialize.h b/shared/serialize.h
new file mode 100644
index 00000000..85f57370
--- /dev/null
+++ b/shared/serialize.h
@@ -0,0 +1,198 @@
+#ifndef SERIALIZE_H_INCLUDED
+#define SERIALIZE_H_INCLUDED
+
+#include "zstring.h"
+#include <wx/stream.h>
+#include "fileError.h"
+#include <boost/scoped_array.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace Utility
+{
+template <class T>
+T readNumber(wxInputStream& stream);
+
+template <class T>
+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 <class T>
+ T readNumberC(); //checked read operation
+
+ Zstring readStringC(); //checked read operation
+
+ typedef boost::shared_ptr<std::vector<char> > CharArray;
+ CharArray readArrayC();
+
+ void check();
+
+protected:
+ wxInputStream& stream_;
+
+private:
+ void throwReadError(); //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 <class T>
+ void writeNumberC(T number); //checked write operation
+
+ void writeStringC(const Zstring& str); //checked write operation
+
+ void writeArrayC(const std::vector<char>& buffer);
+
+ void check();
+
+protected:
+ wxOutputStream& stream_;
+
+private:
+ void throwWriteError(); //throw FileError()
+ const wxString& errorObjName_; //used for error text only!
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//---------------Inline Implementation---------------------------------------------------
+template <class T>
+inline
+T readNumber(wxInputStream& stream)
+{
+ T result = 0;
+ stream.Read(&result, sizeof(T));
+ return result;
+}
+
+
+template <class T>
+inline
+void writeNumber(wxOutputStream& stream, T number)
+{
+ stream.Write(&number, sizeof(T));
+}
+
+
+inline
+Zstring readString(wxInputStream& stream) //read string from file stream
+{
+ const size_t strLength = readNumber<size_t>(stream);
+ if (strLength <= 1000)
+ {
+ DefaultChar buffer[1000];
+ stream.Read(buffer, sizeof(DefaultChar) * strLength);
+ return Zstring(buffer, strLength);
+ }
+ else
+ {
+ boost::scoped_array<DefaultChar> buffer(new DefaultChar[strLength]);
+ stream.Read(buffer.get(), sizeof(DefaultChar) * strLength);
+ return Zstring(buffer.get(), strLength);
+ }
+}
+
+
+inline
+void writeString(wxOutputStream& stream, const Zstring& str) //write string to filestream
+{
+ writeNumber<size_t>(stream, str.length());
+ stream.Write(str.c_str(), sizeof(DefaultChar) * str.length());
+}
+
+
+inline
+void ReadInputStream::check()
+{
+ if (stream_.GetLastError() != wxSTREAM_NO_ERROR)
+ throwReadError();
+}
+
+
+template <class T>
+inline
+T ReadInputStream::readNumberC() //checked read operation
+{
+ T output = readNumber<T>(stream_);
+ check();
+ return output;
+}
+
+
+inline
+Zstring ReadInputStream::readStringC() //checked read operation
+{
+ Zstring output = readString(stream_);
+ check();
+ return output;
+}
+
+
+template <class T>
+inline
+void WriteOutputStream::writeNumberC(T number) //checked write operation
+{
+ writeNumber<T>(stream_, number);
+ check();
+}
+
+
+inline
+void WriteOutputStream::writeStringC(const Zstring& str) //checked write operation
+{
+ writeString(stream_, str);
+ check();
+}
+
+
+
+inline
+void WriteOutputStream::check()
+{
+ if (stream_.GetLastError() != wxSTREAM_NO_ERROR)
+ throwWriteError();
+}
+
+
+}
+
+#endif // SERIALIZE_H_INCLUDED
bgstack15