summaryrefslogtreecommitdiffstats
path: root/src/save/SaveBuf.h
blob: 1ca0066afae99a0d8bf0771a56c89506d6fbcd56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once

#ifdef VALIDATE_SAVE_SIZE
extern int32 _saveBufCount;
#define INITSAVEBUF _saveBufCount = 0;
#define VALIDATESAVEBUF(b) assert(_saveBufCount == b);
#else
#define INITSAVEBUF
#define VALIDATESAVEBUF(b)
#endif

inline void
SkipSaveBuf(uint8 *&buf, int32 skip)
{
	buf += skip;
#ifdef VALIDATE_SAVE_SIZE
	_saveBufCount += skip;
#endif
}

inline void
SkipSaveBuf(uint8*& buf, uint32 &length, int32 skip)
{
	buf += skip;
	length += skip;
#ifdef VALIDATE_SAVE_SIZE
	_saveBufCount += skip;
#endif
}

template<typename T>
inline void
ReadSaveBuf(T *out, uint8 *&buf)
{
	*out = *(T *)buf;
	SkipSaveBuf(buf, sizeof(T));
}

template<typename T>
inline void
ReadSaveBuf(T *out, uint8 *&buf, uint32 &length)
{
	*out = *(T *)buf;
	SkipSaveBuf(buf, length, sizeof(T));
}

template<typename T>
inline T *
WriteSaveBuf(uint8 *&buf, const T &value)
{
	T *p = (T*)buf;
	*p = value;
	SkipSaveBuf(buf, sizeof(T));
	return p;
}

template<typename T>
inline T *
WriteSaveBuf(uint8 *&buf, uint32 &length, const T &value)
{
	T *p = (T*)buf;
	*p = value;
	SkipSaveBuf(buf, length, sizeof(T));
	return p;
}


#define SAVE_HEADER_SIZE (4*sizeof(char)+sizeof(uint32))

#define WriteSaveHeader(buf,a,b,c,d,size) \
	WriteSaveBuf(buf, a);\
	WriteSaveBuf(buf, b);\
	WriteSaveBuf(buf, c);\
	WriteSaveBuf(buf, d);\
	WriteSaveBuf<uint32>(buf, size);

#define WriteSaveHeaderWithLength(buf,len,a,b,c,d,size) \
	WriteSaveBuf(buf, len, a);\
	WriteSaveBuf(buf, len, b);\
	WriteSaveBuf(buf, len, c);\
	WriteSaveBuf(buf, len, d);\
	WriteSaveBuf(buf, len, (uint32)(size));

#ifdef VALIDATE_SAVE_SIZE
#define CheckSaveHeader(buf, a, b, c, d, size) do { \
	char _C; uint32 _size;\
	ReadSaveBuf(&_C, buf);\
	assert(_C == a);\
	ReadSaveBuf(&_C, buf);\
	assert(_C == b);\
	ReadSaveBuf(&_C, buf);\
	assert(_C == c);\
	ReadSaveBuf(&_C, buf);\
	assert(_C == d);\
	ReadSaveBuf(&_size, buf);\
	assert(_size == size);\
	} while(0)

#define CheckSaveHeaderWithLength(buf,len,a,b,c,d,size) do { \
	char _C; uint32 _size;\
	ReadSaveBuf(&_C, buf, len);\
	assert(_C == a);\
	ReadSaveBuf(&_C, buf, len);\
	assert(_C == b);\
	ReadSaveBuf(&_C, buf, len);\
	assert(_C == c);\
	ReadSaveBuf(&_C, buf, len);\
	assert(_C == d);\
	ReadSaveBuf(&_size, buf, len);\
	assert(_size == size);\
	} while(0)
#else
#define CheckSaveHeader(buf, a, b, c, d, size) SkipSaveBuf(buf, 8);
#define CheckSaveHeaderWithLength(buf, a, b, c, d, size) SkipSaveBuf(buf, 8);
#endif