Home | History | Annotate | Download | only in Common
      1 // Common/DynamicBuffer.h
      2 
      3 #ifndef __COMMON_DYNAMIC_BUFFER_H
      4 #define __COMMON_DYNAMIC_BUFFER_H
      5 
      6 #include "Buffer.h"
      7 
      8 template <class T> class CDynamicBuffer: public CBuffer<T>
      9 {
     10   void GrowLength(size_t size)
     11   {
     12     size_t delta;
     13     if (this->_capacity > 64)
     14       delta = this->_capacity / 4;
     15     else if (this->_capacity > 8)
     16       delta = 16;
     17     else
     18       delta = 4;
     19     delta = MyMax(delta, size);
     20     size_t newCap = this->_capacity + delta;
     21     if (newCap < delta)
     22       newCap = this->_capacity + size;
     23     SetCapacity(newCap);
     24   }
     25 public:
     26   CDynamicBuffer(): CBuffer<T>() {};
     27   CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer<T>(buffer) {};
     28   CDynamicBuffer(size_t size): CBuffer<T>(size) {};
     29   CDynamicBuffer& operator=(const CDynamicBuffer &buffer)
     30   {
     31     this->Free();
     32     if (buffer._capacity > 0)
     33     {
     34       SetCapacity(buffer._capacity);
     35       memmove(this->_items, buffer._items, buffer._capacity * sizeof(T));
     36     }
     37     return *this;
     38   }
     39   void EnsureCapacity(size_t capacity)
     40   {
     41     if (this->_capacity < capacity)
     42       GrowLength(capacity - this->_capacity);
     43   }
     44 };
     45 
     46 typedef CDynamicBuffer<char> CCharDynamicBuffer;
     47 typedef CDynamicBuffer<wchar_t> CWCharDynamicBuffer;
     48 typedef CDynamicBuffer<unsigned char> CByteDynamicBuffer;
     49 
     50 #endif
     51