00001
00002 #ifndef SERIALIZABLE_H
00003 #define SERIALIZABLE_H
00004
00005
00006 #include<iostream>
00007
00008 typedef unsigned char u_int8_t;
00009 typedef short int16_t;
00010 typedef unsigned short u_int16_t;
00011 typedef unsigned int u_int32_t;
00012 typedef int int32_t;
00013
00014
00015
00016
00017
00018
00019 class ISerializable
00020 {
00021 public:
00022 virtual ~ISerializable() {}
00023
00024 virtual void serialize(std::string& sBuf) const = 0;
00025 virtual int deserialize(const std::string& sBuf) = 0;
00026 };
00027
00028
00029
00030
00031
00032
00033
00034 void SerializeBytes(const void* p, unsigned int uSize, std::string& sBuf);
00035 void Serialize8(u_int8_t u8, std::string& sBuf);
00036 void Serialize16(u_int16_t u16, std::string& sBuf);
00037 void Serialize32(u_int32_t u32, std::string& sBuf);
00038 void SerializeCString(const char* p, unsigned int uSize, std::string& sBuf);
00039 void SerializeString8(const std::string& s, std::string& sBuf);
00040 void SerializeString16(const std::string& s, std::string& sBuf);
00041 void SerializeString32(const std::string& s, std::string& sBuf);
00042
00043 bool DeserializeBytes(
00044 void* p,
00045 unsigned int uSize,
00046 const std::string& sBuf,
00047 int& iOffset);
00048 bool Deserialize8(u_int8_t& u8, const std::string& sBuf, int& iOffset);
00049 bool Deserialize16(u_int16_t& u16, const std::string& sBuf, int& iOffset);
00050 bool Deserialize32(u_int32_t& u32, const std::string& sBuf, int& iOffset);
00051 bool DeserializeCString(
00052 char* p,
00053 unsigned int uSize,
00054 const std::string& sBuf,
00055 int& iOffset);
00056 bool DeserializeString8(
00057 std::string& s,
00058 const std::string& sBuf,
00059 int& iOffset);
00060 bool DeserializeString16(
00061 std::string& s,
00062 const std::string& sBuf,
00063 int& iOffset);
00064 bool DeserializeString32(
00065 std::string& s,
00066 const std::string& sBuf,
00067 int& iOffset);
00068
00069
00070
00071
00072
00073
00074 #define SERIALIZE_BYTES(X, Y) \
00075 SerializeBytes(X, Y, sBuf)
00076
00077 #define SERIALIZE8(X) \
00078 Serialize8(X, sBuf)
00079
00080 #define SERIALIZE16(X) \
00081 Serialize16(X, sBuf)
00082
00083 #define SERIALIZE32(X) \
00084 Serialize32(X, sBuf)
00085
00086 #define SERIALIZE_C_STR(X) \
00087 SerializeCString(X, sizeof(X), sBuf)
00088
00089 #define SERIALIZE_BASE_CLASS(X) \
00090 X::serialize(sBuf)
00091
00092 #define SERIALIZE_STR8(X) \
00093 SerializeString8(X, sBuf)
00094
00095 #define SERIALIZE_STR16(X) \
00096 SerializeString16(X, sBuf)
00097
00098 #define SERIALIZE_STR32(X) \
00099 SerializeString32(X, sBuf)
00100
00101 #define SERIALIZE_VAR255_STR(X) \
00102 SERIALIZE_STR8(X)
00103
00104 #define SERIALIZE_STR(X) \
00105 SERIALIZE_STR16(X)
00106
00107 #define DESERIALIZE_BYTES(X, Y) \
00108 { if (!DeserializeBytes(X, Y, sBuf, iOffset)) return -1; }
00109
00110 #define DESERIALIZE8(X) \
00111 { if (!Deserialize8(X, sBuf, iOffset)) return -1; }
00112
00113 #define DESERIALIZE16(X) \
00114 { if (!Deserialize16(X, sBuf, iOffset)) return -1; }
00115
00116 #define DESERIALIZE32(X) \
00117 { if (!Deserialize32(X, sBuf, iOffset)) return -1; }
00118
00119 #define DESERIALIZE_C_STR(X) \
00120 { if (!DeserializeCString(X, sizeof(X), sBuf, iOffset)) return -1; }
00121
00122 #define DESERIALIZE_BASE_CLASS(X) \
00123 { \
00124 int iTemp = X::deserialize(sBuf.substr(iOffset)); \
00125 if (iTemp < 0) \
00126 return iTemp; \
00127 iOffset += iTemp; \
00128 }
00129
00130 #define DESERIALIZE_STR8(X) \
00131 { if (!DeserializeString8(X, sBuf, iOffset)) return -1; }
00132
00133 #define DESERIALIZE_STR16(X) \
00134 { if (!DeserializeString16(X, sBuf, iOffset)) return -1; }
00135
00136 #define DESERIALIZE_STR32(X) \
00137 { if (!DeserializeString32(X, sBuf, iOffset)) return -1; }
00138
00139 #define DESERIALIZE_VAR255_STR(X) \
00140 DESERIALIZE_STR8(X)
00141
00142 #define DESERIALIZE_STR(X) \
00143 DESERIALIZE_STR16(X)
00144
00145
00146 #endif //SERIALIZABLE_H