Panda3D
 All Classes Functions Variables Enumerations
streamReader.I
00001 // Filename: streamReader.I
00002 // Created by:  drose (04Aug02)
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: StreamReader::Constructor
00018 //       Access: Public
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE StreamReader::
00022 StreamReader(istream &in) :
00023   _in(&in),
00024   _owns_stream(false)
00025 {
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: StreamReader::Constructor
00030 //       Access: Published
00031 //  Description: If owns_stream is true, the stream pointer will be
00032 //               deleted when the StreamReader destructs.
00033 ////////////////////////////////////////////////////////////////////
00034 INLINE StreamReader::
00035 StreamReader(istream *in, bool owns_stream) :
00036   _in(in),
00037   _owns_stream(owns_stream)
00038 {
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function: StreamReader::Copy Constructor
00043 //       Access: Published
00044 //  Description: The copy constructor does not copy ownership of the
00045 //               stream.
00046 ////////////////////////////////////////////////////////////////////
00047 INLINE StreamReader::
00048 StreamReader(const StreamReader &copy) :
00049   _in(copy._in),
00050   _owns_stream(false)
00051 {
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: StreamReader::Copy Assignment Operator
00056 //       Access: Published
00057 //  Description: The copy constructor does not copy ownership of the
00058 //               stream.
00059 ////////////////////////////////////////////////////////////////////
00060 INLINE void StreamReader::
00061 operator = (const StreamReader &copy) {
00062   if (_owns_stream) {
00063     delete _in;
00064   }
00065   _in = copy._in;
00066   _owns_stream = false;
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: StreamReader::Destructor
00071 //       Access: Published
00072 //  Description:
00073 ////////////////////////////////////////////////////////////////////
00074 INLINE StreamReader::
00075 ~StreamReader() {
00076   if (_owns_stream) {
00077     delete _in;
00078   }
00079 }
00080 
00081 ////////////////////////////////////////////////////////////////////
00082 //     Function: StreamReader::get_istream
00083 //       Access: Published
00084 //  Description: Returns the stream in use.
00085 ////////////////////////////////////////////////////////////////////
00086 INLINE istream *StreamReader::
00087 get_istream() const {
00088   return _in;
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: StreamReader::get_bool
00093 //       Access: Published
00094 //  Description: Extracts a boolean value.
00095 ////////////////////////////////////////////////////////////////////
00096 INLINE bool StreamReader::
00097 get_bool() {
00098   return (get_uint8() != 0);
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: StreamReader::get_int8
00103 //       Access: Published
00104 //  Description: Extracts a signed 8-bit integer.
00105 ////////////////////////////////////////////////////////////////////
00106 INLINE PN_int8 StreamReader::
00107 get_int8() {
00108   return (PN_int8)_in->get();
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: StreamReader::get_uint8
00113 //       Access: Published
00114 //  Description: Extracts an unsigned 8-bit integer.
00115 ////////////////////////////////////////////////////////////////////
00116 INLINE PN_uint8 StreamReader::
00117 get_uint8() {
00118   return (PN_uint8)_in->get();
00119 }
00120 
00121 ////////////////////////////////////////////////////////////////////
00122 //     Function: StreamReader::get_int16
00123 //       Access: Published
00124 //  Description: Extracts a signed 16-bit integer.
00125 ////////////////////////////////////////////////////////////////////
00126 INLINE PN_int16 StreamReader::
00127 get_int16() {
00128   PN_int16 readval, retval;
00129   _in->read((char *)&readval, sizeof(readval));
00130   LittleEndian s(&readval, 0, sizeof(readval));
00131   s.store_value(&retval, sizeof(retval));
00132   return retval;
00133 }
00134 
00135 ////////////////////////////////////////////////////////////////////
00136 //     Function: StreamReader::get_int32
00137 //       Access: Published
00138 //  Description: Extracts a signed 32-bit integer.
00139 ////////////////////////////////////////////////////////////////////
00140 INLINE PN_int32 StreamReader::
00141 get_int32() {
00142   PN_int32 readval, retval;
00143   _in->read((char *)&readval, sizeof(readval));
00144   LittleEndian s(&readval, 0, sizeof(readval));
00145   s.store_value(&retval, sizeof(retval));
00146   return retval;
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: StreamReader::get_int64
00151 //       Access: Published
00152 //  Description: Extracts a signed 64-bit integer.
00153 ////////////////////////////////////////////////////////////////////
00154 INLINE PN_int64 StreamReader::
00155 get_int64() {
00156   PN_int64 readval, retval;
00157   _in->read((char *)&readval, sizeof(readval));
00158   LittleEndian s(&readval, 0, sizeof(readval));
00159   s.store_value(&retval, sizeof(retval));
00160   return retval;
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: StreamReader::get_uint16
00165 //       Access: Published
00166 //  Description: Extracts an unsigned 16-bit integer.
00167 ////////////////////////////////////////////////////////////////////
00168 INLINE PN_uint16 StreamReader::
00169 get_uint16() {
00170   PN_uint16 readval, retval;
00171   _in->read((char *)&readval, sizeof(readval));
00172   LittleEndian s(&readval, 0, sizeof(readval));
00173   s.store_value(&retval, sizeof(retval));
00174   return retval;
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////
00178 //     Function: StreamReader::get_uint32
00179 //       Access: Published
00180 //  Description: Extracts an unsigned 32-bit integer.
00181 ////////////////////////////////////////////////////////////////////
00182 INLINE PN_uint32 StreamReader::
00183 get_uint32() {
00184   PN_uint32 readval, retval;
00185   _in->read((char *)&readval, sizeof(readval));
00186   LittleEndian s(&readval, 0, sizeof(readval));
00187   s.store_value(&retval, sizeof(retval));
00188   return retval;
00189 }
00190 
00191 ////////////////////////////////////////////////////////////////////
00192 //     Function: StreamReader::get_uint64
00193 //       Access: Published
00194 //  Description: Extracts an unsigned 64-bit integer.
00195 ////////////////////////////////////////////////////////////////////
00196 INLINE PN_uint64 StreamReader::
00197 get_uint64() {
00198   PN_uint64 readval, retval;
00199   _in->read((char *)&readval, sizeof(readval));
00200   LittleEndian s(&readval, 0, sizeof(readval));
00201   s.store_value(&retval, sizeof(retval));
00202   return retval;
00203 }
00204 
00205 ////////////////////////////////////////////////////////////////////
00206 //     Function: StreamReader::get_float32
00207 //       Access: Published
00208 //  Description: Extracts a 32-bit single-precision floating-point
00209 //               number.  Since this kind of float is not necessarily
00210 //               portable across different architectures, special care
00211 //               is required.
00212 ////////////////////////////////////////////////////////////////////
00213 INLINE float StreamReader::
00214 get_float32() {
00215   // For now, we assume the float format is portable across all
00216   // architectures we are concerned with.  If we come across one that
00217   // is different, we will have to convert.
00218   nassertr(sizeof(float) == 4, 0.0f);
00219 
00220   float readval, retval;
00221   _in->read((char *)&readval, sizeof(readval));
00222   LittleEndian s(&readval, 0, sizeof(readval));
00223   s.store_value(&retval, sizeof(retval));
00224   return retval;
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: StreamReader::get_float64
00229 //       Access: Published
00230 //  Description: Extracts a 64-bit floating-point number.
00231 ////////////////////////////////////////////////////////////////////
00232 INLINE PN_float64 StreamReader::
00233 get_float64() {
00234   PN_float64 readval, retval;
00235   _in->read((char *)&readval, sizeof(readval));
00236   LittleEndian s(&readval, 0, sizeof(readval));
00237   s.store_value(&retval, sizeof(retval));
00238   return retval;
00239 }
00240 
00241 ////////////////////////////////////////////////////////////////////
00242 //     Function: StreamReader::get_be_int16
00243 //       Access: Published
00244 //  Description: Extracts a signed big-endian 16-bit integer.
00245 ////////////////////////////////////////////////////////////////////
00246 INLINE PN_int16 StreamReader::
00247 get_be_int16() {
00248   PN_int16 readval, retval;
00249   _in->read((char *)&readval, sizeof(readval));
00250   BigEndian s(&readval, 0, sizeof(readval));
00251   s.store_value(&retval, sizeof(retval));
00252   return retval;
00253 }
00254 
00255 ////////////////////////////////////////////////////////////////////
00256 //     Function: StreamReader::get_be_int32
00257 //       Access: Published
00258 //  Description: Extracts a signed big-endian 32-bit integer.
00259 ////////////////////////////////////////////////////////////////////
00260 INLINE PN_int32 StreamReader::
00261 get_be_int32() {
00262   PN_int32 readval, retval;
00263   _in->read((char *)&readval, sizeof(readval));
00264   BigEndian s(&readval, 0, sizeof(readval));
00265   s.store_value(&retval, sizeof(retval));
00266   return retval;
00267 }
00268 
00269 ////////////////////////////////////////////////////////////////////
00270 //     Function: StreamReader::get_be_int64
00271 //       Access: Published
00272 //  Description: Extracts a signed big-endian 64-bit integer.
00273 ////////////////////////////////////////////////////////////////////
00274 INLINE PN_int64 StreamReader::
00275 get_be_int64() {
00276   PN_int64 readval, retval;
00277   _in->read((char *)&readval, sizeof(readval));
00278   BigEndian s(&readval, 0, sizeof(readval));
00279   s.store_value(&retval, sizeof(retval));
00280   return retval;
00281 }
00282 
00283 ////////////////////////////////////////////////////////////////////
00284 //     Function: StreamReader::get_be_uint16
00285 //       Access: Published
00286 //  Description: Extracts an unsigned big-endian 16-bit integer.
00287 ////////////////////////////////////////////////////////////////////
00288 INLINE PN_uint16 StreamReader::
00289 get_be_uint16() {
00290   PN_uint16 readval, retval;
00291   _in->read((char *)&readval, sizeof(readval));
00292   BigEndian s(&readval, 0, sizeof(readval));
00293   s.store_value(&retval, sizeof(retval));
00294   return retval;
00295 }
00296 
00297 ////////////////////////////////////////////////////////////////////
00298 //     Function: StreamReader::get_be_uint32
00299 //       Access: Published
00300 //  Description: Extracts an unsigned big-endian 32-bit integer.
00301 ////////////////////////////////////////////////////////////////////
00302 INLINE PN_uint32 StreamReader::
00303 get_be_uint32() {
00304   PN_uint32 readval, retval;
00305   _in->read((char *)&readval, sizeof(readval));
00306   BigEndian s(&readval, 0, sizeof(readval));
00307   s.store_value(&retval, sizeof(retval));
00308   return retval;
00309 }
00310 
00311 ////////////////////////////////////////////////////////////////////
00312 //     Function: StreamReader::get_be_uint64
00313 //       Access: Published
00314 //  Description: Extracts an unsigned big-endian 64-bit integer.
00315 ////////////////////////////////////////////////////////////////////
00316 INLINE PN_uint64 StreamReader::
00317 get_be_uint64() {
00318   PN_uint64 readval, retval;
00319   _in->read((char *)&readval, sizeof(readval));
00320   BigEndian s(&readval, 0, sizeof(readval));
00321   s.store_value(&retval, sizeof(retval));
00322   return retval;
00323 }
00324 
00325 ////////////////////////////////////////////////////////////////////
00326 //     Function: StreamReader::get_be_float32
00327 //       Access: Published
00328 //  Description: Extracts a 32-bit single-precision big-endian
00329 //               floating-point number.  Since this kind of float is
00330 //               not necessarily portable across different
00331 //               architectures, special care is required.
00332 ////////////////////////////////////////////////////////////////////
00333 INLINE float StreamReader::
00334 get_be_float32() {
00335   // For now, we assume the float format is portable across all
00336   // architectures we are concerned with.  If we come across one that
00337   // is different, we will have to convert.
00338   nassertr(sizeof(float) == 4, 0.0f);
00339 
00340   float readval, retval;
00341   _in->read((char *)&readval, sizeof(readval));
00342   BigEndian s(&readval, 0, sizeof(readval));
00343   s.store_value(&retval, sizeof(retval));
00344   return retval;
00345 }
00346 
00347 ////////////////////////////////////////////////////////////////////
00348 //     Function: StreamReader::get_be_float64
00349 //       Access: Published
00350 //  Description: Extracts a 64-bit big-endian floating-point number.
00351 ////////////////////////////////////////////////////////////////////
00352 INLINE PN_float64 StreamReader::
00353 get_be_float64() {
00354   PN_float64 readval, retval;
00355   _in->read((char *)&readval, sizeof(readval));
00356   BigEndian s(&readval, 0, sizeof(readval));
00357   s.store_value(&retval, sizeof(retval));
00358   return retval;
00359 }
 All Classes Functions Variables Enumerations