From b2bc682693950eff6cd7af70af39a4d8c46aa366 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 9 May 2001 20:52:16 +0000 Subject: [PATCH] *** empty log message *** --- panda/src/express/Sources.pp | 20 +- panda/src/express/bigEndian.I | 91 ---- panda/src/express/bigEndian.h | 53 +- panda/src/express/datagram.I | 92 ++-- panda/src/express/datagram.h | 38 +- panda/src/express/datagramIterator.I | 404 +++++++++++++++ panda/src/express/datagramIterator.cxx | 472 ------------------ panda/src/express/datagramIterator.h | 48 +- panda/src/express/littleEndian.I | 92 ---- panda/src/express/littleEndian.cxx | 25 - panda/src/express/littleEndian.h | 55 +- panda/src/express/nativeNumericData.I | 58 +++ panda/src/express/nativeNumericData.cxx | 6 + panda/src/express/nativeNumericData.h | 49 ++ panda/src/express/reversedNumericData.I | 57 +++ ...{bigEndian.cxx => reversedNumericData.cxx} | 22 +- panda/src/express/reversedNumericData.h | 54 ++ 17 files changed, 769 insertions(+), 867 deletions(-) delete mode 100644 panda/src/express/bigEndian.I delete mode 100644 panda/src/express/littleEndian.I delete mode 100644 panda/src/express/littleEndian.cxx create mode 100644 panda/src/express/nativeNumericData.I create mode 100644 panda/src/express/nativeNumericData.cxx create mode 100644 panda/src/express/nativeNumericData.h create mode 100644 panda/src/express/reversedNumericData.I rename panda/src/express/{bigEndian.cxx => reversedNumericData.cxx} (50%) create mode 100644 panda/src/express/reversedNumericData.h diff --git a/panda/src/express/Sources.pp b/panda/src/express/Sources.pp index 7cca93c2c5..59a30d69aa 100644 --- a/panda/src/express/Sources.pp +++ b/panda/src/express/Sources.pp @@ -9,7 +9,7 @@ #define USE_NET yes #define SOURCES \ - bigEndian.I bigEndian.cxx bigEndian.h buffer.I buffer.cxx buffer.h \ + bigEndian.h buffer.I buffer.cxx buffer.h \ circBuffer.I circBuffer.h clockObject.I clockObject.cxx \ clockObject.h config_express.cxx config_express.h datagram.I \ datagram.cxx datagram.h datagramGenerator.I datagramGenerator.cxx \ @@ -20,13 +20,17 @@ datagramSink.I datagramSink.cxx datagramSink.h \ get_config_path.cxx get_config_path.h \ hashVal.I hashVal.cxx hashVal.h \ - indent.I indent.cxx indent.h littleEndian.I \ - littleEndian.cxx littleEndian.h memoryUsage.I memoryUsage.cxx \ + indent.I indent.cxx indent.h \ + littleEndian.h memoryUsage.I memoryUsage.cxx \ memoryUsage.h memoryUsagePointers.I memoryUsagePointers.cxx \ memoryUsagePointers.h multifile.I multifile.cxx multifile.h \ - namable.I namable.cxx namable.h numeric_types.h \ + namable.I namable.cxx namable.h \ + nativeNumericData.I nativeNumericData.cxx nativeNumericData.h \ + numeric_types.h \ pointerTo.I pointerTo.h referenceCount.I \ - referenceCount.cxx referenceCount.h tokenBoard.I tokenBoard.h \ + referenceCount.cxx referenceCount.h \ + reversedNumericData.I reversedNumericData.cxx reversedNumericData.h \ + tokenBoard.I tokenBoard.h \ trueClock.I trueClock.cxx trueClock.h typeHandle.I typeHandle.cxx \ typeHandle.h typedReferenceCount.I typedReferenceCount.cxx \ typedReferenceCount.h typedef.h error_utils.cxx error_utils.h @@ -36,7 +40,7 @@ patchfile.I patchfile.cxx patchfile.h #define INSTALL_HEADERS \ - bigEndian.I bigEndian.h buffer.I buffer.h circBuffer.I \ + bigEndian.h buffer.I buffer.h circBuffer.I \ circBuffer.h clockObject.I clockObject.h config_express.h \ datagram.I datagram.h datagramInputFile.I datagramInputFile.h \ datagramIterator.I datagramIterator.h \ @@ -44,11 +48,13 @@ datagramSink.I datagramSink.h datagramGenerator.I \ datagramGenerator.h get_config_path.h \ hashVal.I hashVal.h \ - indent.I indent.h littleEndian.I littleEndian.h \ + indent.I indent.h littleEndian.h \ memoryUsage.I memoryUsage.h memoryUsagePointers.I \ memoryUsagePointers.h multifile.I multifile.h \ + nativeNumericData.I nativeNumericData.h \ numeric_types.h \ pointerTo.I pointerTo.h referenceCount.I referenceCount.h \ + reversedNumericData.I reversedNumericData.h \ tokenBoard.h trueClock.I trueClock.h typeHandle.I typeHandle.h \ typedReferenceCount.I typedReferenceCount.h typedef.h \ namable.I namable.h tokenBoard.I patchfile.h patchfile.I \ diff --git a/panda/src/express/bigEndian.I b/panda/src/express/bigEndian.I deleted file mode 100644 index 23e0c2b9af..0000000000 --- a/panda/src/express/bigEndian.I +++ /dev/null @@ -1,91 +0,0 @@ -// Filename: bigEndian.I -// Created by: drose (23Aug00) -// -//////////////////////////////////////////////////////////////////// - -#ifdef IS_LITTLE_ENDIAN - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::Constructor -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE BigEndian:: -BigEndian(const string &string) { - reverse_assign(string.data(), string.length()); -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::Constructor -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE BigEndian:: -BigEndian(const char *data, size_t length) { - reverse_assign(data, length); -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::Copy Constructor -// Access: Public -// Description: Since this is assigned from another BigEndian, -// presumably already reversed, this does *not* reverse -// the byte order. -//////////////////////////////////////////////////////////////////// -INLINE BigEndian:: -BigEndian(const BigEndian &other) : _str(other._str) { -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::String Assignment Operator -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE void BigEndian:: -operator =(const string &string) { - reverse_assign(string.data(), string.length()); -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::Copy Assignment Operator -// Access: Public -// Description: Since this is assigned from another BigEndian, -// presumably already reversed, this does *not* reverse -// the byte order. -//////////////////////////////////////////////////////////////////// -INLINE void BigEndian:: -operator =(const BigEndian &other) { - _str = other._str; -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::string Typecast Operator -// Access: Public -// Description: Returns the data as a string. -//////////////////////////////////////////////////////////////////// -INLINE BigEndian:: -operator const string &() const { - return _str; -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::data -// Access: Public -// Description: Returns the data in the string. -//////////////////////////////////////////////////////////////////// -INLINE const char *BigEndian:: -data() const { - return _str.data(); -} - -//////////////////////////////////////////////////////////////////// -// Function: BigEndian::length -// Access: Public -// Description: Returns the number of bytes in the string. -//////////////////////////////////////////////////////////////////// -INLINE size_t BigEndian:: -length() const { - return _str.length(); -} - -#endif // IS_LITTLE_ENDIAN diff --git a/panda/src/express/bigEndian.h b/panda/src/express/bigEndian.h index 8c566c11e0..f7f6887365 100644 --- a/panda/src/express/bigEndian.h +++ b/panda/src/express/bigEndian.h @@ -9,52 +9,21 @@ #include #include "numeric_types.h" - -#ifdef IS_LITTLE_ENDIAN +#include "nativeNumericData.h" +#include "reversedNumericData.h" //////////////////////////////////////////////////////////////////// // Class : BigEndian -// Description : BigEndian is a special string-like class that -// automatically reverses the byte order when it is -// assigned from either a char buffer or a true -// string--but only when compiling for a littleendian -// architecture. On bigendian machines, BigEndian -// is defined to map directly to string. -// -// See also LittleEndian. +// Description : BigEndian is a special class that automatically +// reverses the byte-order of numeric values for +// little-endian machines, and passes them through +// unchanged for big-endian machines. //////////////////////////////////////////////////////////////////// -class EXPCL_PANDAEXPRESS BigEndian { -public: - INLINE BigEndian(const string &string); - INLINE BigEndian(const char *data, size_t length); - INLINE BigEndian(const BigEndian &other); - - INLINE void operator =(const string &string); - INLINE void operator =(const BigEndian &other); - - INLINE operator const string &() const; - INLINE const char *data() const; - INLINE size_t length() const; - -private: - void reverse_assign(const char *data, size_t length); - - string _str; -}; - -#else // !IS_LITTLE_ENDIAN - -#define BigEndian string - -#endif // IS_LITTLE_ENDIAN - -#include "bigEndian.I" +#ifdef IS_LITTLE_ENDIAN +typedef ReversedNumericData BigEndian; +#else +typedef NativeNumericData BigEndian; #endif - - - - - - +#endif diff --git a/panda/src/express/datagram.I b/panda/src/express/datagram.I index 97a9e76656..d788099ef8 100644 --- a/panda/src/express/datagram.I +++ b/panda/src/express/datagram.I @@ -67,8 +67,8 @@ add_bool(bool b) { // Description: Adds a signed 8-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_int8(PN_int8 int8) { - _message += LittleEndian((char *)&int8, sizeof(int8)); +add_int8(PN_int8 value) { + _message += (char)value; } //////////////////////////////////////////////////////////////////// @@ -77,8 +77,8 @@ add_int8(PN_int8 int8) { // Description: Adds an unsigned 8-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_uint8(PN_uint8 uint8) { - _message += LittleEndian((char *)&uint8, sizeof(uint8)); +add_uint8(PN_uint8 value) { + _message += (char)value; } //////////////////////////////////////////////////////////////////// @@ -87,8 +87,9 @@ add_uint8(PN_uint8 uint8) { // Description: Adds a signed 16-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_int16(PN_int16 int16) { - _message += LittleEndian((char *)&int16, sizeof(int16)); +add_int16(PN_int16 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -97,8 +98,9 @@ add_int16(PN_int16 int16) { // Description: Adds a signed 32-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_int32(PN_int32 int32) { - _message += LittleEndian((char *)&int32, sizeof(int32)); +add_int32(PN_int32 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -107,8 +109,9 @@ add_int32(PN_int32 int32) { // Description: Adds a signed 64-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_int64(PN_int64 int64) { - _message += LittleEndian((char *)&int64, sizeof(int64)); +add_int64(PN_int64 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -117,8 +120,9 @@ add_int64(PN_int64 int64) { // Description: Adds an unsigned 16-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_uint16(PN_uint16 uint16) { - _message += LittleEndian((char *)&uint16, sizeof(uint16)); +add_uint16(PN_uint16 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -127,8 +131,9 @@ add_uint16(PN_uint16 uint16) { // Description: Adds an unsigned 32-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_uint32(PN_uint32 uint32) { - _message += LittleEndian((char *)&uint32, sizeof(uint32)); +add_uint32(PN_uint32 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -137,8 +142,9 @@ add_uint32(PN_uint32 uint32) { // Description: Adds an unsigned 64-bit integer to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_uint64(PN_uint64 uint64) { - _message += LittleEndian((char *)&uint64, sizeof(uint64)); +add_uint64(PN_uint64 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -150,12 +156,13 @@ add_uint64(PN_uint64 uint64) { // special care is required. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_float32(float float32) { +add_float32(float value) { // For now, we assume the float format is portable across all // architectures we are concerned with. If we come across one that // is different, we will have to convert. - nassertv(sizeof(float32) == 4); - _message += LittleEndian((char *)&float32, sizeof(float32)); + nassertv(sizeof(value) == 4); + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -164,8 +171,9 @@ add_float32(float float32) { // Description: Adds a 64-bit floating-point number to the datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_float64(PN_float64 float64) { - _message += LittleEndian((char *)&float64, sizeof(float64)); +add_float64(PN_float64 value) { + LittleEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -175,8 +183,9 @@ add_float64(PN_float64 float64) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_int16(PN_int16 int16) { - _message += BigEndian((char *)&int16, sizeof(int16)); +add_be_int16(PN_int16 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -186,8 +195,9 @@ add_be_int16(PN_int16 int16) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_int32(PN_int32 int32) { - _message += BigEndian((char *)&int32, sizeof(int32)); +add_be_int32(PN_int32 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -197,8 +207,9 @@ add_be_int32(PN_int32 int32) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_int64(PN_int64 int64) { - _message += BigEndian((char *)&int64, sizeof(int64)); +add_be_int64(PN_int64 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -208,8 +219,9 @@ add_be_int64(PN_int64 int64) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_uint16(PN_uint16 uint16) { - _message += BigEndian((char *)&uint16, sizeof(uint16)); +add_be_uint16(PN_uint16 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -219,8 +231,9 @@ add_be_uint16(PN_uint16 uint16) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_uint32(PN_uint32 uint32) { - _message += BigEndian((char *)&uint32, sizeof(uint32)); +add_be_uint32(PN_uint32 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -230,8 +243,9 @@ add_be_uint32(PN_uint32 uint32) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_uint64(PN_uint64 uint64) { - _message += BigEndian((char *)&uint64, sizeof(uint64)); +add_be_uint64(PN_uint64 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -243,12 +257,13 @@ add_be_uint64(PN_uint64 uint64) { // different architectures, special care is required. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_float32(float float32) { +add_be_float32(float value) { // For now, we assume the float format is portable across all // architectures we are concerned with. If we come across one that // is different, we will have to convert. - nassertv(sizeof(float32) == 4); - _message += BigEndian((char *)&float32, sizeof(float32)); + nassertv(sizeof(value) == 4); + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// @@ -258,8 +273,9 @@ add_be_float32(float float32) { // datagram. //////////////////////////////////////////////////////////////////// INLINE void Datagram:: -add_be_float64(PN_float64 float64) { - _message += BigEndian((char *)&float64, sizeof(float64)); +add_be_float64(PN_float64 value) { + BigEndian s(&value, sizeof(value)); + s.append_to(_message, sizeof(value)); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/express/datagram.h b/panda/src/express/datagram.h index a57fe0a4ba..cb70d6d845 100644 --- a/panda/src/express/datagram.h +++ b/panda/src/express/datagram.h @@ -45,29 +45,29 @@ PUBLISHED: virtual void clear(); void dump_hex(ostream &out) const; - INLINE void add_bool(bool b); - INLINE void add_int8(PN_int8 int8); - INLINE void add_uint8(PN_uint8 uint8); + INLINE void add_bool(bool value); + INLINE void add_int8(PN_int8 value); + INLINE void add_uint8(PN_uint8 value); // The default numeric packing is little-endian. - INLINE void add_int16(PN_int16 int16); - INLINE void add_int32(PN_int32 int32); - INLINE void add_int64(PN_int64 int64); - INLINE void add_uint16(PN_uint16 uint16); - INLINE void add_uint32(PN_uint32 uint32); - INLINE void add_uint64(PN_uint64 uint64); - INLINE void add_float32(float float32); - INLINE void add_float64(PN_float64 float64); + INLINE void add_int16(PN_int16 value); + INLINE void add_int32(PN_int32 value); + INLINE void add_int64(PN_int64 value); + INLINE void add_uint16(PN_uint16 value); + INLINE void add_uint32(PN_uint32 value); + INLINE void add_uint64(PN_uint64 value); + INLINE void add_float32(float value); + INLINE void add_float64(PN_float64 value); // These functions pack numbers big-endian, in case that's desired. - INLINE void add_be_int16(PN_int16 int16); - INLINE void add_be_int32(PN_int32 int32); - INLINE void add_be_int64(PN_int64 int64); - INLINE void add_be_uint16(PN_uint16 uint16); - INLINE void add_be_uint32(PN_uint32 uint32); - INLINE void add_be_uint64(PN_uint64 uint64); - INLINE void add_be_float32(float float32); - INLINE void add_be_float64(PN_float64 float64); + INLINE void add_be_int16(PN_int16 value); + INLINE void add_be_int32(PN_int32 value); + INLINE void add_be_int64(PN_int64 value); + INLINE void add_be_uint16(PN_uint16 value); + INLINE void add_be_uint32(PN_uint32 value); + INLINE void add_be_uint64(PN_uint64 value); + INLINE void add_be_float32(float value); + INLINE void add_be_float64(PN_float64 value); INLINE void add_string(const string &str); INLINE void add_fixed_string(const string &str, size_t size); diff --git a/panda/src/express/datagramIterator.I b/panda/src/express/datagramIterator.I index afe3b4cef4..b2a3d0f683 100644 --- a/panda/src/express/datagramIterator.I +++ b/panda/src/express/datagramIterator.I @@ -60,3 +60,407 @@ operator = (const DatagramIterator ©) { INLINE DatagramIterator:: ~DatagramIterator() { } + +// Various ways to get data and increment the iterator... +// Cut-and-paste-orama + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_bool +// Access: Public +// Description: Extracts a boolean value. +//////////////////////////////////////////////////////////////////// +INLINE bool DatagramIterator:: +get_bool() { + return (get_uint8() != 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_int8 +// Access: Public +// Description: Extracts a signed 8-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int8 DatagramIterator:: +get_int8() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int8 tempvar = (PN_int8)_datagram->get_message()[_current_index]; + _current_index++; + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_uint8 +// Access: Public +// Description: Extracts an unsigned 8-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint8 DatagramIterator:: +get_uint8() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint8 tempvar = (PN_uint8)_datagram->get_message()[_current_index]; + _current_index++; + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_int16 +// Access: Public +// Description: Extracts a signed 16-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int16 DatagramIterator:: +get_int16() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int16 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_int32 +// Access: Public +// Description: Extracts a signed 32-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int32 DatagramIterator:: +get_int32() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int32 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_int64 +// Access: Public +// Description: Extracts a signed 64-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int64 DatagramIterator:: +get_int64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int64 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_uint16 +// Access: Public +// Description: Extracts an unsigned 16-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint16 DatagramIterator:: +get_uint16() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint16 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_uint32 +// Access: Public +// Description: Extracts an unsigned 32-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint32 DatagramIterator:: +get_uint32() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint32 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_uint64 +// Access: Public +// Description: Extracts an unsigned 64-bit integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint64 DatagramIterator:: +get_uint64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint64 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_float32 +// Access: Public +// Description: Extracts a 32-bit single-precision floating-point +// number. Since this kind of float is not necessarily +// portable across different architectures, special care +// is required. +//////////////////////////////////////////////////////////////////// +INLINE float DatagramIterator:: +get_float32() { + // For now, we assume the float format is portable across all + // architectures we are concerned with. If we come across one that + // is different, we will have to convert. + nassertr(sizeof(float) == 4, 0.0); + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0.0); + + float tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_float64 +// Access: Public +// Description: Extracts a 64-bit floating-point number. +//////////////////////////////////////////////////////////////////// +INLINE PN_float64 DatagramIterator:: +get_float64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0.0); + + PN_float64 tempvar; + LittleEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_int16 +// Access: Public +// Description: Extracts a signed 16-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int16 DatagramIterator:: +get_be_int16() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int16 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_int32 +// Access: Public +// Description: Extracts a signed 32-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int32 DatagramIterator:: +get_be_int32() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int32 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_int64 +// Access: Public +// Description: Extracts a signed 64-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_int64 DatagramIterator:: +get_be_int64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_int64 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_uint16 +// Access: Public +// Description: Extracts an unsigned 16-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint16 DatagramIterator:: +get_be_uint16() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint16 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_uint32 +// Access: Public +// Description: Extracts an unsigned 32-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint32 DatagramIterator:: +get_be_uint32() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint32 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_uint64 +// Access: Public +// Description: Extracts an unsigned 64-bit big-endian integer. +//////////////////////////////////////////////////////////////////// +INLINE PN_uint64 DatagramIterator:: +get_be_uint64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0); + + PN_uint64 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_float32 +// Access: Public +// Description: Extracts a 32-bit big-endian single-precision +// floating-point number. Since this kind of float is +// not necessarily portable across different +// architectures, special care is required. +//////////////////////////////////////////////////////////////////// +INLINE float DatagramIterator:: +get_be_float32() { + // For now, we assume the float format is portable across all + // architectures we are concerned with. If we come across one that + // is different, we will have to convert. + nassertr(sizeof(float) == 4, 0.0); + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0.0); + + float tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_be_float64 +// Access: Public +// Description: Extracts a 64-bit big-endian floating-point number. +//////////////////////////////////////////////////////////////////// +INLINE PN_float64 DatagramIterator:: +get_be_float64() { + nassertr(_datagram != (const Datagram *)NULL && + _current_index < _datagram->get_length(), 0.0); + + PN_float64 tempvar; + BigEndian s(_datagram->get_message(), _current_index, sizeof(tempvar)); + s.store_value(&tempvar, sizeof(tempvar)); + _current_index += sizeof(tempvar); + + return tempvar; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::skip_bytes +// Access: Public +// Description: Skips over the indicated number of bytes in the +// datagram. +//////////////////////////////////////////////////////////////////// +INLINE void DatagramIterator:: +skip_bytes(size_t size) { + nassertv((int)size >= 0); + nassertv(_current_index + size <= _datagram->get_length()); + _current_index += size; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_remaining_bytes +// Access: Public +// Description: Returns the remaining bytes in the datagram as a +// string, but does not extract them from the iterator. +//////////////////////////////////////////////////////////////////// +INLINE string DatagramIterator:: +get_remaining_bytes() const { + nassertr(_datagram != (const Datagram *)NULL && + _current_index <= _datagram->get_length(), ""); + return _datagram->get_message().substr(_current_index); +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_remaining_size +// Access: Public +// Description: Return the bytes left in the datagram. +//////////////////////////////////////////////////////////////////// +INLINE int DatagramIterator:: +get_remaining_size() const { + return _datagram->get_length() - _current_index; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_datagram +// Access: Public +// Description: Return the datagram of this iterator. +//////////////////////////////////////////////////////////////////// +const Datagram &DatagramIterator:: +get_datagram() const { + return *_datagram; +} + +//////////////////////////////////////////////////////////////////// +// Function: DatagramIterator::get_current_index +// Access: Public +// Description: Returns the current position within the datagram of the +// next piece of data to extract. +//////////////////////////////////////////////////////////////////// +INLINE size_t DatagramIterator:: +get_current_index() const { + return _current_index; +} diff --git a/panda/src/express/datagramIterator.cxx b/panda/src/express/datagramIterator.cxx index 4ebe530348..0bd4f071bf 100644 --- a/panda/src/express/datagramIterator.cxx +++ b/panda/src/express/datagramIterator.cxx @@ -8,422 +8,6 @@ #include -// Various ways to get data and increment the iterator... -// Cut-and-paste-orama - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_bool -// Access: Public -// Description: Extracts a boolean value. -//////////////////////////////////////////////////////////////////// -bool DatagramIterator:: -get_bool() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint8 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return (tempvar != 0); -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_int8 -// Access: Public -// Description: Extracts a signed 8-bit integer. -//////////////////////////////////////////////////////////////////// -PN_int8 DatagramIterator:: -get_int8() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int8 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_uint8 -// Access: Public -// Description: Extracts an unsigned 8-bit integer. -//////////////////////////////////////////////////////////////////// -PN_uint8 DatagramIterator:: -get_uint8() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint8 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_int16 -// Access: Public -// Description: Extracts a signed 16-bit integer. -//////////////////////////////////////////////////////////////////// -PN_int16 DatagramIterator:: -get_int16() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int16 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_int32 -// Access: Public -// Description: Extracts a signed 32-bit integer. -//////////////////////////////////////////////////////////////////// -PN_int32 DatagramIterator:: -get_int32() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int32 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_int64 -// Access: Public -// Description: Extracts a signed 64-bit integer. -//////////////////////////////////////////////////////////////////// -PN_int64 DatagramIterator:: -get_int64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int64 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_uint16 -// Access: Public -// Description: Extracts an unsigned 16-bit integer. -//////////////////////////////////////////////////////////////////// -PN_uint16 DatagramIterator:: -get_uint16() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint16 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_uint32 -// Access: Public -// Description: Extracts an unsigned 32-bit integer. -//////////////////////////////////////////////////////////////////// -PN_uint32 DatagramIterator:: -get_uint32() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint32 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_uint64 -// Access: Public -// Description: Extracts an unsigned 64-bit integer. -//////////////////////////////////////////////////////////////////// -PN_uint64 DatagramIterator:: -get_uint64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint64 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_float32 -// Access: Public -// Description: Extracts a 32-bit single-precision floating-point -// number. Since this kind of float is not necessarily -// portable across different architectures, special care -// is required. -//////////////////////////////////////////////////////////////////// -float DatagramIterator:: -get_float32() { - // For now, we assume the float format is portable across all - // architectures we are concerned with. If we come across one that - // is different, we will have to convert. - nassertr(sizeof(float) == 4, 0.0); - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0.0); - - float tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0.0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_float64 -// Access: Public -// Description: Extracts a 64-bit floating-point number. -//////////////////////////////////////////////////////////////////// -PN_float64 DatagramIterator:: -get_float64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0.0); - - PN_float64 tempvar; - LittleEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0.0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_int16 -// Access: Public -// Description: Extracts a signed 16-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_int16 DatagramIterator:: -get_be_int16() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int16 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_int32 -// Access: Public -// Description: Extracts a signed 32-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_int32 DatagramIterator:: -get_be_int32() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int32 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_int64 -// Access: Public -// Description: Extracts a signed 64-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_int64 DatagramIterator:: -get_be_int64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_int64 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_uint16 -// Access: Public -// Description: Extracts an unsigned 16-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_uint16 DatagramIterator:: -get_be_uint16() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint16 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_uint32 -// Access: Public -// Description: Extracts an unsigned 32-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_uint32 DatagramIterator:: -get_be_uint32() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint32 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_uint64 -// Access: Public -// Description: Extracts an unsigned 64-bit big-endian integer. -//////////////////////////////////////////////////////////////////// -PN_uint64 DatagramIterator:: -get_be_uint64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0); - - PN_uint64 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_float32 -// Access: Public -// Description: Extracts a 32-bit big-endian single-precision -// floating-point number. Since this kind of float is -// not necessarily portable across different -// architectures, special care is required. -//////////////////////////////////////////////////////////////////// -float DatagramIterator:: -get_be_float32() { - // For now, we assume the float format is portable across all - // architectures we are concerned with. If we come across one that - // is different, we will have to convert. - nassertr(sizeof(float) == 4, 0.0); - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0.0); - - float tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0.0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_be_float64 -// Access: Public -// Description: Extracts a 64-bit big-endian floating-point number. -//////////////////////////////////////////////////////////////////// -PN_float64 DatagramIterator:: -get_be_float64() { - nassertr(_datagram != (const Datagram *)NULL && - _current_index < _datagram->get_length(), 0.0); - - PN_float64 tempvar; - BigEndian s = - _datagram->get_message().substr(_current_index, sizeof(tempvar)); - - nassertr(s.length() == sizeof(tempvar), 0.0); - memcpy((void *)&tempvar, (void *)s.data(), sizeof(tempvar)); - _current_index += sizeof(tempvar); - - return tempvar; -} - //////////////////////////////////////////////////////////////////// // Function: DatagramIterator::get_string // Access: Public @@ -466,19 +50,6 @@ get_fixed_string(size_t size) { return s.substr(0, zero_byte); } -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::skip_bytes -// Access: Public -// Description: Skips over the indicated number of bytes in the -// datagram. -//////////////////////////////////////////////////////////////////// -void DatagramIterator:: -skip_bytes(size_t size) { - nassertv((int)size >= 0); - nassertv(_current_index + size <= _datagram->get_length()); - _current_index += size; -} - //////////////////////////////////////////////////////////////////// // Function: DatagramIterator::extract_bytes // Access: Public @@ -496,46 +67,3 @@ extract_bytes(size_t size) { return _datagram->get_message().substr(start, size); } -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_remaining_bytes -// Access: Public -// Description: Returns the remaining bytes in the datagram as a -// string, but does not extract them from the iterator. -//////////////////////////////////////////////////////////////////// -string DatagramIterator:: -get_remaining_bytes() const { - nassertr(_datagram != (const Datagram *)NULL && - _current_index <= _datagram->get_length(), ""); - return _datagram->get_message().substr(_current_index); -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_remaining_size -// Access: Public -// Description: Return the bytes left in the datagram. -//////////////////////////////////////////////////////////////////// -int DatagramIterator:: -get_remaining_size() const { - return _datagram->get_length() - _current_index; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_datagram -// Access: Public -// Description: Return the datagram of this iterator. -//////////////////////////////////////////////////////////////////// -const Datagram &DatagramIterator:: -get_datagram() const { - return *_datagram; -} - -//////////////////////////////////////////////////////////////////// -// Function: DatagramIterator::get_current_index -// Access: Public -// Description: Returns the current position within the datagram of the -// next piece of data to extract. -//////////////////////////////////////////////////////////////////// -size_t DatagramIterator:: -get_current_index() const { - return _current_index; -} diff --git a/panda/src/express/datagramIterator.h b/panda/src/express/datagramIterator.h index cb74749447..f5e9780f8b 100644 --- a/panda/src/express/datagramIterator.h +++ b/panda/src/express/datagramIterator.h @@ -25,39 +25,39 @@ PUBLISHED: INLINE void operator = (const DatagramIterator ©); INLINE ~DatagramIterator(); - bool get_bool(); - PN_int8 get_int8(); - PN_uint8 get_uint8(); + INLINE bool get_bool(); + INLINE PN_int8 get_int8(); + INLINE PN_uint8 get_uint8(); - PN_int16 get_int16(); - PN_int32 get_int32(); - PN_int64 get_int64(); - PN_uint16 get_uint16(); - PN_uint32 get_uint32(); - PN_uint64 get_uint64(); - float get_float32(); - PN_float64 get_float64(); + INLINE PN_int16 get_int16(); + INLINE PN_int32 get_int32(); + INLINE PN_int64 get_int64(); + INLINE PN_uint16 get_uint16(); + INLINE PN_uint32 get_uint32(); + INLINE PN_uint64 get_uint64(); + INLINE float get_float32(); + INLINE PN_float64 get_float64(); - PN_int16 get_be_int16(); - PN_int32 get_be_int32(); - PN_int64 get_be_int64(); - PN_uint16 get_be_uint16(); - PN_uint32 get_be_uint32(); - PN_uint64 get_be_uint64(); - float get_be_float32(); - PN_float64 get_be_float64(); + INLINE PN_int16 get_be_int16(); + INLINE PN_int32 get_be_int32(); + INLINE PN_int64 get_be_int64(); + INLINE PN_uint16 get_be_uint16(); + INLINE PN_uint32 get_be_uint32(); + INLINE PN_uint64 get_be_uint64(); + INLINE float get_be_float32(); + INLINE PN_float64 get_be_float64(); string get_string(); string get_fixed_string(size_t size); - void skip_bytes(size_t size); + INLINE void skip_bytes(size_t size); string extract_bytes(size_t size); - string get_remaining_bytes() const; - int get_remaining_size() const; + INLINE string get_remaining_bytes() const; + INLINE int get_remaining_size() const; - const Datagram &get_datagram() const; - size_t get_current_index() const; + INLINE const Datagram &get_datagram() const; + INLINE size_t get_current_index() const; private: const Datagram *_datagram; diff --git a/panda/src/express/littleEndian.I b/panda/src/express/littleEndian.I deleted file mode 100644 index 550daf4c6c..0000000000 --- a/panda/src/express/littleEndian.I +++ /dev/null @@ -1,92 +0,0 @@ -// Filename: littleEndian.I -// Created by: drose (09Feb00) -// -//////////////////////////////////////////////////////////////////// - - -#ifdef IS_BIG_ENDIAN - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::Constructor -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE LittleEndian:: -LittleEndian(const string &string) { - reverse_assign(string.data(), string.length()); -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::Constructor -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE LittleEndian:: -LittleEndian(const char *data, size_t length) { - reverse_assign(data, length); -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::Copy Constructor -// Access: Public -// Description: Since this is assigned from another LittleEndian, -// presumably already reversed, this does *not* reverse -// the byte order. -//////////////////////////////////////////////////////////////////// -INLINE LittleEndian:: -LittleEndian(const LittleEndian &other) : _str(other._str) { -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::String Assignment Operator -// Access: Public -// Description: Reverses the bytes in the indicated string. -//////////////////////////////////////////////////////////////////// -INLINE void LittleEndian:: -operator =(const string &string) { - reverse_assign(string.data(), string.length()); -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::Copy Assignment Operator -// Access: Public -// Description: Since this is assigned from another LittleEndian, -// presumably already reversed, this does *not* reverse -// the byte order. -//////////////////////////////////////////////////////////////////// -INLINE void LittleEndian:: -operator =(const LittleEndian &other) { - _str = other._str; -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::string Typecast Operator -// Access: Public -// Description: Returns the data as a string. -//////////////////////////////////////////////////////////////////// -INLINE LittleEndian:: -operator const string &() const { - return _str; -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::data -// Access: Public -// Description: Returns the data in the string. -//////////////////////////////////////////////////////////////////// -INLINE const char *LittleEndian:: -data() const { - return _str.data(); -} - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::length -// Access: Public -// Description: Returns the number of bytes in the string. -//////////////////////////////////////////////////////////////////// -INLINE size_t LittleEndian:: -length() const { - return _str.length(); -} - -#endif // IS_BIG_ENDIAN diff --git a/panda/src/express/littleEndian.cxx b/panda/src/express/littleEndian.cxx deleted file mode 100644 index 7c41185f62..0000000000 --- a/panda/src/express/littleEndian.cxx +++ /dev/null @@ -1,25 +0,0 @@ -// Filename: littleEndian.cxx -// Created by: drose (09Feb00) -// -//////////////////////////////////////////////////////////////////// - -#include "littleEndian.h" - -#ifdef IS_BIG_ENDIAN - -//////////////////////////////////////////////////////////////////// -// Function: LittleEndian::reverse_assign -// Access: Private -// Description: Actually does the data reversal. -//////////////////////////////////////////////////////////////////// -void LittleEndian:: -reverse_assign(const char *data, size_t length) { - _str = ""; - _str.reserve(length); - - for (size_t i = 0; i < length; i++) { - _str += data[length - 1 - i]; - } -} - -#endif // IS_BIG_ENDIAN diff --git a/panda/src/express/littleEndian.h b/panda/src/express/littleEndian.h index 85ef935013..b9aa0614f1 100644 --- a/panda/src/express/littleEndian.h +++ b/panda/src/express/littleEndian.h @@ -9,54 +9,21 @@ #include #include "numeric_types.h" - -#ifdef IS_BIG_ENDIAN +#include "nativeNumericData.h" +#include "reversedNumericData.h" //////////////////////////////////////////////////////////////////// // Class : LittleEndian -// Description : LittleEndian is a special string-like class that -// automatically reverses the byte order when it is -// assigned from either a char buffer or a true -// string--but only when compiling for a bigendian -// architecture. On littleendian machines, LittleEndian -// is defined to map directly to string. -// -// This is a sneaky interface to automatically handle -// numeric conversions so that network data is always -// sent littleendian. +// Description : LittleEndian is a special class that automatically +// reverses the byte-order of numeric values for +// big-endian machines, and passes them through +// unchanged for little-endian machines. //////////////////////////////////////////////////////////////////// -class EXPCL_PANDAEXPRESS LittleEndian { -public: - INLINE LittleEndian(const string &string); - INLINE LittleEndian(const char *data, size_t length); - INLINE LittleEndian(const LittleEndian &other); - - INLINE void operator =(const string &string); - INLINE void operator =(const LittleEndian &other); - - INLINE operator const string &() const; - INLINE const char *data() const; - INLINE size_t length() const; - -private: - void reverse_assign(const char *data, size_t length); - - string _str; -}; - -#else // !IS_BIG_ENDIAN - -#define LittleEndian string - -#endif // IS_BIG_ENDIAN - -#include "littleEndian.I" +#ifdef IS_LITTLE_ENDIAN +typedef NativeNumericData LittleEndian; +#else +typedef ReversedNumericData LittleEndian; #endif - - - - - - +#endif diff --git a/panda/src/express/nativeNumericData.I b/panda/src/express/nativeNumericData.I new file mode 100644 index 0000000000..7c4277d866 --- /dev/null +++ b/panda/src/express/nativeNumericData.I @@ -0,0 +1,58 @@ +// Filename: nativeNumericData.I +// Created by: drose (09May01) +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: NativeNumericData::Constructor +// Access: Public +// Description: This constructor accepts the address of a numeric +// variable, and its sizeof. +//////////////////////////////////////////////////////////////////// +INLINE NativeNumericData:: +NativeNumericData(const void *data, size_t) : + _source(data) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: NativeNumericData::Constructor +// Access: Public +// Description: This constructor accepts a string containing a packed +// numeric value, the offset within the string at which +// the numeric value starts, and the size of the numeric +// value. +// +// It is essential that the string not be destructed or +// modified as long as the NumericData object remains; +// it may just store a pointer into that string's +// internal buffer. +//////////////////////////////////////////////////////////////////// +INLINE NativeNumericData:: +NativeNumericData(const string &data, size_t start, size_t) { + _source = (void *)(data.data() + start); +} + +//////////////////////////////////////////////////////////////////// +// Function: NativeNumericData::store_value +// Access: Public +// Description: Copies the data, with byte reversal if appropriate, +// into the indicated numeric variable, whose address +// and sizeof are given. +//////////////////////////////////////////////////////////////////// +INLINE void NativeNumericData:: +store_value(void *dest, size_t length) { + memcpy(dest, _source, length); +} + +//////////////////////////////////////////////////////////////////// +// Function: NativeNumericData::append_to +// Access: Public +// Description: Packs the numeric data, with byte reversal if +// appropriate, onto the end of the given string. +//////////////////////////////////////////////////////////////////// +INLINE void NativeNumericData:: +append_to(string &dest, size_t length) { + dest.append((const char *)_source, length); +} diff --git a/panda/src/express/nativeNumericData.cxx b/panda/src/express/nativeNumericData.cxx new file mode 100644 index 0000000000..3d3d057685 --- /dev/null +++ b/panda/src/express/nativeNumericData.cxx @@ -0,0 +1,6 @@ +// Filename: nativeNumericData.cxx +// Created by: drose (09May01) +// +//////////////////////////////////////////////////////////////////// + +#include "nativeNumericData.h" diff --git a/panda/src/express/nativeNumericData.h b/panda/src/express/nativeNumericData.h new file mode 100644 index 0000000000..f372485913 --- /dev/null +++ b/panda/src/express/nativeNumericData.h @@ -0,0 +1,49 @@ +// Filename: nativeNumericData.h +// Created by: drose (09May01) +// +//////////////////////////////////////////////////////////////////// + +#ifndef NATIVENUMERICDATA_H +#define NATIVENUMERICDATA_H + +#include + +#include // for memcpy() + +//////////////////////////////////////////////////////////////////// +// Class : NativeNumericData +// Description : NativeNumericData and ReversedNumericData work +// together to provide a sneaky interface for +// automatically byte-swapping numbers, when necessary, +// to transparency support big-endian and little-endian +// architectures. +// +// Both of these classes provide interfaces that accept +// a pointer to a numeric variable and the size of the +// number, and they can append that data to the end of a +// string, or memcpy it into another location. +// +// The difference is that NativeNumericData simply +// passes everything through unchanged, while +// ReversedNumericData always byte-swaps everything. +// Otherwise, they have the same interface. +// +// The transparent part comes from LittleEndian and +// BigEndian, which are typedeffed to be one of these or +// the other, according to the machine's architecture. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAEXPRESS NativeNumericData { +public: + INLINE NativeNumericData(const void *data, size_t length); + INLINE NativeNumericData(const string &data, size_t start, size_t length); + + INLINE void store_value(void *dest, size_t length); + INLINE void append_to(string &dest, size_t length); + +private: + const void *_source; +}; + +#include "nativeNumericData.I" + +#endif diff --git a/panda/src/express/reversedNumericData.I b/panda/src/express/reversedNumericData.I new file mode 100644 index 0000000000..45241e0895 --- /dev/null +++ b/panda/src/express/reversedNumericData.I @@ -0,0 +1,57 @@ +// Filename: reversedNumericData.I +// Created by: drose (09May01) +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: ReversedNumericData::Constructor +// Access: Public +// Description: This constructor accepts the address of a numeric +// variable, and its sizeof. +//////////////////////////////////////////////////////////////////// +INLINE ReversedNumericData:: +ReversedNumericData(const void *data, size_t length) { + reverse_assign((const char *)data, length); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReversedNumericData::Constructor +// Access: Public +// Description: This constructor accepts a string containing a packed +// numeric value, the offset within the string at which +// the numeric value starts, and the size of the numeric +// value. +// +// It is essential that the string not be destructed or +// modified as long as the NumericData object remains; +// it may just store a pointer into that string's +// internal buffer. +//////////////////////////////////////////////////////////////////// +INLINE ReversedNumericData:: +ReversedNumericData(const string &data, size_t start, size_t length) { + reverse_assign(data.data() + start, length); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReversedNumericData::store_value +// Access: Public +// Description: Copies the data, with byte reversal if appropriate, +// into the indicated numeric variable, whose address +// and sizeof are given. +//////////////////////////////////////////////////////////////////// +INLINE void ReversedNumericData:: +store_value(void *dest, size_t length) { + memcpy(dest, _data, length); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReversedNumericData::append_to +// Access: Public +// Description: Packs the numeric data, with byte reversal if +// appropriate, onto the end of the given string. +//////////////////////////////////////////////////////////////////// +INLINE void ReversedNumericData:: +append_to(string &dest, size_t length) { + dest.append((const char *)_data, length); +} diff --git a/panda/src/express/bigEndian.cxx b/panda/src/express/reversedNumericData.cxx similarity index 50% rename from panda/src/express/bigEndian.cxx rename to panda/src/express/reversedNumericData.cxx index b128b68e0e..c8af1f6782 100644 --- a/panda/src/express/bigEndian.cxx +++ b/panda/src/express/reversedNumericData.cxx @@ -1,25 +1,21 @@ -// Filename: bigEndian.cxx -// Created by: drose (23Aug00) +// Filename: reversedNumericData.cxx +// Created by: drose (09May01) // //////////////////////////////////////////////////////////////////// -#include "bigEndian.h" +#include "reversedNumericData.h" -#ifdef IS_LITTLE_ENDIAN +#include //////////////////////////////////////////////////////////////////// -// Function: BigEndian::reverse_assign +// Function: ReversedNumericData::reverse_assign // Access: Private // Description: Actually does the data reversal. //////////////////////////////////////////////////////////////////// -void BigEndian:: -reverse_assign(const char *data, size_t length) { - _str = ""; - _str.reserve(length); - +void ReversedNumericData:: +reverse_assign(const char *source, size_t length) { + nassertv(length <= max_numeric_size); for (size_t i = 0; i < length; i++) { - _str += data[length - 1 - i]; + _data[i] = source[length - 1 - i]; } } - -#endif // IS_LITTLE_ENDIAN diff --git a/panda/src/express/reversedNumericData.h b/panda/src/express/reversedNumericData.h new file mode 100644 index 0000000000..ce74ad3062 --- /dev/null +++ b/panda/src/express/reversedNumericData.h @@ -0,0 +1,54 @@ +// Filename: reversedNumericData.h +// Created by: drose (09May01) +// +//////////////////////////////////////////////////////////////////// + +#ifndef REVERSEDNUMERICDATA_H +#define REVERSEDNUMERICDATA_H + +#include + +#include // for memcpy() + +// The maximum size of any numeric data type. At present, this is +// int64 and float64. +static const int max_numeric_size = 8; + +//////////////////////////////////////////////////////////////////// +// Class : ReversedNumericData +// Description : NativeNumericData and ReversedNumericData work +// together to provide a sneaky interface for +// automatically byte-swapping numbers, when necessary, +// to transparency support big-endian and little-endian +// architectures. +// +// Both of these classes provide interfaces that accept +// a pointer to a numeric variable and the size of the +// number, and they can append that data to the end of a +// string, or memcpy it into another location. +// +// The difference is that NativeNumericData simply +// passes everything through unchanged, while +// ReversedNumericData always byte-swaps everything. +// Otherwise, they have the same interface. +// +// The transparent part comes from LittleEndian and +// BigEndian, which are typedeffed to be one of these or +// the other, according to the machine's architecture. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAEXPRESS ReversedNumericData { +public: + INLINE ReversedNumericData(const void *data, size_t length); + INLINE ReversedNumericData(const string &data, size_t start, size_t length); + + INLINE void store_value(void *dest, size_t length); + INLINE void append_to(string &dest, size_t length); + +private: + void reverse_assign(const char *source, size_t length); + char _data[max_numeric_size]; +}; + +#include "reversedNumericData.I" + +#endif