Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Common/DynamicBuffer.h |
michael@0 | 2 | |
michael@0 | 3 | #ifndef __COMMON_DYNAMICBUFFER_H |
michael@0 | 4 | #define __COMMON_DYNAMICBUFFER_H |
michael@0 | 5 | |
michael@0 | 6 | #include "Buffer.h" |
michael@0 | 7 | |
michael@0 | 8 | template <class T> class CDynamicBuffer: public CBuffer<T> |
michael@0 | 9 | { |
michael@0 | 10 | void GrowLength(size_t size) |
michael@0 | 11 | { |
michael@0 | 12 | size_t delta; |
michael@0 | 13 | if (this->_capacity > 64) |
michael@0 | 14 | delta = this->_capacity / 4; |
michael@0 | 15 | else if (this->_capacity > 8) |
michael@0 | 16 | delta = 16; |
michael@0 | 17 | else |
michael@0 | 18 | delta = 4; |
michael@0 | 19 | delta = MyMax(delta, size); |
michael@0 | 20 | SetCapacity(this->_capacity + delta); |
michael@0 | 21 | } |
michael@0 | 22 | public: |
michael@0 | 23 | CDynamicBuffer(): CBuffer<T>() {}; |
michael@0 | 24 | CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer<T>(buffer) {}; |
michael@0 | 25 | CDynamicBuffer(size_t size): CBuffer<T>(size) {}; |
michael@0 | 26 | CDynamicBuffer& operator=(const CDynamicBuffer &buffer) |
michael@0 | 27 | { |
michael@0 | 28 | this->Free(); |
michael@0 | 29 | if(buffer._capacity > 0) |
michael@0 | 30 | { |
michael@0 | 31 | SetCapacity(buffer._capacity); |
michael@0 | 32 | memmove(this->_items, buffer._items, buffer._capacity * sizeof(T)); |
michael@0 | 33 | } |
michael@0 | 34 | return *this; |
michael@0 | 35 | } |
michael@0 | 36 | void EnsureCapacity(size_t capacity) |
michael@0 | 37 | { |
michael@0 | 38 | if (this->_capacity < capacity) |
michael@0 | 39 | GrowLength(capacity - this->_capacity); |
michael@0 | 40 | } |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | typedef CDynamicBuffer<char> CCharDynamicBuffer; |
michael@0 | 44 | typedef CDynamicBuffer<wchar_t> CWCharDynamicBuffer; |
michael@0 | 45 | typedef CDynamicBuffer<unsigned char> CByteDynamicBuffer; |
michael@0 | 46 | |
michael@0 | 47 | #endif |