DynamicBuffer.cpp
Go to the documentation of this file.00001 //*** DynamicBuffer.cpp *** 00002 00003 #include "DynamicBuffer.h" 00004 #include "Debug.h" 00005 #include "StandardLibrary.h" 00006 00007 00008 //*** Constructor *** 00009 00010 DynamicBuffer::DynamicBuffer(unsigned int initialCapacity): 00011 buffer_(0), 00012 initialCapacity_(initialCapacity), 00013 capacity_(initialCapacity), 00014 size_(0), 00015 position_(0) 00016 { 00017 00018 } 00019 00020 00021 //*** Destructor *** 00022 00023 DynamicBuffer::~DynamicBuffer() 00024 { 00025 // Free memory used by the buffer 00026 if (buffer_) 00027 { 00028 Free(buffer_); 00029 buffer_=0; 00030 } 00031 } 00032 00033 00034 //*** Copy Constructor *** 00035 00036 DynamicBuffer::DynamicBuffer(const DynamicBuffer& dynamicBuffer): 00037 buffer_(0), 00038 initialCapacity_(dynamicBuffer.initialCapacity_), 00039 capacity_(dynamicBuffer.capacity_), 00040 size_(dynamicBuffer.size_), 00041 position_(dynamicBuffer.position_) 00042 { 00043 if (dynamicBuffer.buffer_) 00044 { 00045 // Allocate memory 00046 buffer_=static_cast<unsigned char*>(Malloc(capacity_)); 00047 Assert(buffer_,"Couldn't allocate memory for dynamic buffer"); 00048 if (!buffer_) 00049 { 00050 FatalError("Allocation failed when allocating memory for dynamic buffer"); 00051 } 00052 00053 // Copy contents 00054 MemCpy(buffer_,dynamicBuffer.buffer_,size_); 00055 } 00056 } 00057 00058 00059 //*** Assignment operator *** 00060 00061 const DynamicBuffer& DynamicBuffer::operator =(const DynamicBuffer& dynamicBuffer) 00062 { 00063 // Free memory used by the buffer 00064 if (buffer_) 00065 { 00066 Free(buffer_); 00067 buffer_=0; 00068 } 00069 00070 buffer_=0; 00071 initialCapacity_=dynamicBuffer.initialCapacity_; 00072 capacity_=dynamicBuffer.capacity_; 00073 size_=dynamicBuffer.size_; 00074 position_=dynamicBuffer.position_; 00075 00076 if (dynamicBuffer.buffer_) 00077 { 00078 // Allocate memory 00079 buffer_=static_cast<unsigned char*>(Malloc(capacity_)); 00080 Assert(buffer_,"Couldn't allocate memory for dynamic buffer"); 00081 if (!buffer_) 00082 { 00083 FatalError("Allocation failed when allocating memory for dynamic buffer"); 00084 } 00085 00086 // Copy contents 00087 MemCpy(buffer_,dynamicBuffer.buffer_,size_); 00088 } 00089 00090 return *this; 00091 } 00092 00093 00094 //*** Clear *** 00095 00096 void DynamicBuffer::Clear(bool releaseMemory) 00097 { 00098 // Reset the position and size variables 00099 position_=0; 00100 size_=0; 00101 00102 // Free allocated memory and reset to initial size if requested 00103 if (releaseMemory) 00104 { 00105 // Free memory used by the buffer 00106 if (buffer_) 00107 { 00108 Free(buffer_); 00109 buffer_=0; 00110 } 00111 00112 // Reset to initial size 00113 capacity_=initialCapacity_; 00114 } 00115 } 00116 00117 00118 //*** GetSize *** 00119 00120 unsigned int DynamicBuffer::GetSize() const 00121 { 00122 return size_; 00123 } 00124 00125 00126 //*** SetSize *** 00127 00128 void DynamicBuffer::SetSize(unsigned int size) 00129 { 00130 size_=size; 00131 if (position_>size_) 00132 { 00133 position_=size_; 00134 } 00135 } 00136 00137 00138 //*** GetCapacity *** 00139 00140 unsigned int DynamicBuffer::GetCapacity() const 00141 { 00142 return capacity_; 00143 } 00144 00145 00146 //*** SetCapacity *** 00147 00148 void DynamicBuffer::SetCapacity(unsigned int capacity) 00149 { 00150 if (capacity_<capacity) 00151 { 00152 capacity_=capacity; 00153 00154 if (buffer_) 00155 { 00156 // Reallocate the actual buffer 00157 buffer_=static_cast<unsigned char*>(Realloc(buffer_,capacity_)); 00158 Assert(buffer_,"Couldn't reallocate memory for dynamic buffer"); 00159 if (!buffer_) 00160 { 00161 FatalError("Reallocation failed when allocating memory for dynamic buffer"); 00162 } 00163 } 00164 } 00165 } 00166 00167 00168 //*** GetPosition *** 00169 00170 unsigned int DynamicBuffer::GetPosition() const 00171 { 00172 return position_; 00173 } 00174 00175 00176 //*** SetPosition *** 00177 00178 void DynamicBuffer::SetPosition(unsigned int position) 00179 { 00180 position_=position; 00181 } 00182 00183 00184 //** Write methods */ 00185 00186 unsigned int DynamicBuffer::Write(const char* value, unsigned int count) 00187 { 00188 return Write<char>(value,count); 00189 } 00190 00191 unsigned int DynamicBuffer::Write(const short* value, unsigned int count) 00192 { 00193 return Write<short>(value,count); 00194 } 00195 00196 unsigned int DynamicBuffer::Write(const int* value, unsigned int count) 00197 { 00198 return Write<int>(value,count); 00199 } 00200 00201 unsigned int DynamicBuffer::Write(const long* value, unsigned int count) 00202 { 00203 return Write<long>(value,count); 00204 } 00205 00206 unsigned int DynamicBuffer::Write(const unsigned char* value, unsigned int count) 00207 { 00208 return Write<unsigned char>(value,count); 00209 } 00210 00211 unsigned int DynamicBuffer::Write(const unsigned short* value, unsigned int count) 00212 { 00213 return Write<unsigned short>(value,count); 00214 } 00215 00216 unsigned int DynamicBuffer::Write(const unsigned int* value, unsigned int count) 00217 { 00218 return Write<unsigned int>(value,count); 00219 } 00220 00221 unsigned int DynamicBuffer::Write(const unsigned long* value, unsigned int count) 00222 { 00223 return Write<unsigned long>(value,count); 00224 } 00225 00226 unsigned int DynamicBuffer::Write(const float* value, unsigned int count) 00227 { 00228 return Write<float>(value,count); 00229 } 00230 00231 unsigned int DynamicBuffer::Write(const double* value, unsigned int count) 00232 { 00233 return Write<double>(value,count); 00234 } 00235 00236 unsigned int DynamicBuffer::Write(const bool* value, unsigned int count) 00237 { 00238 return Write<bool>(value,count); 00239 } 00240 00241 00242 00243 00244 //** Read methods */ 00245 00246 unsigned int DynamicBuffer::Read(char* value, unsigned int count) 00247 { 00248 return Read<char>(value,count); 00249 } 00250 00251 unsigned int DynamicBuffer::Read(short* value, unsigned int count) 00252 { 00253 return Read<short>(value,count); 00254 } 00255 00256 unsigned int DynamicBuffer::Read(int* value, unsigned int count) 00257 { 00258 return Read<int>(value,count); 00259 } 00260 00261 unsigned int DynamicBuffer::Read(long* value, unsigned int count) 00262 { 00263 return Read<long>(value,count); 00264 } 00265 00266 unsigned int DynamicBuffer::Read(unsigned char* value, unsigned int count) 00267 { 00268 return Read<unsigned char>(value,count); 00269 } 00270 00271 unsigned int DynamicBuffer::Read(unsigned short* value, unsigned int count) 00272 { 00273 return Read<unsigned short>(value,count); 00274 } 00275 00276 unsigned int DynamicBuffer::Read(unsigned int* value, unsigned int count) 00277 { 00278 return Read<unsigned int>(value,count); 00279 } 00280 00281 unsigned int DynamicBuffer::Read(unsigned long* value, unsigned int count) 00282 { 00283 return Read<unsigned long>(value,count); 00284 } 00285 00286 unsigned int DynamicBuffer::Read(float* value, unsigned int count) 00287 { 00288 return Read<float>(value,count); 00289 } 00290 00291 unsigned int DynamicBuffer::Read(double* value, unsigned int count) 00292 { 00293 return Read<double>(value,count); 00294 } 00295 00296 unsigned int DynamicBuffer::Read(bool* value, unsigned int count) 00297 { 00298 return Read<bool>(value,count); 00299 } 00300 00301 00302 //*** GetPointer *** 00303 00304 void* DynamicBuffer::GetPointer() const 00305 { 00306 return buffer_; 00307 } 00308 00309 00310 //*** Write *** 00311 00312 template <typename TYPE> 00313 unsigned int DynamicBuffer::Write(const TYPE* value, unsigned int count) 00314 { 00315 // Calculate the total number of bytes to be written 00316 unsigned int totalSize=sizeof(TYPE)*count; 00317 00318 // Make sure there's room to write all those bytes 00319 Resize(totalSize); 00320 00321 // Copy the actual bytes into the buffer 00322 MemCpy(buffer_+position_,value,totalSize); 00323 00324 // Update the current position 00325 position_+=totalSize; 00326 00327 // Update the current size, if we wrote enough bytes 00328 if (size_<position_) 00329 { 00330 size_=position_; 00331 } 00332 00333 // Return the number of values written 00334 return count; 00335 } 00336 00337 00338 //*** Read *** 00339 00340 template <typename TYPE> 00341 unsigned int DynamicBuffer::Read(TYPE* value, unsigned int count) 00342 { 00343 // Calculate the total number of bytes to be read 00344 unsigned int totalSize=sizeof(*value)*count; 00345 00346 // Make sure we're not trying to read more bytes than there is 00347 if (position_+totalSize>size_) 00348 { 00349 count=(size_-position_)/sizeof(*value); 00350 totalSize=sizeof(*value)*count; 00351 } 00352 00353 // Copy the actual bytes from the buffer 00354 if (buffer_ && totalSize>0) 00355 { 00356 MemCpy(value,buffer_+position_,totalSize); 00357 } 00358 00359 // Update the current position 00360 position_+=totalSize; 00361 00362 // Return the number of elements written 00363 return count; 00364 } 00365 00366 00367 //*** Resize *** 00368 00369 void DynamicBuffer::Resize(unsigned int size) 00370 { 00371 unsigned int newCapacity=capacity_; 00372 while (newCapacity<(position_+size)) 00373 { 00374 newCapacity*=2; 00375 } 00376 00377 // First-time buffer allocation 00378 if (!buffer_) 00379 { 00380 capacity_=newCapacity; 00381 00382 // Allocate memory 00383 buffer_=static_cast<unsigned char*>(Malloc(capacity_)); 00384 Assert(buffer_,"Couldn't allocate memory for dynamic buffer"); 00385 if (!buffer_) 00386 { 00387 FatalError("Allocation failed when allocating memory for dynamic buffer"); 00388 } 00389 return; 00390 } 00391 00392 00393 // Check if the buffer needs resizing 00394 if (newCapacity>capacity_) 00395 { 00396 capacity_=newCapacity; 00397 00398 // Reallocate the actual buffer 00399 buffer_=static_cast<unsigned char*>(Realloc(buffer_,capacity_)); 00400 Assert(buffer_,"Couldn't reallocate memory for dynamic buffer"); 00401 if (!buffer_) 00402 { 00403 FatalError("Allocation failed when reallocating memory for dynamic buffer"); 00404 } 00405 } 00406 } 00407 00408 00409
Reproduction/republishing of any material on this site without permission is strictly prohibited.
