michael@0: // Common/Buffer.h michael@0: michael@0: #ifndef __COMMON_BUFFER_H michael@0: #define __COMMON_BUFFER_H michael@0: michael@0: #include "Defs.h" michael@0: michael@0: template class CBuffer michael@0: { michael@0: protected: michael@0: size_t _capacity; michael@0: T *_items; michael@0: void Free() michael@0: { michael@0: delete []_items; michael@0: _items = 0; michael@0: _capacity = 0; michael@0: } michael@0: public: michael@0: CBuffer(): _capacity(0), _items(0) {}; michael@0: CBuffer(const CBuffer &buffer): _capacity(0), _items(0) { *this = buffer; } michael@0: CBuffer(size_t size): _items(0), _capacity(0) { SetCapacity(size); } michael@0: virtual ~CBuffer() { delete []_items; } michael@0: operator T *() { return _items; }; michael@0: operator const T *() const { return _items; }; michael@0: size_t GetCapacity() const { return _capacity; } michael@0: void SetCapacity(size_t newCapacity) michael@0: { michael@0: if (newCapacity == _capacity) michael@0: return; michael@0: T *newBuffer; michael@0: if (newCapacity > 0) michael@0: { michael@0: newBuffer = new T[newCapacity]; michael@0: if(_capacity > 0) michael@0: memmove(newBuffer, _items, MyMin(_capacity, newCapacity) * sizeof(T)); michael@0: } michael@0: else michael@0: newBuffer = 0; michael@0: delete []_items; michael@0: _items = newBuffer; michael@0: _capacity = newCapacity; michael@0: } michael@0: CBuffer& operator=(const CBuffer &buffer) michael@0: { michael@0: Free(); michael@0: if(buffer._capacity > 0) michael@0: { michael@0: SetCapacity(buffer._capacity); michael@0: memmove(_items, buffer._items, buffer._capacity * sizeof(T)); michael@0: } michael@0: return *this; michael@0: } michael@0: }; michael@0: michael@0: template michael@0: bool operator==(const CBuffer& b1, const CBuffer& b2) michael@0: { michael@0: if (b1.GetCapacity() != b2.GetCapacity()) michael@0: return false; michael@0: for (size_t i = 0; i < b1.GetCapacity(); i++) michael@0: if (b1[i] != b2[i]) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: bool operator!=(const CBuffer& b1, const CBuffer& b2) michael@0: { michael@0: return !(b1 == b2); michael@0: } michael@0: michael@0: typedef CBuffer CCharBuffer; michael@0: typedef CBuffer CWCharBuffer; michael@0: typedef CBuffer CByteBuffer; michael@0: michael@0: #endif