File: data-io.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (252 lines) | stat: -rw-r--r-- 8,581 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef COMMON_DATA_IO_H
#define COMMON_DATA_IO_H

#include "common/scummsys.h"

namespace Common {

enum class EndianStorageFormat {
	Big,
	Little,
};

// Traits for a storage format.  You can specialize this to make more storage formats
// that have their own behavior.
template<class TDataFormat>
struct DataFormatTraits {
};

template<>
struct DataFormatTraits<EndianStorageFormat> {
	static inline bool isLittleEndian(EndianStorageFormat storageFormat) {
		return storageFormat == EndianStorageFormat::Little;
	}
};

template<class TDataFormat, class T>
struct SimpleDataIO {
	static const uint kMaxSize = sizeof(T);

	static uint computeSize(TDataFormat dataFormat);

	static void encode(TDataFormat dataFormat, byte *data, const T &value);
	static void decode(TDataFormat dataFormat, const byte *data, T &value);
};

template<class TDataFormat, class T>
uint SimpleDataIO<TDataFormat, T>::computeSize(TDataFormat dataFormat) {
	return sizeof(T);
}

template<class TDataFormat, class T>
void SimpleDataIO<TDataFormat, T>::encode(TDataFormat dataFormat, byte *data, const T &value) {
	const byte *valueBytes = reinterpret_cast<const byte *>(&value);
	byte *dataBytes = reinterpret_cast<byte *>(data);

	const bool isTargetLE = DataFormatTraits<TDataFormat>::isLittleEndian(dataFormat);
#ifdef SCUMM_LITTLE_ENDIAN
	const bool isSystemLE = true;
#endif
#ifdef SCUMM_BIG_ENDIAN
	const bool isSystemLE = false;
#endif

	const bool requiresSwap = (isSystemLE != isTargetLE);

	byte temp[sizeof(T)];

	if (requiresSwap) {
		for (uint i = 0; i < sizeof(T); i++)
			temp[i] = valueBytes[sizeof(T) - 1 - i];
	} else {
		for (uint i = 0; i < sizeof(T); i++)
			temp[i] = valueBytes[i];
	}

	for (uint i = 0; i < sizeof(T); i++)
		dataBytes[i] = temp[i];
}

template<class TDataFormat, class T>
void SimpleDataIO<TDataFormat, T>::decode(TDataFormat dataFormat, const byte *data, T &value) {
	byte *valueBytes = reinterpret_cast<byte *>(&value);
	const byte *dataBytes = reinterpret_cast<const byte *>(data);

	const bool isTargetLE = DataFormatTraits<TDataFormat>::isLittleEndian(dataFormat);
#ifdef SCUMM_LITTLE_ENDIAN
	const bool isSystemLE = true;
#endif
#ifdef SCUMM_BIG_ENDIAN
	const bool isSystemLE = false;
#endif

	const bool requiresSwap = (isSystemLE != isTargetLE);

	byte temp[sizeof(T)];

	if (requiresSwap) {
		for (uint i = 0; i < sizeof(T); i++)
			temp[i] = dataBytes[sizeof(T) - 1 - i];
	} else {
		for (uint i = 0; i < sizeof(T); i++)
			temp[i] = dataBytes[i];
	}

	for (uint i = 0; i < sizeof(T); i++)
		valueBytes[i] = temp[i];
}

template<class TDataFormat, class T>
struct DataIO {
};

template<class TDataFormat>
struct DataIO<TDataFormat, uint8> : public SimpleDataIO<TDataFormat, uint8> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, uint16> : public SimpleDataIO<TDataFormat, uint16> {
};
template<class TDataFormat>
struct DataIO<TDataFormat, uint32> : public SimpleDataIO<TDataFormat, uint32> {
};
template<class TDataFormat>
struct DataIO<TDataFormat, uint64> : public SimpleDataIO<TDataFormat, uint64> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, int8> : public SimpleDataIO<TDataFormat, int8> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, int16> : public SimpleDataIO<TDataFormat, int16> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, int32> : public SimpleDataIO<TDataFormat, int32> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, int64> : public SimpleDataIO<TDataFormat, int64> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, char> : public SimpleDataIO<TDataFormat, char> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, float> : public SimpleDataIO<TDataFormat, float> {
};

template<class TDataFormat>
struct DataIO<TDataFormat, double> : public SimpleDataIO<TDataFormat, double> {
};

template<class TDataFormat, class T, class... TMore>
struct DataMultipleIO;

template<class TDataFormat, class T>
struct DataMultipleIO<TDataFormat, T> {
	static const uint kMaxSize = DataIO<TDataFormat, T>::kMaxSize;

