other-licenses/7zstub/src/Common/Buffer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial