forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcPackerInterface.I
More file actions
383 lines (342 loc) · 9.83 KB
/
dcPackerInterface.I
File metadata and controls
383 lines (342 loc) · 9.83 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file dcPackerInterface.I
* @author drose
* @date 2004-06-18
*/
/**
* Returns the name of this field, or empty string if the field is unnamed.
*/
INLINE const std::string &DCPackerInterface::
get_name() const {
return _name;
}
/**
* Returns true if the other interface is bitwise the same as this one--that
* is, a uint32 only matches a uint32, etc. Names of components, and range
* limits, are not compared.
*/
INLINE bool DCPackerInterface::
check_match(const DCPackerInterface *other) const {
return do_check_match(other);
}
/**
* Returns true if this field type always packs to the same number of bytes,
* false if it is variable.
*/
INLINE bool DCPackerInterface::
has_fixed_byte_size() const {
return _has_fixed_byte_size;
}
/**
* If has_fixed_byte_size() returns true, this returns the number of bytes
* this field type will use.
*/
INLINE size_t DCPackerInterface::
get_fixed_byte_size() const {
return _fixed_byte_size;
}
/**
* Returns true if this field type always has the same structure regardless of
* the data in the stream, or false if its structure may vary. This is
* almost, but not quite, the same thing as has_fixed_byte_size. The
* difference is that a DCSwitch may have multiple cases all with the same
* byte size, but they will still (presumably) have different structures, in
* the sense that the actual list of fields varies according to the live data.
*/
INLINE bool DCPackerInterface::
has_fixed_structure() const {
return _has_fixed_structure;
}
/**
* Returns true if this field, or any sub-field of this field, has a limit
* imposed in the DC file on its legal values. If this is false, then
* unpack_validate() is trivial.
*/
INLINE bool DCPackerInterface::
has_range_limits() const {
return _has_range_limits;
}
/**
* Returns the number of bytes that should be written into the stream on a
* push() to record the number of bytes in the record up until the next pop().
* This is only meaningful if _has_nested_fields is true.
*/
INLINE size_t DCPackerInterface::
get_num_length_bytes() const {
return _num_length_bytes;
}
/**
* Returns true if this field type has any nested fields (and thus expects a
* push() .. pop() interface to the DCPacker), or false otherwise. If this
* returns true, get_num_nested_fields() may be called to determine how many
* nested fields are expected.
*/
INLINE bool DCPackerInterface::
has_nested_fields() const {
return _has_nested_fields;
}
/**
* Returns the number of nested fields required by this field type. These may
* be array elements or structure elements. The return value may be -1 to
* indicate the number of nested fields is variable.
*/
INLINE int DCPackerInterface::
get_num_nested_fields() const {
return _num_nested_fields;
}
/**
* Returns the type of value expected by this field.
*/
INLINE DCPackType DCPackerInterface::
get_pack_type() const {
return _pack_type;
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_int8(char *buffer, int value) {
buffer[0] = (char)(value & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_int16(char *buffer, int value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_int32(char *buffer, int value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
buffer[2] = (char)((value >> 16) & 0xff);
buffer[3] = (char)((value >> 24) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_int64(char *buffer, int64_t value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
buffer[2] = (char)((value >> 16) & 0xff);
buffer[3] = (char)((value >> 24) & 0xff);
buffer[4] = (char)((value >> 32) & 0xff);
buffer[5] = (char)((value >> 40) & 0xff);
buffer[6] = (char)((value >> 48) & 0xff);
buffer[7] = (char)((value >> 56) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_uint8(char *buffer, unsigned int value) {
buffer[0] = (char)(value & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_uint16(char *buffer, unsigned int value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_uint32(char *buffer, unsigned int value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
buffer[2] = (char)((value >> 16) & 0xff);
buffer[3] = (char)((value >> 24) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_uint64(char *buffer, uint64_t value) {
buffer[0] = (char)(value & 0xff);
buffer[1] = (char)((value >> 8) & 0xff);
buffer[2] = (char)((value >> 16) & 0xff);
buffer[3] = (char)((value >> 24) & 0xff);
buffer[4] = (char)((value >> 32) & 0xff);
buffer[5] = (char)((value >> 40) & 0xff);
buffer[6] = (char)((value >> 48) & 0xff);
buffer[7] = (char)((value >> 56) & 0xff);
}
/**
*
*/
INLINE void DCPackerInterface::
do_pack_float64(char *buffer, double value) {
#ifdef WORDS_BIGENDIAN
// Reverse the byte ordering for big-endian machines.
char *p = (char *)&value;
for (size_t i = 0; i < 8; i++) {
buffer[i] = p[7 - i];
}
#else
memcpy(buffer, &value, 8);
#endif
}
/**
*
*/
INLINE int DCPackerInterface::
do_unpack_int8(const char *buffer) {
return (int)(signed char)buffer[0];
}
/**
*
*/
INLINE int DCPackerInterface::
do_unpack_int16(const char *buffer) {
return (int)((unsigned int)(unsigned char)buffer[0] |
((int)(signed char)buffer[1] << 8));
}
/**
*
*/
INLINE int DCPackerInterface::
do_unpack_int32(const char *buffer) {
return (int)((unsigned int)(unsigned char)buffer[0] |
((unsigned int)(unsigned char)buffer[1] << 8) |
((unsigned int)(unsigned char)buffer[2] << 16) |
((int)(signed char)buffer[3] << 24));
}
/**
*
*/
INLINE int64_t DCPackerInterface::
do_unpack_int64(const char *buffer) {
return (int64_t)((uint64_t)(unsigned char)buffer[0] |
((uint64_t)(unsigned char)buffer[1] << 8) |
((uint64_t)(unsigned char)buffer[2] << 16) |
((uint64_t)(unsigned char)buffer[3] << 24) |
((uint64_t)(unsigned char)buffer[4] << 32) |
((uint64_t)(unsigned char)buffer[5] << 40) |
((uint64_t)(unsigned char)buffer[6] << 48) |
((int64_t)(signed char)buffer[7] << 56));
}
/**
*
*/
INLINE unsigned int DCPackerInterface::
do_unpack_uint8(const char *buffer) {
return (unsigned int)(unsigned char)buffer[0];
}
/**
*
*/
INLINE unsigned int DCPackerInterface::
do_unpack_uint16(const char *buffer) {
return ((unsigned int)(unsigned char)buffer[0] |
((unsigned int)(unsigned char)buffer[1] << 8));
}
/**
*
*/
INLINE unsigned int DCPackerInterface::
do_unpack_uint32(const char *buffer) {
return ((unsigned int)(unsigned char)buffer[0] |
((unsigned int)(unsigned char)buffer[1] << 8) |
((unsigned int)(unsigned char)buffer[2] << 16) |
((unsigned int)(unsigned char)buffer[3] << 24));
}
/**
*
*/
INLINE uint64_t DCPackerInterface::
do_unpack_uint64(const char *buffer) {
return ((uint64_t)(unsigned char)buffer[0] |
((uint64_t)(unsigned char)buffer[1] << 8) |
((uint64_t)(unsigned char)buffer[2] << 16) |
((uint64_t)(unsigned char)buffer[3] << 24) |
((uint64_t)(unsigned char)buffer[4] << 32) |
((uint64_t)(unsigned char)buffer[5] << 40) |
((uint64_t)(unsigned char)buffer[6] << 48) |
((uint64_t)(unsigned char)buffer[7] << 56));
}
/**
*
*/
INLINE double DCPackerInterface::
do_unpack_float64(const char *buffer) {
#ifdef WORDS_BIGENDIAN
char reverse[8];
// Reverse the byte ordering for big-endian machines.
for (size_t i = 0; i < 8; i++) {
reverse[i] = buffer[7 - i];
}
return *(double *)reverse;
#else
return *(double *)buffer;
#endif // WORDS_BIGENDIAN
}
/**
* Confirms that the signed value fits within num_bits bits. Sets range_error
* true if it does not.
*/
INLINE void DCPackerInterface::
validate_int_limits(int value, int num_bits, bool &range_error) {
// What we're really checking is that all of the bits above the lower
// (num_bits - 1) bits are the same--either all 1 or all 0.
// First, turn on the lower (num_bits - 1).
int mask = ((int)1 << (num_bits - 1)) - 1;
value |= mask;
// The result should be either mask (all high bits are 0) or -1 (all high
// bits are 1). If it is anything else we have a range error.
if (value != mask && value != -1) {
range_error = true;
}
}
/**
* Confirms that the signed value fits within num_bits bits. Sets range_error
* true if it does not.
*/
INLINE void DCPackerInterface::
validate_int64_limits(int64_t value, int num_bits, bool &range_error) {
int64_t mask = ((int64_t)1 << (num_bits - 1)) - 1;
value |= mask;
if (value != mask && value != -1) {
range_error = true;
}
}
/**
* Confirms that the unsigned value fits within num_bits bits. Sets
* range_error true if it does not.
*/
INLINE void DCPackerInterface::
validate_uint_limits(unsigned int value, int num_bits, bool &range_error) {
// Here we're really checking that all of the bits above the lower num_bits
// bits are all 0.
unsigned int mask = ((unsigned int)1 << num_bits) - 1;
value &= ~mask;
if (value != 0) {
range_error = true;
}
}
/**
* Confirms that the unsigned value fits within num_bits bits. Sets
* range_error true if it does not.
*/
INLINE void DCPackerInterface::
validate_uint64_limits(uint64_t value, int num_bits, bool &range_error) {
uint64_t mask = ((uint64_t)1 << num_bits) - 1;
value &= ~mask;
if (value != 0) {
range_error = true;
}
}