From 27e0164234c916adaf0ce78e29dffe565302ef53 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 25 Nov 2006 23:38:14 +0000 Subject: I think this is a better serializer model. --- src/exceptions.h | 5 ++ src/serializer.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++ src/serializer.h | 48 +++++++++--------- src/serializerbinary.cpp | 126 ++--------------------------------------------- src/serializerbinary.h | 26 ---------- src/serializerbzip2.cpp | 122 --------------------------------------------- src/serializerbzip2.h | 26 ---------- 7 files changed, 154 insertions(+), 320 deletions(-) diff --git a/src/exceptions.h b/src/exceptions.h index b4126b7..cf9d47a 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -8,6 +8,11 @@ subExceptionDecl( XmlException ) subExceptionDecl( FileException ) subExceptionDecl( ConnectionException ) +enum eFileException +{ + excodeEOF +}; + enum eConnectionException { excodeReadError, diff --git a/src/serializer.cpp b/src/serializer.cpp index 0afaf4b..587f613 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp @@ -14,6 +14,127 @@ bool Serializer::isLoading() { return bLoading; } +Serializer &Serializer::operator<<(bool p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(int8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(int16_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(int32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(int64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(uint8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(uint16_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(uint32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(uint64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(float p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(double p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator<<(long double p) +{ + write( &p, sizeof(p) ); + return *this; +} + +Serializer &Serializer::operator>>(bool &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(int8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(int16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(int32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(int64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(uint8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(uint16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(uint32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(uint64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(float &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(double &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &Serializer::operator>>(long double &p) +{ + read( &p, sizeof(p) ); + return *this; +} /*Serializer::Serializer &operator&(bool &p) { diff --git a/src/serializer.h b/src/serializer.h index 334c3c7..e79c810 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -26,31 +26,31 @@ public: virtual void write(const void *, int32_t)=0; virtual void read(void *, int32_t)=0; - virtual Serializer &operator<<(bool)=0; - virtual Serializer &operator<<(int8_t)=0; - virtual Serializer &operator<<(int16_t)=0; - virtual Serializer &operator<<(int32_t)=0; - virtual Serializer &operator<<(int64_t)=0; - virtual Serializer &operator<<(uint8_t)=0; - virtual Serializer &operator<<(uint16_t)=0; - virtual Serializer &operator<<(uint32_t)=0; - virtual Serializer &operator<<(uint64_t)=0; - virtual Serializer &operator<<(float)=0; - virtual Serializer &operator<<(double)=0; - virtual Serializer &operator<<(long double)=0; + virtual Serializer &operator<<(bool); + virtual Serializer &operator<<(int8_t); + virtual Serializer &operator<<(int16_t); + virtual Serializer &operator<<(int32_t); + virtual Serializer &operator<<(int64_t); + virtual Serializer &operator<<(uint8_t); + virtual Serializer &operator<<(uint16_t); + virtual Serializer &operator<<(uint32_t); + virtual Serializer &operator<<(uint64_t); + virtual Serializer &operator<<(float); + virtual Serializer &operator<<(double); + virtual Serializer &operator<<(long double); - virtual Serializer &operator>>(bool &)=0; - virtual Serializer &operator>>(int8_t &)=0; - virtual Serializer &operator>>(int16_t &)=0; - virtual Serializer &operator>>(int32_t &)=0; - virtual Serializer &operator>>(int64_t &)=0; - virtual Serializer &operator>>(uint8_t &)=0; - virtual Serializer &operator>>(uint16_t &)=0; - virtual Serializer &operator>>(uint32_t &)=0; - virtual Serializer &operator>>(uint64_t &)=0; - virtual Serializer &operator>>(float &)=0; - virtual Serializer &operator>>(double &)=0; - virtual Serializer &operator>>(long double &)=0; + virtual Serializer &operator>>(bool &); + virtual Serializer &operator>>(int8_t &); + virtual Serializer &operator>>(int16_t &); + virtual Serializer &operator>>(int32_t &); + virtual Serializer &operator>>(int64_t &); + virtual Serializer &operator>>(uint8_t &); + virtual Serializer &operator>>(uint16_t &); + virtual Serializer &operator>>(uint32_t &); + virtual Serializer &operator>>(uint64_t &); + virtual Serializer &operator>>(float &); + virtual Serializer &operator>>(double &); + virtual Serializer &operator>>(long double &); /* virtual Serializer &operator&(bool &); diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp index 23fb2b1..bfe3cc9 100644 --- a/src/serializerbinary.cpp +++ b/src/serializerbinary.cpp @@ -48,127 +48,9 @@ void SerializerBinary::write(const void * pData, int32_t nSize) void SerializerBinary::read(void * pData, int32_t nSize) { - fread(pData, nSize, 1, fhFile); -} - -Serializer &SerializerBinary::operator<<(bool p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(int8_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(int16_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(int32_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(int64_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(uint8_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(uint16_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(uint32_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(uint64_t p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(float p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(double p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator<<(long double p) -{ - fwrite(&p, sizeof(p), 1, fhFile); - return *this; + if( fread(pData, nSize, 1, fhFile) < 1 ) + { + throw FileException( excodeEOF, "End of file read"); + } } -Serializer &SerializerBinary::operator>>(bool &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(int8_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(int16_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(int32_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(int64_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(uint8_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(uint16_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(uint32_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(uint64_t &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(float &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(double &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} -Serializer &SerializerBinary::operator>>(long double &p) -{ - fread(&p, sizeof(p), 1, fhFile); - return *this; -} diff --git a/src/serializerbinary.h b/src/serializerbinary.h index 2930077..8510fcd 100644 --- a/src/serializerbinary.h +++ b/src/serializerbinary.h @@ -16,32 +16,6 @@ public: virtual void write(const void *, int32_t); virtual void read(void *, int32_t); - virtual Serializer &operator<<(bool); - virtual Serializer &operator<<(int8_t); - virtual Serializer &operator<<(int16_t); - virtual Serializer &operator<<(int32_t); - virtual Serializer &operator<<(int64_t); - virtual Serializer &operator<<(uint8_t); - virtual Serializer &operator<<(uint16_t); - virtual Serializer &operator<<(uint32_t); - virtual Serializer &operator<<(uint64_t); - virtual Serializer &operator<<(float); - virtual Serializer &operator<<(double); - virtual Serializer &operator<<(long double); - - virtual Serializer &operator>>(bool &); - virtual Serializer &operator>>(int8_t &); - virtual Serializer &operator>>(int16_t &); - virtual Serializer &operator>>(int32_t &); - virtual Serializer &operator>>(int64_t &); - virtual Serializer &operator>>(uint8_t &); - virtual Serializer &operator>>(uint16_t &); - virtual Serializer &operator>>(uint32_t &); - virtual Serializer &operator>>(uint64_t &); - virtual Serializer &operator>>(float &); - virtual Serializer &operator>>(double &); - virtual Serializer &operator>>(long double &); - private: FILE *fhFile; bool bCloseFile; diff --git a/src/serializerbzip2.cpp b/src/serializerbzip2.cpp index 9d4dafa..bafabc8 100644 --- a/src/serializerbzip2.cpp +++ b/src/serializerbzip2.cpp @@ -86,125 +86,3 @@ void SerializerBZip2::read(void * pData, int32_t nSize) checkBZError(); } -Serializer &SerializerBZip2::operator<<(bool p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(int8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(int16_t p) -{ - write (&p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(int32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(int64_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(uint8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(uint16_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(uint32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(uint64_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(float p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(double p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator<<(long double p) -{ - write( &p, sizeof(p) ); - return *this; -} - -Serializer &SerializerBZip2::operator>>(bool &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(int8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(int16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(int32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(int64_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(uint8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(uint16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(uint32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(uint64_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(float &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(double &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &SerializerBZip2::operator>>(long double &p) -{ - read( &p, sizeof(p) ); - return *this; -} - diff --git a/src/serializerbzip2.h b/src/serializerbzip2.h index 098c66e..4aeb22e 100644 --- a/src/serializerbzip2.h +++ b/src/serializerbzip2.h @@ -16,32 +16,6 @@ public: virtual void write(const void *, int32_t); virtual void read(void *, int32_t); - virtual Serializer &operator<<(bool); - virtual Serializer &operator<<(int8_t); - virtual Serializer &operator<<(int16_t); - virtual Serializer &operator<<(int32_t); - virtual Serializer &operator<<(int64_t); - virtual Serializer &operator<<(uint8_t); - virtual Serializer &operator<<(uint16_t); - virtual Serializer &operator<<(uint32_t); - virtual Serializer &operator<<(uint64_t); - virtual Serializer &operator<<(float); - virtual Serializer &operator<<(double); - virtual Serializer &operator<<(long double); - - virtual Serializer &operator>>(bool &); - virtual Serializer &operator>>(int8_t &); - virtual Serializer &operator>>(int16_t &); - virtual Serializer &operator>>(int32_t &); - virtual Serializer &operator>>(int64_t &); - virtual Serializer &operator>>(uint8_t &); - virtual Serializer &operator>>(uint16_t &); - virtual Serializer &operator>>(uint32_t &); - virtual Serializer &operator>>(uint64_t &); - virtual Serializer &operator>>(float &); - virtual Serializer &operator>>(double &); - virtual Serializer &operator>>(long double &); - private: void checkBZError(); FILE *fhFile; -- cgit v1.2.3