	static uint computeSize(TDataFormat dataFormat);

	static void encode(TDataFormat dataFormat, byte *data, const T &value);
	static void decode(TDataFormat dataFormat, const byte *data, T &value);
};

template<class TDataFormat, class T>
uint DataMultipleIO<TDataFormat, T>::computeSize(TDataFormat dataFormat) {
	return DataIO<TDataFormat, T>::computeSize(dataFormat);
}

template<class TDataFormat, class T>
void DataMultipleIO<TDataFormat, T>::encode(TDataFormat dataFormat, byte *data, const T &value) {
	return DataIO<TDataFormat, T>::encode(dataFormat, data, value);
}

template<class TDataFormat, class T>
void DataMultipleIO<TDataFormat, T>::decode(TDataFormat dataFormat, const byte *data, T &value) {
	return DataIO<TDataFormat, T>::decode(dataFormat, data, value);
}

template<class TDataFormat, class T, uint TSize>
struct DataMultipleIO<TDataFormat, T[TSize]> {
	static const uint kMaxSize = DataIO<TDataFormat, T>::kMaxSize * TSize;

	static uint computeSize(TDataFormat dataFormat);

	static void encode(TDataFormat dataFormat, byte *data, const T (&value)[TSize]);
	static void decode(TDataFormat dataFormat, const byte *data, T (&value)[TSize]);
};

template<class TDataFormat, class T, uint TSize>
uint DataMultipleIO<TDataFormat, T[TSize]>::computeSize(TDataFormat dataFormat) {
	return DataMultipleIO<TDataFormat, T>::computeSize(dataFormat) * TSize;
}

template<class TDataFormat, class T, uint TSize>
void DataMultipleIO<TDataFormat, T[TSize]>::encode(TDataFormat dataFormat, byte *data, const T (&value)[TSize]) {
	const uint elementSize = DataIO<TDataFormat, T>::computeSize(dataFormat);
	for (uint i = 0; i < TSize; i++)
		DataMultipleIO<TDataFormat, T>::encode(dataFormat, data + elementSize * i, value[i]);
}

template<class TDataFormat, class T, uint TSize>
void DataMultipleIO<TDataFormat, T[TSize]>::decode(TDataFormat dataFormat, const byte *data, T (&value)[TSize]) {
	const uint elementSize = DataIO<TDataFormat, T>::computeSize(dataFormat);
	for (uint i = 0; i < TSize; i++)
		DataMultipleIO<TDataFormat, T>::decode(dataFormat, data + elementSize * i, value[i]);
}

template<class TDataFormat, class T, class... TMore>
struct DataMultipleIO {
	static const uint kMaxSize = DataIO<TDataFormat, T>::kMaxSize + DataMultipleIO<TDataFormat, TMore...>::kMaxSize;

	static uint computeSize(TDataFormat dataFormat);

	static void encode(TDataFormat dataFormat, byte *data, const T &firstValue, const TMore &...moreValues);
	static void decode(TDataFormat dataFormat, const byte *data, T &firstValue, TMore &...moreValues);
};

template<class TDataFormat, class T, class... TMore>
uint DataMultipleIO<TDataFormat, T, TMore...>::computeSize(TDataFormat dataFormat) {
	return DataMultipleIO<TDataFormat, T>::computeSize(dataFormat) + DataMultipleIO<TDataFormat, TMore...>::computeSize(dataFormat);
}

template<class TDataFormat, class T, class... TMore>
void DataMultipleIO<TDataFormat, T, TMore...>::encode(TDataFormat dataFormat, byte *data, const T &firstValue, const TMore &...moreValues) {
	DataMultipleIO<TDataFormat, T>::encode(dataFormat, data, firstValue);
	DataMultipleIO<TDataFormat, TMore...>::encode(dataFormat, data + DataMultipleIO<TDataFormat, T>::computeSize(dataFormat), moreValues...);
}

template<class TDataFormat, class T, class... TMore>
void DataMultipleIO<TDataFormat, T, TMore...>::decode(TDataFormat dataFormat, const byte *data, T &firstValue, TMore &...moreValues) {
	DataMultipleIO<TDataFormat, T>::decode(dataFormat, data, firstValue);
	DataMultipleIO<TDataFormat, TMore...>::decode(dataFormat, data + DataMultipleIO<TDataFormat, T>::computeSize(dataFormat), moreValues...);
}

} // End of namespace Common

#endif