Panda3D
|
00001 // Filename: dcPackData.I 00002 // Created by: drose (15Jun04) 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: DCPackData::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE DCPackData:: 00022 DCPackData() { 00023 _buffer = NULL; 00024 _allocated_size = 0; 00025 _used_length = 0; 00026 } 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: DCPackData::Destructor 00030 // Access: Published 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 INLINE DCPackData:: 00034 ~DCPackData() { 00035 if (_buffer != (const char *)NULL) { 00036 delete[] _buffer; 00037 } 00038 } 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function: DCPackData::clear 00042 // Access: Published 00043 // Description: Empties the contents of the data (without necessarily 00044 // freeing its allocated memory). 00045 //////////////////////////////////////////////////////////////////// 00046 INLINE void DCPackData:: 00047 clear() { 00048 _used_length = 0; 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: DCPackData::append_data 00053 // Access: Public 00054 // Description: Adds the indicated bytes to the end of the data. 00055 //////////////////////////////////////////////////////////////////// 00056 INLINE void DCPackData:: 00057 append_data(const char *buffer, size_t size) { 00058 set_used_length(_used_length + size); 00059 memcpy(_buffer + _used_length - size, buffer, size); 00060 } 00061 00062 //////////////////////////////////////////////////////////////////// 00063 // Function: DCPackData::get_write_pointer 00064 // Access: Public 00065 // Description: Adds the indicated number of bytes to the end of the 00066 // data without initializing them, and returns a pointer 00067 // to the beginning of the new data. 00068 //////////////////////////////////////////////////////////////////// 00069 INLINE char *DCPackData:: 00070 get_write_pointer(size_t size) { 00071 set_used_length(_used_length + size); 00072 return _buffer + _used_length - size; 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: DCPackData::append_junk 00077 // Access: Public 00078 // Description: Adds some uninitialized bytes to the end of the data. 00079 //////////////////////////////////////////////////////////////////// 00080 INLINE void DCPackData:: 00081 append_junk(size_t size) { 00082 set_used_length(_used_length + size); 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: DCPackData::rewrite_data 00087 // Access: Public 00088 // Description: Changes the data at the indicated position to the 00089 // given value. It is an error if there are not at 00090 // least position + size bytes in the data. 00091 //////////////////////////////////////////////////////////////////// 00092 INLINE void DCPackData:: 00093 rewrite_data(size_t position, const char *buffer, size_t size) { 00094 nassertv(position + size <= _used_length); 00095 memcpy(_buffer + position, buffer, size); 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: DCPackData::get_rewrite_pointer 00100 // Access: Public 00101 // Description: Returns a pointer into the middle of the data at the 00102 // indicated point. 00103 //////////////////////////////////////////////////////////////////// 00104 INLINE char *DCPackData:: 00105 get_rewrite_pointer(size_t position, size_t size) { 00106 nassertr(position + size <= _used_length, NULL); 00107 return _buffer + position; 00108 } 00109 00110 //////////////////////////////////////////////////////////////////// 00111 // Function: DCPackData::get_string 00112 // Access: Published 00113 // Description: Returns the data buffer as a string. Also see 00114 // get_data(). 00115 //////////////////////////////////////////////////////////////////// 00116 INLINE string DCPackData:: 00117 get_string() const { 00118 return string(_buffer, _used_length); 00119 } 00120 00121 //////////////////////////////////////////////////////////////////// 00122 // Function: DCPackData::get_length 00123 // Access: Published 00124 // Description: Returns the current length of the buffer. This is 00125 // the number of useful bytes stored in the buffer, not 00126 // the amount of memory it takes up. 00127 //////////////////////////////////////////////////////////////////// 00128 INLINE size_t DCPackData:: 00129 get_length() const { 00130 return _used_length; 00131 } 00132 00133 //////////////////////////////////////////////////////////////////// 00134 // Function: DCPackData::get_data 00135 // Access: Public 00136 // Description: Returns the beginning of the data buffer. The buffer 00137 // is not null-terminated, but see also get_string(). 00138 // This may (or may not) return NULL if the buffer is 00139 // empty. 00140 // 00141 // This may be used in conjunction with get_length() to 00142 // copy all of the bytes out of the buffer. 00143 //////////////////////////////////////////////////////////////////// 00144 INLINE const char *DCPackData:: 00145 get_data() const { 00146 return _buffer; 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: DCPackData::take_data 00151 // Access: Public 00152 // Description: Returns the pointer to the beginning of the data 00153 // buffer, and transfers ownership of the buffer to the 00154 // caller. The caller is now responsible for ultimately 00155 // freeing the returned pointer with delete[], if it is 00156 // non-NULL. This may (or may not) return NULL if the 00157 // buffer is empty. 00158 // 00159 // This also empties the DCPackData structure, and sets 00160 // its length to zero (so you should call get_length() 00161 // before calling this method). 00162 //////////////////////////////////////////////////////////////////// 00163 INLINE char *DCPackData:: 00164 take_data() { 00165 char *data = _buffer; 00166 00167 _buffer = NULL; 00168 _allocated_size = 0; 00169 _used_length = 0; 00170 00171 return data; 00172 }