From 3a10107f803d8c356ce220f2fa3e2b5502dcbf37 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 24 Jun 2004 18:31:11 +0000 Subject: [PATCH] add unpack-by-reference --- direct/src/dcparser/dcPacker.I | 129 ++++++++++++++++++++++ direct/src/dcparser/dcPacker.cxx | 3 +- direct/src/dcparser/dcPacker.h | 13 +++ direct/src/dcparser/dcSimpleParameter.cxx | 2 +- 4 files changed, 145 insertions(+), 2 deletions(-) diff --git a/direct/src/dcparser/dcPacker.I b/direct/src/dcparser/dcPacker.I index 8211e272ef..ae19fdf929 100755 --- a/direct/src/dcparser/dcPacker.I +++ b/direct/src/dcparser/dcPacker.I @@ -402,6 +402,135 @@ unpack_literal_value() { return string(_unpack_data + start, _unpack_p - start); } +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_double +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_double(double &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_double(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_int +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_int(int &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_int(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_uint +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_uint(unsigned int &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_uint(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_int64 +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_int64(PN_int64 &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_int64(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_uint64 +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_uint64(PN_uint64 &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_uint64(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_string +// Access: Public +// Description: Unpacks the current numeric or string value from the +// stream. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_string(string &value) { + nassertv(_mode == M_unpack); + if (_current_field == NULL) { + _pack_error = true; + + } else { + _current_field->unpack_string(_unpack_data, _unpack_length, _unpack_p, + value, _pack_error, _range_error); + advance(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: DCPacker::unpack_literal_value +// Access: Public +// Description: Returns the literal string that represents the packed +// value of the current field, and advances the field +// pointer. +//////////////////////////////////////////////////////////////////// +INLINE void DCPacker:: +unpack_literal_value(string &value) { + size_t start = _unpack_p; + unpack_skip(); + nassertv(_unpack_p >= start); + value.assign(_unpack_data + start, _unpack_p - start); +} + //////////////////////////////////////////////////////////////////// // Function: DCPacker::had_pack_error // Access: Published diff --git a/direct/src/dcparser/dcPacker.cxx b/direct/src/dcparser/dcPacker.cxx index 24aa9dc9af..6ffd0eca8d 100755 --- a/direct/src/dcparser/dcPacker.cxx +++ b/direct/src/dcparser/dcPacker.cxx @@ -689,7 +689,8 @@ unpack_object() { case PT_string: { - string str = unpack_string(); + string str; + unpack_string(str); object = PyString_FromStringAndSize(str.data(), str.size()); } break; diff --git a/direct/src/dcparser/dcPacker.h b/direct/src/dcparser/dcPacker.h index aa10232878..c24fb2cc63 100755 --- a/direct/src/dcparser/dcPacker.h +++ b/direct/src/dcparser/dcPacker.h @@ -92,6 +92,19 @@ PUBLISHED: void unpack_validate(); void unpack_skip(); +public: + // The following are variants on the above unpack() calls that pass + // the result back by reference instead of as a return value. + INLINE void unpack_double(double &value); + INLINE void unpack_int(int &value); + INLINE void unpack_uint(unsigned int &value); + INLINE void unpack_int64(PN_int64 &value); + INLINE void unpack_uint64(PN_uint64 &value); + INLINE void unpack_string(string &value); + INLINE void unpack_literal_value(string &value); + +PUBLISHED: + #ifdef HAVE_PYTHON void pack_object(PyObject *object); PyObject *unpack_object(); diff --git a/direct/src/dcparser/dcSimpleParameter.cxx b/direct/src/dcparser/dcSimpleParameter.cxx index 6f4380335b..3f0a4fdea8 100644 --- a/direct/src/dcparser/dcSimpleParameter.cxx +++ b/direct/src/dcparser/dcSimpleParameter.cxx @@ -1620,7 +1620,7 @@ unpack_string(const char *data, size_t length, size_t &p, string &value, pack_error = true; return; } - value = string(data + p, string_length); + value.assign(data + p, string_length); p += string_length; return;