annotate src/win32/7zip/7z/CPP/Common/DynamicBuffer.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // Common/DynamicBuffer.h
rlm@1 2
rlm@1 3 #ifndef __COMMON_DYNAMICBUFFER_H
rlm@1 4 #define __COMMON_DYNAMICBUFFER_H
rlm@1 5
rlm@1 6 #include "Buffer.h"
rlm@1 7
rlm@1 8 template <class T> class CDynamicBuffer: public CBuffer<T>
rlm@1 9 {
rlm@1 10 void GrowLength(size_t size)
rlm@1 11 {
rlm@1 12 size_t delta;
rlm@1 13 if (this->_capacity > 64)
rlm@1 14 delta = this->_capacity / 4;
rlm@1 15 else if (this->_capacity > 8)
rlm@1 16 delta = 16;
rlm@1 17 else
rlm@1 18 delta = 4;
rlm@1 19 delta = MyMax(delta, size);
rlm@1 20 SetCapacity(this->_capacity + delta);
rlm@1 21 }
rlm@1 22 public:
rlm@1 23 CDynamicBuffer(): CBuffer<T>() {};
rlm@1 24 CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer<T>(buffer) {};
rlm@1 25 CDynamicBuffer(size_t size): CBuffer<T>(size) {};
rlm@1 26 CDynamicBuffer& operator=(const CDynamicBuffer &buffer)
rlm@1 27 {
rlm@1 28 this->Free();
rlm@1 29 if (buffer._capacity > 0)
rlm@1 30 {
rlm@1 31 SetCapacity(buffer._capacity);
rlm@1 32 memmove(this->_items, buffer._items, buffer._capacity * sizeof(T));
rlm@1 33 }
rlm@1 34 return *this;
rlm@1 35 }
rlm@1 36 void EnsureCapacity(size_t capacity)
rlm@1 37 {
rlm@1 38 if (this->_capacity < capacity)
rlm@1 39 GrowLength(capacity - this->_capacity);
rlm@1 40 }
rlm@1 41 };
rlm@1 42
rlm@1 43 typedef CDynamicBuffer<char> CCharDynamicBuffer;
rlm@1 44 typedef CDynamicBuffer<wchar_t> CWCharDynamicBuffer;
rlm@1 45 typedef CDynamicBuffer<unsigned char> CByteDynamicBuffer;
rlm@1 46
rlm@1 47 #endif