22 #ifndef COMMON_SERIALIZER_H 23 #define COMMON_SERIALIZER_H 25 #include "common/stream.h" 26 #include "common/str.h" 39 #define VER(x) Common::Serializer::Version(x) 41 #define SYNC_AS(SUFFIX,TYPE,SIZE) \ 42 template<typename T> \ 43 void syncAs ## SUFFIX(T &val, Version minVersion = 0, Version maxVersion = kLastVersion) { \ 44 if (_version < minVersion || _version > maxVersion) \ 47 val = static_cast<T>(_loadStream->read ## SUFFIX()); \ 50 _saveStream->write ## SUFFIX(tmp); \ 52 _bytesSynced += SIZE; \ 55 #define SYNC_PRIMITIVE(suffix) \ 56 template <typename T> \ 57 static inline void suffix(Serializer &s, T &value) { \ 58 s.syncAs##suffix(value); \ 81 typedef uint32 Version;
82 static const Version kLastVersion = 0xFFFFFFFF;
84 SYNC_PRIMITIVE(Uint32LE)
85 SYNC_PRIMITIVE(Uint32BE)
86 SYNC_PRIMITIVE(Sint32LE)
87 SYNC_PRIMITIVE(Sint32BE)
88 SYNC_PRIMITIVE(FloatLE)
89 SYNC_PRIMITIVE(FloatBE)
90 SYNC_PRIMITIVE(DoubleLE)
91 SYNC_PRIMITIVE(DoubleBE)
92 SYNC_PRIMITIVE(Uint16LE)
93 SYNC_PRIMITIVE(Uint16BE)
94 SYNC_PRIMITIVE(Sint16LE)
95 SYNC_PRIMITIVE(Sint16BE)
109 : _loadStream(in), _saveStream(out), _bytesSynced(0), _version(0) {
114 inline bool isSaving() {
return (_saveStream != 0); }
115 inline bool isLoading() {
return (_loadStream != 0); }
117 SYNC_AS(Byte, byte, 1)
118 SYNC_AS(SByte, int8, 1)
120 SYNC_AS(Uint16LE, uint16, 2)
121 SYNC_AS(Uint16BE, uint16, 2)
122 SYNC_AS(Sint16LE, int16, 2)
123 SYNC_AS(Sint16BE, int16, 2)
125 SYNC_AS(Uint32LE, uint32, 4)
126 SYNC_AS(Uint32BE, uint32, 4)
127 SYNC_AS(Sint32LE, int32, 4)
128 SYNC_AS(Sint32BE, int32, 4)
129 SYNC_AS(FloatLE,
float, 4)
130 SYNC_AS(FloatBE,
float, 4)
132 SYNC_AS(DoubleLE,
double, 8)
133 SYNC_AS(DoubleBE,
double, 8)
142 return _saveStream->
err();
144 return _loadStream->
err();
163 _version = currentVersion;
164 syncAsUint32LE(_version);
165 return _version <= currentVersion;
188 void skip(uint32 size, Version minVersion = 0, Version maxVersion = kLastVersion) {
189 if (_version < minVersion || _version > maxVersion)
192 _bytesSynced += size;
194 _loadStream->
skip(size);
204 void syncBytes(byte *buf, uint32 size, Version minVersion = 0, Version maxVersion = kLastVersion) {
205 if (_version < minVersion || _version > maxVersion)
209 _loadStream->
read(buf, size);
211 _saveStream->
write(buf, size);
212 _bytesSynced += size;
227 bool matchBytes(
const char *magic, byte size, Version minVersion = 0, Version maxVersion = kLastVersion) {
228 if (_version < minVersion || _version > maxVersion)
233 _saveStream->
write(magic, size);
237 _loadStream->
read(buf, size);
238 match = (0 == memcmp(buf, magic, size));
240 _bytesSynced += size;
249 if (_version < minVersion || _version > maxVersion)
255 while ((c = _loadStream->
readByte())) {
263 _bytesSynced += str.size() + 1;
271 if (_version < minVersion || _version > maxVersion)
274 uint32 len = str.size();
279 U32String::value_type *sl =
new U32String::value_type[len];
280 for (uint i = 0; i < len; i++)
281 syncAsUint32LE(sl[i]);
285 for (uint i = 0; i < len; i++)
287 _bytesSynced += 4 * len;
291 template <
typename T>
292 void syncArray(T *arr,
size_t entries,
void (*serializer)(
Serializer &, T &), Version minVersion = 0, Version maxVersion = kLastVersion) {
293 if (_version < minVersion || _version > maxVersion)
296 for (
size_t i = 0; i < entries; ++i) {
297 serializer(*
this, arr[i]);
302 #undef SYNC_PRIMITIVE 313 virtual void saveLoadWithSerializer(
Serializer &ser) = 0;
void syncBytes(byte *buf, uint32 size, Version minVersion=0, Version maxVersion=kLastVersion)
Definition: serializer.h:204
virtual bool err() const
Definition: stream.h:61
void writeUint32LE(uint32 value)
Definition: stream.h:159
virtual void clearErr()
Definition: stream.h:71
uint bytesSynced() const
Definition: serializer.h:182
void clearErr()
Definition: serializer.h:150
bool syncVersion(Version currentVersion)
Definition: serializer.h:162
Definition: serializer.h:79
byte readByte()
Definition: stream.h:434
void setVersion(Version version)
Definition: serializer.h:177
void syncString(String &str, Version minVersion=0, Version maxVersion=kLastVersion)
Definition: serializer.h:248
Definition: algorithm.h:29
bool err() const
Definition: serializer.h:140
Definition: serializer.h:308
void syncString32(U32String &str, Version minVersion=0, Version maxVersion=kLastVersion)
Definition: serializer.h:270
Version getVersion() const
Definition: serializer.h:172
bool matchBytes(const char *magic, byte size, Version minVersion=0, Version maxVersion=kLastVersion)
Definition: serializer.h:227
void skip(uint32 size, Version minVersion=0, Version maxVersion=kLastVersion)
Definition: serializer.h:188
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
void writeString(const String &str)
virtual uint32 read(void *dataPtr, uint32 dataSize)=0
virtual bool skip(uint32 offset)
Definition: stream.h:793
void writeByte(byte value)
Definition: stream.h:140