From de1aa6d66e11232edf2d7bbc729425003f6c3db5 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 13 May 2006 00:08:20 +0000 Subject: david - initial commit of serialize code ;p --- src/serializable.cpp | 8 ++ src/serializable.h | 14 ++++ src/serializer.cpp | 185 +++++++++++++++++++++++++++++++++++++++++++++++ src/serializer.h | 64 ++++++++++++++++ src/serializerbinary.cpp | 163 +++++++++++++++++++++++++++++++++++++++++ src/serializerbinary.h | 46 ++++++++++++ src/test/serialize.cpp | 24 ++++++ 7 files changed, 504 insertions(+) create mode 100644 src/serializable.cpp create mode 100644 src/serializable.h create mode 100644 src/serializer.cpp create mode 100644 src/serializer.h create mode 100644 src/serializerbinary.cpp create mode 100644 src/serializerbinary.h create mode 100644 src/test/serialize.cpp (limited to 'src') diff --git a/src/serializable.cpp b/src/serializable.cpp new file mode 100644 index 0000000..fd50943 --- /dev/null +++ b/src/serializable.cpp @@ -0,0 +1,8 @@ +#include "serializable.h" + +Serializable::Serializable() +{ +} +Serializable::~Serializable() +{ +} diff --git a/src/serializable.h b/src/serializable.h new file mode 100644 index 0000000..36c94df --- /dev/null +++ b/src/serializable.h @@ -0,0 +1,14 @@ +#ifndef SERIALIZER_H +#define SERIALIZER_H + +//#include "serializer.h" + +class Serializable +{ +public: + Serializable(); + virtual ~Serializable(); + virtual void serialize(class Serializer &)=0; +}; + +#endif diff --git a/src/serializer.cpp b/src/serializer.cpp new file mode 100644 index 0000000..5982a26 --- /dev/null +++ b/src/serializer.cpp @@ -0,0 +1,185 @@ +#include "serializer.h" +#include "serializable.h" + +Serializer::Serializer(bool bLoading): + bLoading(bLoading) +{ +} +Serializer::~Serializer() +{ +} + +bool Serializer::isLoading() +{ + return bLoading; +} + +/*Serializer::Serializer &operator&(bool &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(int8_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(int16_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(int32_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(int64_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(uint8_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(uint16_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(uint32_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(uint64_t &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(float &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(double &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +} + +Serializer::Serializer &operator&(long double &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +}*/ + + +Serializer &Serializer::operator<<(Serializable &p) +{ + p.serialize(*this); + return *this; +} + +Serializer &Serializer::operator>>(Serializable &p) +{ + p.serialize(*this); + return *this; +} + +/*Serializer::Serializer &operator&(Serializable &p) +{ + if (bLoading) + { + return *this << p; + } + else + { + return *this >> p; + } +}*/ + diff --git a/src/serializer.h b/src/serializer.h new file mode 100644 index 0000000..03e6820 --- /dev/null +++ b/src/serializer.h @@ -0,0 +1,64 @@ +#ifndef SERIALIZABLE_H +#define SERIALIZABLE_H + +#include +//#include "serializable.h" + +class Serializer +{ +private: + bool bLoading; +public: + bool isLoading(); + + Serializer(bool bLoading); + virtual ~Serializer(); + virtual void close()=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 &)=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<<(class Serializable &); + virtual Serializer &operator>>(class Serializable &); + //virtual Serializer &operator&(Serializable &); +}; + +#endif diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp new file mode 100644 index 0000000..1ea9f36 --- /dev/null +++ b/src/serializerbinary.cpp @@ -0,0 +1,163 @@ +#include "serializerbinary.h" + +SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading): + Serializer(bLoading), + fhFile(fhFile), + bCloseFile(false) +{ +} + +SerializerBinary::SerializerBinary(char *sFileName, bool bLoading): + Serializer(bLoading), + bCloseFile(true) +{ + if (bLoading) + { + fhFile = fopen(sFileName, "rb"); + } + else + { + fhFile = fopen(sFileName, "wb"); + } +} + +SerializerBinary::~SerializerBinary() +{ + close(); +} + +void SerializerBinary::close() +{ + if (fhFile != NULL) + { + fclose(fhFile); + fhFile = NULL; + } +} + +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) +{ + printf("int: %d, size: %d\n", p, sizeof(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; +} + +Serializer &SerializerBinary::operator>>(bool &p) +{ + bool bTmp; + fread(&bTmp, sizeof(p), 1, fhFile); + p = bTmp; + 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) +{ + printf("reding before: %d", p); + fread(&p, sizeof(p), 1, fhFile); + printf(" after: %d\n", p); + 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 new file mode 100644 index 0000000..4a2f301 --- /dev/null +++ b/src/serializerbinary.h @@ -0,0 +1,46 @@ +#ifndef SERIALIZER_BINARY_H +#define SERIALIZER_BINARY_H + +#include "serializer.h" +#include + +class SerializerBinary : public Serializer +{ +public: + SerializerBinary(FILE *fhFile, bool bLoading); + SerializerBinary(char *sFileName, bool bLoading); + ~SerializerBinary(); + + virtual void close(); + + 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; +}; + +#endif diff --git a/src/test/serialize.cpp b/src/test/serialize.cpp new file mode 100644 index 0000000..883be5e --- /dev/null +++ b/src/test/serialize.cpp @@ -0,0 +1,24 @@ +#include "serializerbinary.h" +#include + +int main() +{ + int32_t one; + double two; + bool three; + SerializerBinary ar("hello.dat", false); + ar << (int)85; + ar << (double)2.63434; + ar << false; + ar.close(); + + one = 0; two = 0; three = true; + + SerializerBinary ar2("hello.dat", true); + ar2 >> one; + ar2 >> two; + ar2 >> three; + + printf("we got %d - %f - %s\n", one, two, (three ? "true":"false")); + return 0; +} -- cgit v1.2.3