From eb71d88041ccbb0aec3532f9b1aa40e956e948c2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 13 May 2006 01:54:06 +0000 Subject: Changed some things in the serializer...it's cool --- src/serializer.cpp | 30 ++++++++++++++++++++++++------ src/serializer.h | 14 ++++++++++---- src/serializerbinary.cpp | 10 +++------- src/serializerbinary.h | 5 +++-- src/staticstring.cpp | 16 ++++++++++++++++ src/staticstring.h | 5 ++++- src/test/serialize.cpp | 10 ++++++++-- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/serializer.cpp b/src/serializer.cpp index 5982a26..d41cc64 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp @@ -1,5 +1,6 @@ #include "serializer.h" #include "serializable.h" +#include Serializer::Serializer(bool bLoading): bLoading(bLoading) @@ -159,16 +160,16 @@ Serializer::Serializer &operator&(long double &p) }*/ -Serializer &Serializer::operator<<(Serializable &p) +Serializer &operator<<(Serializer &s, Serializable &p) { - p.serialize(*this); - return *this; + p.serialize( s ); + return s; } -Serializer &Serializer::operator>>(Serializable &p) +Serializer &operator>>(Serializer &s, Serializable &p) { - p.serialize(*this); - return *this; + p.serialize( s ); + return s; } /*Serializer::Serializer &operator&(Serializable &p) @@ -183,3 +184,20 @@ Serializer &Serializer::operator>>(Serializable &p) } }*/ +Serializer &operator<<( Serializer &ar, std::string &s ) +{ + ar << s.length(); + ar.write( s.c_str(), s.length() ); +} + +Serializer &operator>>( Serializer &ar, std::string &s ) +{ + int l; + ar >> l; + char *tmp = new char[l+1]; + tmp[l] = '\0'; + ar.read( tmp, l ); + s = tmp; + delete tmp; +} + diff --git a/src/serializer.h b/src/serializer.h index f2648e3..274d8de 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -2,6 +2,8 @@ #define SERIALIZABLE_H #include +#include +#include //#include "serializable.h" class Serializer @@ -15,9 +17,9 @@ public: virtual ~Serializer(); virtual void close()=0; - virtual void write(void *, int32_t)=0; + 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; @@ -59,9 +61,13 @@ public: virtual Serializer &operator&(long double &); */ - virtual Serializer &operator<<(class Serializable &); - virtual Serializer &operator>>(class Serializable &); //virtual Serializer &operator&(Serializable &); }; +Serializer &operator<<(Serializer &, class Serializable &); +Serializer &operator>>(Serializer &, class Serializable &); + +Serializer &operator<<(Serializer &, std::string &); +Serializer &operator>>(Serializer &, std::string &); + #endif diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp index 53e469f..4b741ae 100644 --- a/src/serializerbinary.cpp +++ b/src/serializerbinary.cpp @@ -1,4 +1,5 @@ #include "serializerbinary.h" +#include "serializable.h" SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading): Serializer(bLoading), @@ -35,7 +36,7 @@ void SerializerBinary::close() } } -void SerializerBinary::write(void * pData, int32_t nSize) +void SerializerBinary::write(const void * pData, int32_t nSize) { fwrite(pData, nSize, 1, fhFile); } @@ -62,7 +63,6 @@ Serializer &SerializerBinary::operator<<(int16_t p) } Serializer &SerializerBinary::operator<<(int32_t p) { - printf("int: %d, size: %d\n", p, sizeof(p)); fwrite(&p, sizeof(p), 1, fhFile); return *this; } @@ -109,9 +109,7 @@ Serializer &SerializerBinary::operator<<(long double p) Serializer &SerializerBinary::operator>>(bool &p) { - bool bTmp; - fread(&bTmp, sizeof(p), 1, fhFile); - p = bTmp; + fread(&p, sizeof(p), 1, fhFile); return *this; } Serializer &SerializerBinary::operator>>(int8_t &p) @@ -126,9 +124,7 @@ Serializer &SerializerBinary::operator>>(int16_t &p) } Serializer &SerializerBinary::operator>>(int32_t &p) { - printf("reding before: %d", p); fread(&p, sizeof(p), 1, fhFile); - printf(" after: %d\n", p); return *this; } Serializer &SerializerBinary::operator>>(int64_t &p) diff --git a/src/serializerbinary.h b/src/serializerbinary.h index 7323d3b..9d68d46 100644 --- a/src/serializerbinary.h +++ b/src/serializerbinary.h @@ -13,9 +13,9 @@ public: virtual void close(); - virtual void write(void *, int32_t); + 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); @@ -41,6 +41,7 @@ public: virtual Serializer &operator>>(float &); virtual Serializer &operator>>(double &); virtual Serializer &operator>>(long double &); + private: FILE *fhFile; bool bCloseFile; diff --git a/src/staticstring.cpp b/src/staticstring.cpp index 7543178..24b9ecb 100644 --- a/src/staticstring.cpp +++ b/src/staticstring.cpp @@ -1,4 +1,5 @@ #include "staticstring.h" +#include "serializer.h" #include #include @@ -182,3 +183,18 @@ void StaticString::clear() memset( lpStr, 0, nLen+1 ); } +void StaticString::serialize( Serializer &ar ) +{ + if( ar.isLoading() ) + { + ar >> nLen; + setLength( nLen ); + ar.read( lpStr, nLen ); + } + else + { + ar << nLen; + ar.write( lpStr, nLen ); + } +} + diff --git a/src/staticstring.h b/src/staticstring.h index 88cae70..1c98b3c 100644 --- a/src/staticstring.h +++ b/src/staticstring.h @@ -2,6 +2,7 @@ #define STATIC_STRING_H #include +#include "serializable.h" /** * Simple string managing class. Allows for dynamically allocated string data @@ -10,7 +11,7 @@ * and making accessing meta-info like length fast and reliable as well. *@author Mike Buland */ -class StaticString +class StaticString : public Serializable { public: StaticString(); @@ -40,6 +41,8 @@ public: void clear(); + virtual void serialize( class Serializer &ar ); + private: char *lpStr; int nLen; diff --git a/src/test/serialize.cpp b/src/test/serialize.cpp index 883be5e..e233704 100644 --- a/src/test/serialize.cpp +++ b/src/test/serialize.cpp @@ -1,24 +1,30 @@ #include "serializerbinary.h" +#include "staticstring.h" #include +#include int main() { int32_t one; double two; bool three; + StaticString s("Test string!"); + std::string ss("Another test string"); SerializerBinary ar("hello.dat", false); ar << (int)85; ar << (double)2.63434; ar << false; + ar << ss; ar.close(); - one = 0; two = 0; three = true; + one = 0; two = 0; three = true; s = "die"; SerializerBinary ar2("hello.dat", true); ar2 >> one; ar2 >> two; ar2 >> three; + ar2 >> s; - printf("we got %d - %f - %s\n", one, two, (three ? "true":"false")); + printf("we got %d - %f - %s - \"%s\"\n", one, two, (three ? "true":"false"), s.getString() ); return 0; } -- cgit v1.2.3