Panda3D

datagramIterator.I

00001 // Filename: datagramIterator.I
00002 // Created by:  drose (08May01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: DatagramIterator::Constructor
00018 //       Access: Public
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE DatagramIterator::
00022 DatagramIterator() :
00023     _datagram((Datagram *)NULL),
00024     _current_index(0) {
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: DatagramIterator::Constructor
00029 //       Access: Public
00030 //  Description:
00031 ////////////////////////////////////////////////////////////////////
00032 INLINE DatagramIterator::
00033 DatagramIterator(const Datagram &datagram, size_t offset) :
00034     _datagram(&datagram),
00035     _current_index(offset) {
00036   nassertv(_current_index <= _datagram->get_length());
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: DatagramIterator::Copy Constructor
00041 //       Access: Public
00042 //  Description:
00043 ////////////////////////////////////////////////////////////////////
00044 INLINE DatagramIterator::
00045 DatagramIterator(const DatagramIterator &copy) :
00046     _datagram(copy._datagram),
00047   _current_index(copy._current_index) {
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: DatagramIterator::Copy Assignment Operator
00052 //       Access: Public
00053 //  Description:
00054 ////////////////////////////////////////////////////////////////////
00055 INLINE void DatagramIterator::
00056 operator = (const DatagramIterator &copy) {
00057   _datagram = copy._datagram;
00058   _current_index = copy._current_index;
00059 }
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: DatagramIterator::assign
00062 //       Access: Public
00063 //  Description: direct Assignment to a Datagram
00064 ////////////////////////////////////////////////////////////////////
00065 INLINE void DatagramIterator::assign(Datagram &datagram, size_t offset)
00066 {
00067     _datagram =&datagram;
00068     _current_index = offset;     
00069 }
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: DatagramIterator::Destructor
00072 //       Access: Public
00073 //  Description:
00074 ////////////////////////////////////////////////////////////////////
00075 INLINE DatagramIterator::
00076 ~DatagramIterator() {
00077 }
00078 
00079 // Various ways to get data and increment the iterator...
00080 // Cut-and-paste-orama
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: DatagramIterator::get_bool
00084 //       Access: Public
00085 //  Description: Extracts a boolean value.
00086 ////////////////////////////////////////////////////////////////////
00087 INLINE bool DatagramIterator::
00088 get_bool() {
00089   return get_uint8() != 0;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: DatagramIterator::get_int8
00094 //       Access: Public
00095 //  Description: Extracts a signed 8-bit integer.
00096 ////////////////////////////////////////////////////////////////////
00097 INLINE PN_int8 DatagramIterator::
00098 get_int8() {
00099   nassertr(_datagram != (const Datagram *)NULL, 0);
00100   // Avoid reading junk data off the end of the datagram:
00101   nassertr(_current_index < _datagram->get_length(), 0);
00102   // Get the Data:
00103   const char *ptr = (const char *)_datagram->get_data();
00104   PN_int8 tempvar = (PN_int8)ptr[_current_index];
00105   ++_current_index;
00106 
00107   return tempvar;
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: DatagramIterator::get_uint8
00112 //       Access: Public
00113 //  Description: Extracts an unsigned 8-bit integer.
00114 ////////////////////////////////////////////////////////////////////
00115 INLINE PN_uint8 DatagramIterator::
00116 get_uint8() {
00117   nassertr(_datagram != (const Datagram *)NULL, 0);
00118   // Avoid reading junk data off the end of the datagram:
00119   nassertr(_current_index < _datagram->get_length(), 0);
00120   // Get the Data:
00121   const char *ptr = (const char *)_datagram->get_data();
00122   PN_uint8 tempvar = (PN_uint8)ptr[_current_index];
00123   ++_current_index;
00124 
00125   return tempvar;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: DatagramIterator::get_int16
00130 //       Access: Public
00131 //  Description: Extracts a signed 16-bit integer.
00132 ////////////////////////////////////////////////////////////////////
00133 INLINE PN_int16 DatagramIterator::
00134 get_int16() {
00135   nassertr(_datagram != (const Datagram *)NULL, 0);
00136   nassertr(_current_index < _datagram->get_length(), 0);
00137 
00138   PN_int16 tempvar;
00139   // Avoid reading junk data off the end of the datagram:
00140   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00141   // Get the Data:
00142   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00143   s.store_value(&tempvar, sizeof(tempvar));
00144   _current_index += sizeof(tempvar);
00145 
00146   return tempvar;
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: DatagramIterator::get_int32
00151 //       Access: Public
00152 //  Description: Extracts a signed 32-bit integer.
00153 ////////////////////////////////////////////////////////////////////
00154 INLINE PN_int32 DatagramIterator::
00155 get_int32() {
00156   nassertr(_datagram != (const Datagram *)NULL, 0);
00157   nassertr(_current_index < _datagram->get_length(), 0);
00158 
00159   PN_int32 tempvar;
00160   // Avoid reading junk data off the end of the datagram:
00161   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00162   // Get the Data:
00163   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00164   s.store_value(&tempvar, sizeof(tempvar));
00165   _current_index += sizeof(tempvar);
00166 
00167   return tempvar;
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: DatagramIterator::get_int64
00172 //       Access: Public
00173 //  Description: Extracts a signed 64-bit integer.
00174 ////////////////////////////////////////////////////////////////////
00175 INLINE PN_int64 DatagramIterator::
00176 get_int64() {
00177   nassertr(_datagram != (const Datagram *)NULL, 0);
00178   nassertr(_current_index < _datagram->get_length(), 0);
00179 
00180   PN_int64 tempvar;
00181   // Avoid reading junk data off the end of the datagram:
00182   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00183   // Get the Data:
00184   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00185   s.store_value(&tempvar, sizeof(tempvar));
00186   _current_index += sizeof(tempvar);
00187 
00188   return tempvar;
00189 }
00190 
00191 ////////////////////////////////////////////////////////////////////
00192 //     Function: DatagramIterator::get_uint16
00193 //       Access: Public
00194 //  Description: Extracts an unsigned 16-bit integer.
00195 ////////////////////////////////////////////////////////////////////
00196 INLINE PN_uint16 DatagramIterator::
00197 get_uint16() {
00198   nassertr(_datagram != (const Datagram *)NULL, 0);
00199   nassertr(_current_index < _datagram->get_length(), 0);
00200 
00201   PN_uint16 tempvar;  
00202   // Avoid reading junk data off the end of the datagram:
00203   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00204   // Get the Data:
00205   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00206   s.store_value(&tempvar, sizeof(tempvar));
00207   _current_index += sizeof(tempvar);
00208 
00209   return tempvar;
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: DatagramIterator::get_uint32
00214 //       Access: Public
00215 //  Description: Extracts an unsigned 32-bit integer.
00216 ////////////////////////////////////////////////////////////////////
00217 INLINE PN_uint32 DatagramIterator::
00218 get_uint32() {
00219   nassertr(_datagram != (const Datagram *)NULL, 0);
00220   nassertr(_current_index < _datagram->get_length(), 0);
00221 
00222   PN_uint32 tempvar;
00223   // Avoid reading junk data off the end of the datagram:
00224   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00225   // Get the Data:
00226   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00227   s.store_value(&tempvar, sizeof(tempvar));
00228   _current_index += sizeof(tempvar);
00229 
00230   return tempvar;
00231 }
00232 
00233 ////////////////////////////////////////////////////////////////////
00234 //     Function: DatagramIterator::get_uint64
00235 //       Access: Public
00236 //  Description: Extracts an unsigned 64-bit integer.
00237 ////////////////////////////////////////////////////////////////////
00238 INLINE PN_uint64 DatagramIterator::
00239 get_uint64() {
00240   nassertr(_datagram != (const Datagram *)NULL, 0);
00241   nassertr(_current_index < _datagram->get_length(), 0);
00242 
00243   PN_uint64 tempvar;
00244   // Avoid reading junk data off the end of the datagram:
00245   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00246   // Get the Data:
00247   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00248   s.store_value(&tempvar, sizeof(tempvar));
00249   _current_index += sizeof(tempvar);
00250 
00251   return tempvar;
00252 }
00253 
00254 ////////////////////////////////////////////////////////////////////
00255 //     Function: DatagramIterator::get_float32
00256 //       Access: Public
00257 //  Description: Extracts a 32-bit single-precision floating-point
00258 //               number.
00259 ////////////////////////////////////////////////////////////////////
00260 INLINE PN_float32 DatagramIterator::
00261 get_float32() {
00262   nassertr(_datagram != (const Datagram *)NULL, 0.0);
00263   nassertr(_current_index < _datagram->get_length(), 0.0);
00264 
00265   PN_float32 tempvar;
00266   // Avoid reading junk data off the end of the datagram:
00267   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
00268   // Get the Data:
00269   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00270   s.store_value(&tempvar, sizeof(tempvar));
00271   _current_index += sizeof(tempvar);
00272 
00273   return tempvar;
00274 }
00275 
00276 ////////////////////////////////////////////////////////////////////
00277 //     Function: DatagramIterator::get_float64
00278 //       Access: Public
00279 //  Description: Extracts a 64-bit floating-point number.
00280 ////////////////////////////////////////////////////////////////////
00281 INLINE PN_float64 DatagramIterator::
00282 get_float64() {
00283   nassertr(_datagram != (const Datagram *)NULL, 0.0);
00284   nassertr(_current_index < _datagram->get_length(), 0.0);
00285 
00286   PN_float64 tempvar;
00287   // Avoid reading junk data off the end of the datagram:
00288   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
00289   // Get the Data:
00290   LittleEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00291   s.store_value(&tempvar, sizeof(tempvar));
00292   _current_index += sizeof(tempvar);
00293 
00294   return tempvar;
00295 }
00296 
00297 
00298 ////////////////////////////////////////////////////////////////////
00299 //     Function: DatagramIterator::get_stdfloat
00300 //       Access: Public
00301 //  Description: Extracts either a 32-bit or a 64-bit floating-point
00302 //               number, according to Datagram::set_stdfloat_double().
00303 ////////////////////////////////////////////////////////////////////
00304 INLINE PN_stdfloat DatagramIterator::
00305 get_stdfloat() {
00306   nassertr(_datagram != (const Datagram *)NULL, 0.0);
00307   if (_datagram->get_stdfloat_double()) {
00308     return (PN_stdfloat)get_float64();
00309   } else {
00310     return (PN_stdfloat)get_float32();
00311   }
00312 }
00313 
00314 ////////////////////////////////////////////////////////////////////
00315 //     Function: DatagramIterator::get_be_int16
00316 //       Access: Public
00317 //  Description: Extracts a signed 16-bit big-endian integer.
00318 ////////////////////////////////////////////////////////////////////
00319 INLINE PN_int16 DatagramIterator::
00320 get_be_int16() {
00321   nassertr(_datagram != (const Datagram *)NULL, 0);
00322   nassertr(_current_index < _datagram->get_length(), 0);
00323 
00324   PN_int16 tempvar;
00325   // Avoid reading junk data off the end of the datagram:
00326   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00327   // Get the Data:
00328   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00329   s.store_value(&tempvar, sizeof(tempvar));
00330   _current_index += sizeof(tempvar);
00331 
00332   return tempvar;
00333 }
00334 
00335 ////////////////////////////////////////////////////////////////////
00336 //     Function: DatagramIterator::get_be_int32
00337 //       Access: Public
00338 //  Description: Extracts a signed 32-bit big-endian integer.
00339 ////////////////////////////////////////////////////////////////////
00340 INLINE PN_int32 DatagramIterator::
00341 get_be_int32() {
00342   nassertr(_datagram != (const Datagram *)NULL, 0);
00343   nassertr(_current_index < _datagram->get_length(), 0);
00344 
00345   PN_int32 tempvar;
00346   // Avoid reading junk data off the end of the datagram:
00347   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00348   // Get the Data:
00349   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00350   s.store_value(&tempvar, sizeof(tempvar));
00351   _current_index += sizeof(tempvar);
00352 
00353   return tempvar;
00354 }
00355 
00356 ////////////////////////////////////////////////////////////////////
00357 //     Function: DatagramIterator::get_be_int64
00358 //       Access: Public
00359 //  Description: Extracts a signed 64-bit big-endian integer.
00360 ////////////////////////////////////////////////////////////////////
00361 INLINE PN_int64 DatagramIterator::
00362 get_be_int64() {
00363   nassertr(_datagram != (const Datagram *)NULL, 0);
00364   nassertr(_current_index < _datagram->get_length(), 0);
00365 
00366   PN_int64 tempvar;
00367   // Avoid reading junk data off the end of the datagram:
00368   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00369   // Get the Data:
00370   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00371   s.store_value(&tempvar, sizeof(tempvar));
00372   _current_index += sizeof(tempvar);
00373 
00374   return tempvar;
00375 }
00376 
00377 ////////////////////////////////////////////////////////////////////
00378 //     Function: DatagramIterator::get_be_uint16
00379 //       Access: Public
00380 //  Description: Extracts an unsigned 16-bit big-endian integer.
00381 ////////////////////////////////////////////////////////////////////
00382 INLINE PN_uint16 DatagramIterator::
00383 get_be_uint16() {
00384   nassertr(_datagram != (const Datagram *)NULL, 0);
00385   nassertr(_current_index < _datagram->get_length(), 0);
00386 
00387   PN_uint16 tempvar;
00388   // Avoid reading junk data off the end of the datagram:
00389   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00390   // Get the Data:
00391   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00392   s.store_value(&tempvar, sizeof(tempvar));
00393   _current_index += sizeof(tempvar);
00394 
00395   return tempvar;
00396 }
00397 
00398 ////////////////////////////////////////////////////////////////////
00399 //     Function: DatagramIterator::get_be_uint32
00400 //       Access: Public
00401 //  Description: Extracts an unsigned 32-bit big-endian integer.
00402 ////////////////////////////////////////////////////////////////////
00403 INLINE PN_uint32 DatagramIterator::
00404 get_be_uint32() {
00405   nassertr(_datagram != (const Datagram *)NULL, 0);
00406   nassertr(_current_index < _datagram->get_length(), 0);
00407 
00408   PN_uint32 tempvar;
00409   // Avoid reading junk data off the end of the datagram:
00410   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00411   // Get the Data:
00412   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00413   s.store_value(&tempvar, sizeof(tempvar));
00414   _current_index += sizeof(tempvar);
00415 
00416   return tempvar;
00417 }
00418 
00419 ////////////////////////////////////////////////////////////////////
00420 //     Function: DatagramIterator::get_be_uint64
00421 //       Access: Public
00422 //  Description: Extracts an unsigned 64-bit big-endian integer.
00423 ////////////////////////////////////////////////////////////////////
00424 INLINE PN_uint64 DatagramIterator::
00425 get_be_uint64() {
00426   nassertr(_datagram != (const Datagram *)NULL, 0);
00427   nassertr(_current_index < _datagram->get_length(), 0);
00428 
00429   PN_uint64 tempvar;
00430   // Avoid reading junk data off the end of the datagram:
00431   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00432   // Get the Data:
00433   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00434   s.store_value(&tempvar, sizeof(tempvar));
00435   _current_index += sizeof(tempvar);
00436 
00437   return tempvar;
00438 }
00439 
00440 ////////////////////////////////////////////////////////////////////
00441 //     Function: DatagramIterator::get_be_float32
00442 //       Access: Public
00443 //  Description: Extracts a 32-bit big-endian single-precision
00444 //               floating-point number.
00445 ////////////////////////////////////////////////////////////////////
00446 INLINE PN_float32 DatagramIterator::
00447 get_be_float32() {
00448   nassertr(_datagram != (const Datagram *)NULL, 0.0);
00449   nassertr(_current_index < _datagram->get_length(), 0.0);
00450 
00451   PN_float32 tempvar;
00452   // Avoid reading junk data off the end of the datagram:
00453   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0);
00454   // Get the Data:
00455   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00456   s.store_value(&tempvar, sizeof(tempvar));
00457   _current_index += sizeof(tempvar);
00458 
00459   return tempvar;
00460 }
00461 
00462 ////////////////////////////////////////////////////////////////////
00463 //     Function: DatagramIterator::get_be_float64
00464 //       Access: Public
00465 //  Description: Extracts a 64-bit big-endian floating-point number.
00466 ////////////////////////////////////////////////////////////////////
00467 INLINE PN_float64 DatagramIterator::
00468 get_be_float64() {
00469   nassertr(_datagram != (const Datagram *)NULL, 0.0);
00470   nassertr(_current_index < _datagram->get_length(), 0.0);
00471 
00472   PN_float64 tempvar;
00473   // Avoid reading junk data off the end of the datagram:
00474   nassertr(_current_index + sizeof(tempvar) <= _datagram->get_length(), 0.0);
00475   // Get the Data:
00476   BigEndian s(_datagram->get_data(), _current_index, sizeof(tempvar));
00477   s.store_value(&tempvar, sizeof(tempvar));
00478   _current_index += sizeof(tempvar);
00479 
00480   return tempvar;
00481 }
00482 
00483 ////////////////////////////////////////////////////////////////////
00484 //     Function: DatagramIterator::skip_bytes
00485 //       Access: Public
00486 //  Description: Skips over the indicated number of bytes in the
00487 //               datagram.
00488 ////////////////////////////////////////////////////////////////////
00489 INLINE void DatagramIterator::
00490 skip_bytes(size_t size) {
00491   nassertv(_datagram != (const Datagram *)NULL);
00492   nassertv((int)size >= 0);
00493 #ifndef NDEBUG
00494   if (_current_index + size > _datagram->get_length()) {
00495      nout << "datagram overflow: current_index = " << _current_index
00496           << " size = " << size << " length = " << _datagram->get_length() << "\n";
00497     _datagram->dump_hex(nout);
00498   }
00499 #endif
00500   nassertv(_current_index + size <= _datagram->get_length());
00501   _current_index += size;
00502 }
00503 
00504 ////////////////////////////////////////////////////////////////////
00505 //     Function: DatagramIterator::get_remaining_bytes
00506 //       Access: Public
00507 //  Description: Returns the remaining bytes in the datagram as a
00508 //               string, but does not extract them from the iterator.
00509 ////////////////////////////////////////////////////////////////////
00510 INLINE string DatagramIterator::
00511 get_remaining_bytes() const {
00512   nassertr(_datagram != (const Datagram *)NULL, "");
00513   nassertr(_current_index <= _datagram->get_length(), "");
00514 
00515   const char *ptr = (const char *)_datagram->get_data();
00516   int remaining_size = _datagram->get_length() - _current_index;
00517   return string(ptr + _current_index, remaining_size);
00518 }
00519 
00520 ////////////////////////////////////////////////////////////////////
00521 //     Function: DatagramIterator::get_remaining_size
00522 //       Access: Public
00523 //  Description: Return the bytes left in the datagram.
00524 ////////////////////////////////////////////////////////////////////
00525 INLINE int DatagramIterator::
00526 get_remaining_size() const {
00527   return _datagram->get_length() - _current_index;
00528 }
00529 
00530 ////////////////////////////////////////////////////////////////////
00531 //     Function: DatagramIterator::get_datagram
00532 //       Access: Public
00533 //  Description: Return the datagram of this iterator.
00534 ////////////////////////////////////////////////////////////////////
00535 INLINE const Datagram &DatagramIterator::
00536 get_datagram() const {
00537   return *_datagram;
00538 }
00539 
00540 ////////////////////////////////////////////////////////////////////
00541 //     Function: DatagramIterator::get_current_index
00542 //       Access: Public
00543 //  Description: Returns the current position within the datagram of the
00544 //               next piece of data to extract.
00545 ////////////////////////////////////////////////////////////////////
00546 INLINE size_t DatagramIterator::
00547 get_current_index() const {
00548   return _current_index;
00549 }
00550 
00551 INLINE void
00552 generic_read_datagram(bool &result, DatagramIterator &source) {
00553   result = source.get_bool();
00554 }
00555 
00556 INLINE void
00557 generic_read_datagram(int &result, DatagramIterator &source) {
00558   result = source.get_int32();
00559 }
00560 
00561 INLINE void
00562 generic_read_datagram(float &result, DatagramIterator &source) {
00563   result = source.get_float32();
00564 }
00565 
00566 INLINE void
00567 generic_read_datagram(double &result, DatagramIterator &source) {
00568   result = source.get_float64();
00569 }
00570 
00571 INLINE void
00572 generic_read_datagram(string &result, DatagramIterator &source) {
00573   result = source.get_string();
00574 }
00575 
00576 INLINE void
00577 generic_read_datagram(wstring &result, DatagramIterator &source) {
00578   result = source.get_wstring();
00579 }
00580 
00581 
 All Classes Functions Variables Enumerations