15 #include <QtCore/QByteArray>
17 #include <glm/glm.hpp>
85 _start_pos = _parent.
pos();
89 size_t cur_pos = _parent.
pos();
91 if ( cur_pos >= _start_pos ) {
92 _parent._lastAdvance = cur_pos - _start_pos;
94 _parent._lastAdvance = 0;
100 size_t _start_pos = 0;
129 _store =
new char[_capacity];
145 _capacity = storeLength;
148 _storeIsExternal =
true;
149 _store = externalStore;
173 if (!_storeIsExternal) {
188 if (!extendBy(bytes,
"padding")) {
204 return *
this << int8_t(c);
214 if (!extendBy(1,
"int8_t")) {
232 return *
this << int16_t(val);
242 if (!extendBy(
sizeof(val),
"int16_t")) {
246 memcpy(&_store[_pos], (
char*)&val,
sizeof(val));
262 return *
this << int32_t(val);
272 if (!extendBy(
sizeof(val),
"int32_t")) {
276 memcpy(&_store[_pos], (
char*)&val,
sizeof(val));
290 return *
this << int64_t(val);
300 if (!extendBy(
sizeof(val),
"int64_t")) {
304 memcpy(&_store[_pos], (
char*)&val,
sizeof(val));
318 if (extendBy(
sizeof(val),
"float")) {
319 memcpy(&_store[_pos], (
char*)&val,
sizeof(val));
336 size_t sz =
sizeof(val.x);
337 if (extendBy(sz*3,
"glm::vec3")) {
338 memcpy(&_store[_pos ], (
char*)&val.x, sz);
339 memcpy(&_store[_pos + sz ], (
char*)&val.y, sz);
340 memcpy(&_store[_pos + sz*2], (
char*)&val.z, sz);
356 size_t sz =
sizeof(val.x);
357 if (extendBy(sz*4,
"glm::vec4")) {
358 memcpy(&_store[_pos ], (
char*)&val.x, sz);
359 memcpy(&_store[_pos + sz ], (
char*)&val.y, sz);
360 memcpy(&_store[_pos + sz*2], (
char*)&val.z, sz);
361 memcpy(&_store[_pos + sz*3], (
char*)&val.w, sz);
377 size_t sz =
sizeof(val.x);
378 if (extendBy(sz*2,
"glm::ivec2")) {
379 memcpy(&_store[_pos ], (
char*)&val.x, sz);
380 memcpy(&_store[_pos + sz ], (
char*)&val.y, sz);
399 size_t len = strlen(val)+1;
400 if (extendBy(len,
"string")) {
401 memcpy(&_store[_pos], val, len);
416 return *
this << val.toUtf8().constData();
438 size_t pos()
const {
return _pos; }
445 size_t length()
const {
return _length; }
480 void rewind() { _pos = 0; _overflow =
false; _lastAdvance = 0; }
506 bool extendBy(
size_t bytes,
const QString &type_name) {
509 if ( _capacity < _length + bytes) {
510 if ( _storeIsExternal ) {
511 qCritical() <<
"Serializer trying to write past end of output buffer of" << _capacity <<
"bytes. Error writing" << bytes <<
"bytes for" << type_name <<
" from position " << _pos <<
", length " << _length;
516 changeAllocation(_length + bytes);
520 _lastAdvance = bytes;
525 void changeAllocation(
size_t new_size);
528 bool _storeIsExternal =
false;
529 bool _overflow =
false;
530 size_t _capacity = 0;
533 size_t _lastAdvance = 0;
597 _start_pos = _parent.
pos();
601 size_t cur_pos = _parent.
pos();
603 if ( cur_pos >= _start_pos ) {
604 _parent._lastAdvance = cur_pos - _start_pos;
606 _parent._lastAdvance = 0;
612 size_t _start_pos = 0;
622 _length = storeLength;
624 _store = externalStore;
655 if (!canAdvanceBy(bytes,
"padding")) {
660 _lastAdvance = bytes;
671 return *
this >>
reinterpret_cast<int8_t&
>(c);
681 if ( canAdvanceBy(1,
"int8_t") ) {
683 _lastAdvance =
sizeof(c);
698 return *
this >>
reinterpret_cast<int16_t&
>(val);
708 if ( canAdvanceBy(
sizeof(val),
"int16_t") ) {
709 memcpy((
char*)&val, &_store[_pos],
sizeof(val));
711 _lastAdvance =
sizeof(val);
726 return *
this >>
reinterpret_cast<int32_t&
>(val);
736 if ( canAdvanceBy(
sizeof(val),
"int32_t") ) {
737 memcpy((
char*)&val, &_store[_pos],
sizeof(val));
739 _lastAdvance =
sizeof(val);
753 return *
this >>
reinterpret_cast<int64_t&
>(val);
763 if ( canAdvanceBy(
sizeof(val),
"int64_t") ) {
764 memcpy((
char*)&val, &_store[_pos],
sizeof(val));
766 _lastAdvance =
sizeof(val);
781 if ( canAdvanceBy(
sizeof(val),
"float") ) {
782 memcpy((
char*)&val, &_store[_pos],
sizeof(val));
784 _lastAdvance =
sizeof(val);
801 size_t sz =
sizeof(val.x);
803 if ( canAdvanceBy(sz*3,
"glm::vec3") ) {
804 memcpy((
char*)&val.x, &_store[_pos ], sz);
805 memcpy((
char*)&val.y, &_store[_pos + sz ], sz);
806 memcpy((
char*)&val.z, &_store[_pos + sz*2], sz);
809 _lastAdvance = sz * 3;
825 size_t sz =
sizeof(val.x);
827 if ( canAdvanceBy(sz*4,
"glm::vec4")) {
828 memcpy((
char*)&val.x, &_store[_pos ], sz);
829 memcpy((
char*)&val.y, &_store[_pos + sz ], sz);
830 memcpy((
char*)&val.z, &_store[_pos + sz*2], sz);
831 memcpy((
char*)&val.w, &_store[_pos + sz*3], sz);
849 size_t sz =
sizeof(val.x);
851 if ( canAdvanceBy(sz*2,
"glm::ivec2") ) {
852 memcpy((
char*)&val.x, &_store[_pos ], sz);
853 memcpy((
char*)&val.y, &_store[_pos + sz ], sz);
856 _lastAdvance = sz * 2;
873 const char *
buffer()
const {
return _store; }
880 size_t pos()
const {
return _pos; }
887 size_t length()
const {
return _length; }
911 void rewind() { _pos = 0; _overflow =
false; _lastAdvance = 0; }
936 bool canAdvanceBy(
size_t bytes,
const QString &type_name) {
939 if ( _length < _pos + bytes) {
940 qCritical() <<
"Deserializer trying to read past end of input buffer of" << _length <<
"bytes, reading" << bytes <<
"bytes for" << type_name <<
"from position " << _pos;
949 bool _overflow =
false;
952 size_t _lastAdvance = 0;
RAII tracker of advance position.
Definition: SerDes.h:594
Data deserializer.
Definition: SerDes.h:573
DataDeserializer & operator>>(int64_t &val)
Read an int64_t from the buffer.
Definition: SerDes.h:762
void rewind()
Reset the serializer to the start, clear overflow bit.
Definition: SerDes.h:911
DataDeserializer & operator>>(uint32_t &val)
Read an uint32_t from the buffer.
Definition: SerDes.h:725
DataDeserializer(const char *externalStore, size_t storeLength)
Construct a Deserializer *.
Definition: SerDes.h:621
DataDeserializer & operator>>(int16_t &val)
Read an int16_t from the buffer.
Definition: SerDes.h:707
DataDeserializer & operator>>(glm::vec3 &val)
Read a glm::vec3 from the buffer.
Definition: SerDes.h:800
friend QDebug operator<<(QDebug debug, const DataDeserializer &ds)
Dump the contents of this object into QDebug.
Definition: SerDes.cpp:62
DataDeserializer & operator>>(int32_t &val)
Read an int32_t from the buffer.
Definition: SerDes.h:735
size_t lastAdvance() const
Size of the last advance.
Definition: SerDes.h:921
void skipPadding(size_t bytes)
Skips padding in the input.
Definition: SerDes.h:654
bool isEmpty() const
Whether there's any data in the buffer.
Definition: SerDes.h:895
DataDeserializer & operator>>(float &val)
Read an float from the buffer.
Definition: SerDes.h:780
DataDeserializer(const DataSerializer &serializer)
Construct a new Deserializer reading data from a Serializer.
Definition: SerDes.h:645
DataDeserializer & operator>>(glm::ivec2 &val)
Read a glm::ivec2 from the buffer.
Definition: SerDes.h:848
DataDeserializer & operator>>(uint16_t &val)
Read an uint16_t from the buffer.
Definition: SerDes.h:697
DataDeserializer & operator>>(int8_t &c)
Read an int8_t from the buffer.
Definition: SerDes.h:680
bool isOverflow() const
The buffer size limit has been reached.
Definition: SerDes.h:905
DataDeserializer(const uint8_t *externalStore, size_t storeLength)
Construct a Deserializer.
Definition: SerDes.h:634
DataDeserializer & operator>>(uint64_t &val)
Read an uint64_t from the buffer.
Definition: SerDes.h:752
const char * buffer() const
Pointer to the start of the internal buffer.
Definition: SerDes.h:873
size_t length() const
Last position that was written to in the buffer. Starts at 0.
Definition: SerDes.h:887
DataDeserializer & operator>>(uint8_t &c)
Read an uint8_t from the buffer.
Definition: SerDes.h:670
DataDeserializer & operator>>(glm::vec4 &val)
Read a glm::vec4 from the buffer.
Definition: SerDes.h:824
size_t pos() const
Current position in the buffer. Starts at 0.
Definition: SerDes.h:880
RAII tracker of advance position.
Definition: SerDes.h:82
Data serializer.
Definition: SerDes.h:61
size_t lastAdvance() const
Size of the last advance.
Definition: SerDes.h:491
DataSerializer & operator<<(int16_t val)
Add an int16_t to the output.
Definition: SerDes.h:241
DataSerializer & operator<<(const char *val)
Write a null-terminated string into the buffer.
Definition: SerDes.h:398
DataSerializer & operator<<(uint16_t val)
Add an uint16_t to the output.
Definition: SerDes.h:231
DataSerializer & operator<<(glm::ivec2 val)
Add a glm::ivec2 to the output.
Definition: SerDes.h:376
DataSerializer & operator<<(glm::vec3 val)
Add an glm::vec3 to the output.
Definition: SerDes.h:335
void addPadding(size_t bytes)
Adds padding to the output.
Definition: SerDes.h:187
DataSerializer & operator<<(float val)
Add an float to the output.
Definition: SerDes.h:317
DataSerializer & operator<<(uint8_t c)
Add an uint8_t to the output.
Definition: SerDes.h:203
size_t pos() const
Current position in the buffer. Starts at 0.
Definition: SerDes.h:438
DataSerializer()
Construct a dynamically allocated serializer.
Definition: SerDes.h:125
DataSerializer & operator<<(uint64_t val)
Add an uint64_t to the output.
Definition: SerDes.h:289
bool isOverflow() const
The buffer size limit has been reached.
Definition: SerDes.h:474
static const int DEFAULT_SIZE
Default size for a dynamically allocated buffer.
Definition: SerDes.h:108
DataSerializer & operator<<(uint32_t val)
Add an uint32_t to the output.
Definition: SerDes.h:261
size_t length() const
Last position that was written to in the buffer. Starts at 0.
Definition: SerDes.h:445
size_t capacity() const
Current capacity of the buffer.
Definition: SerDes.h:456
DataSerializer & operator<<(int64_t val)
Add an int64_t to the output.
Definition: SerDes.h:299
static const char PADDING_CHAR
Character to use for padding.
Definition: SerDes.h:116
void rewind()
Reset the serializer to the start, clear overflow bit.
Definition: SerDes.h:480
DataSerializer & operator<<(int8_t c)
Add an int8_t to the output.
Definition: SerDes.h:213
DataSerializer & operator<<(const QString &val)
Write a QString into the buffer.
Definition: SerDes.h:415
bool isEmpty() const
Whether there's any data in the buffer.
Definition: SerDes.h:464
DataSerializer & operator<<(int32_t val)
Add an int32_t to the output.
Definition: SerDes.h:271
char * buffer() const
Pointer to the start of the internal buffer.
Definition: SerDes.h:431
DataSerializer(char *externalStore, size_t storeLength)
Construct a statically allocated serializer.
Definition: SerDes.h:144
DataSerializer & operator<<(glm::vec4 val)
Add a glm::vec4 to the output.
Definition: SerDes.h:355
DataSerializer(uint8_t *externalStore, size_t storeLength)
Construct a statically allocated serializer.
Definition: SerDes.h:163