misc/libphysfs/lzma/CPP/Common/DynamicBuffer.h
changeset 12218 bb5522e88ab2
equal deleted inserted replaced
12217:ea891871f481 12218:bb5522e88ab2
       
     1 // Common/DynamicBuffer.h
       
     2 
       
     3 #ifndef __COMMON_DYNAMICBUFFER_H
       
     4 #define __COMMON_DYNAMICBUFFER_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     SetCapacity(this->_capacity + delta);
       
    21   }
       
    22 public:
       
    23   CDynamicBuffer(): CBuffer<T>() {};
       
    24   CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer<T>(buffer) {};
       
    25   CDynamicBuffer(size_t size): CBuffer<T>(size) {};
       
    26   CDynamicBuffer& operator=(const CDynamicBuffer &buffer)
       
    27   {
       
    28     this->Free();
       
    29     if(buffer._capacity > 0)
       
    30     {
       
    31       SetCapacity(buffer._capacity);
       
    32       memmove(this->_items, buffer._items, buffer._capacity * sizeof(T));
       
    33     }
       
    34     return *this;
       
    35   }
       
    36   void EnsureCapacity(size_t capacity)
       
    37   {
       
    38     if (this->_capacity < capacity)
       
    39       GrowLength(capacity - this->_capacity);
       
    40   }
       
    41 };
       
    42 
       
    43 typedef CDynamicBuffer<char> CCharDynamicBuffer;
       
    44 typedef CDynamicBuffer<wchar_t> CWCharDynamicBuffer;
       
    45 typedef CDynamicBuffer<unsigned char> CByteDynamicBuffer;
       
    46 
       
    47 #endif