annotate src/win32/7zip/7z/CPP/Common/Buffer.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/Buffer.h
rlm@1 2
rlm@1 3 #ifndef __COMMON_BUFFER_H
rlm@1 4 #define __COMMON_BUFFER_H
rlm@1 5
rlm@1 6 #include "Defs.h"
rlm@1 7
rlm@1 8 template <class T> class CBuffer
rlm@1 9 {
rlm@1 10 protected:
rlm@1 11 size_t _capacity;
rlm@1 12 T *_items;
rlm@1 13 public:
rlm@1 14 void Free()
rlm@1 15 {
rlm@1 16 delete []_items;
rlm@1 17 _items = 0;
rlm@1 18 _capacity = 0;
rlm@1 19 }
rlm@1 20 CBuffer(): _capacity(0), _items(0) {};
rlm@1 21 CBuffer(const CBuffer &buffer): _capacity(0), _items(0) { *this = buffer; }
rlm@1 22 CBuffer(size_t size): _items(0), _capacity(0) { SetCapacity(size); }
rlm@1 23 virtual ~CBuffer() { delete []_items; }
rlm@1 24 operator T *() { return _items; };
rlm@1 25 operator const T *() const { return _items; };
rlm@1 26 size_t GetCapacity() const { return _capacity; }
rlm@1 27 void SetCapacity(size_t newCapacity)
rlm@1 28 {
rlm@1 29 if (newCapacity == _capacity)
rlm@1 30 return;
rlm@1 31 T *newBuffer;
rlm@1 32 if (newCapacity > 0)
rlm@1 33 {
rlm@1 34 newBuffer = new T[newCapacity];
rlm@1 35 if (_capacity > 0)
rlm@1 36 memmove(newBuffer, _items, MyMin(_capacity, newCapacity) * sizeof(T));
rlm@1 37 }
rlm@1 38 else
rlm@1 39 newBuffer = 0;
rlm@1 40 delete []_items;
rlm@1 41 _items = newBuffer;
rlm@1 42 _capacity = newCapacity;
rlm@1 43 }
rlm@1 44 CBuffer& operator=(const CBuffer &buffer)
rlm@1 45 {
rlm@1 46 Free();
rlm@1 47 if (buffer._capacity > 0)
rlm@1 48 {
rlm@1 49 SetCapacity(buffer._capacity);
rlm@1 50 memmove(_items, buffer._items, buffer._capacity * sizeof(T));
rlm@1 51 }
rlm@1 52 return *this;
rlm@1 53 }
rlm@1 54 };
rlm@1 55
rlm@1 56 template <class T>
rlm@1 57 bool operator==(const CBuffer<T>& b1, const CBuffer<T>& b2)
rlm@1 58 {
rlm@1 59 if (b1.GetCapacity() != b2.GetCapacity())
rlm@1 60 return false;
rlm@1 61 for (size_t i = 0; i < b1.GetCapacity(); i++)
rlm@1 62 if (b1[i] != b2[i])
rlm@1 63 return false;
rlm@1 64 return true;
rlm@1 65 }
rlm@1 66
rlm@1 67 template <class T>
rlm@1 68 bool operator!=(const CBuffer<T>& b1, const CBuffer<T>& b2)
rlm@1 69 {
rlm@1 70 return !(b1 == b2);
rlm@1 71 }
rlm@1 72
rlm@1 73 typedef CBuffer<char> CCharBuffer;
rlm@1 74 typedef CBuffer<wchar_t> CWCharBuffer;
rlm@1 75 typedef CBuffer<unsigned char> CByteBuffer;
rlm@1 76
rlm@1 77 #endif