1 // Common/MyBuffer.h 2 3 #ifndef __COMMON_MY_BUFFER_H 4 #define __COMMON_MY_BUFFER_H 5 6 #include "Defs.h" 7 #include "MyTypes.h" 8 9 /* 7-Zip now uses CBuffer only as CByteBuffer. 10 So there is no need to use MY_ARRAY_NEW macro in CBuffer code. */ 11 12 template <class T> class CBuffer 13 { 14 T *_items; 15 size_t _size; 16 17 public: Free()18 void Free() 19 { 20 if (_items) 21 { 22 delete []_items; 23 _items = 0; 24 } 25 _size = 0; 26 } 27 CBuffer()28 CBuffer(): _items(0), _size(0) {}; CBuffer(size_t size)29 CBuffer(size_t size): _items(0), _size(0) { _items = new T[size]; _size = size; } CBuffer(const CBuffer & buffer)30 CBuffer(const CBuffer &buffer): _items(0), _size(0) 31 { 32 size_t size = buffer._size; 33 if (size != 0) 34 { 35 _items = new T[size]; 36 memcpy(_items, buffer._items, size * sizeof(T)); 37 _size = size; 38 } 39 } 40 ~CBuffer()41 ~CBuffer() { delete []_items; } 42 43 operator T *() { return _items; } 44 operator const T *() const { return _items; } Size()45 size_t Size() const { return _size; } 46 Alloc(size_t size)47 void Alloc(size_t size) 48 { 49 if (size != _size) 50 { 51 Free(); 52 if (size != 0) 53 { 54 _items = new T[size]; 55 _size = size; 56 } 57 } 58 } 59 AllocAtLeast(size_t size)60 void AllocAtLeast(size_t size) 61 { 62 if (size > _size) 63 { 64 Free(); 65 _items = new T[size]; 66 _size = size; 67 } 68 } 69 CopyFrom(const T * data,size_t size)70 void CopyFrom(const T *data, size_t size) 71 { 72 Alloc(size); 73 if (size != 0) 74 memcpy(_items, data, size * sizeof(T)); 75 } 76 ChangeSize_KeepData(size_t newSize,size_t keepSize)77 void ChangeSize_KeepData(size_t newSize, size_t keepSize) 78 { 79 if (newSize == _size) 80 return; 81 T *newBuffer = NULL; 82 if (newSize != 0) 83 { 84 newBuffer = new T[newSize]; 85 if (keepSize > _size) 86 keepSize = _size; 87 if (keepSize != 0) 88 memcpy(newBuffer, _items, MyMin(keepSize, newSize) * sizeof(T)); 89 } 90 delete []_items; 91 _items = newBuffer; 92 _size = newSize; 93 } 94 Wipe()95 void Wipe() 96 { 97 if (_size != 0) 98 memset(_items, 0, _size * sizeof(T)); 99 } 100 101 CBuffer& operator=(const CBuffer &buffer) 102 { 103 if (&buffer != this) 104 CopyFrom(buffer, buffer._size); 105 return *this; 106 } 107 }; 108 109 template <class T> 110 bool operator==(const CBuffer<T>& b1, const CBuffer<T>& b2) 111 { 112 size_t size1 = b1.Size(); 113 if (size1 != b2.Size()) 114 return false; 115 if (size1 == 0) 116 return true; 117 return memcmp(b1, b2, size1 * sizeof(T)) == 0; 118 } 119 120 template <class T> 121 bool operator!=(const CBuffer<T>& b1, const CBuffer<T>& b2) 122 { 123 size_t size1 = b1.Size(); 124 if (size1 != b2.Size()) 125 return true; 126 if (size1 == 0) 127 return false; 128 return memcmp(b1, b2, size1 * sizeof(T)) != 0; 129 } 130 131 132 // typedef CBuffer<char> CCharBuffer; 133 // typedef CBuffer<wchar_t> CWCharBuffer; 134 typedef CBuffer<unsigned char> CByteBuffer; 135 136 137 class CByteBuffer_Wipe: public CByteBuffer 138 { CLASS_NO_COPY(CByteBuffer_Wipe)139 CLASS_NO_COPY(CByteBuffer_Wipe) 140 public: 141 // CByteBuffer_Wipe(): CBuffer<unsigned char>() {} 142 CByteBuffer_Wipe(size_t size): CBuffer<unsigned char>(size) {} ~CByteBuffer_Wipe()143 ~CByteBuffer_Wipe() { Wipe(); } 144 }; 145 146 147 148 template <class T> class CObjArray 149 { 150 protected: 151 T *_items; 152 private: 153 // we disable copy 154 CObjArray(const CObjArray &buffer); 155 void operator=(const CObjArray &buffer); 156 public: Free()157 void Free() 158 { 159 delete []_items; 160 _items = 0; 161 } CObjArray(size_t size)162 CObjArray(size_t size): _items(0) 163 { 164 if (size != 0) 165 { 166 MY_ARRAY_NEW(_items, T, size) 167 // _items = new T[size]; 168 } 169 } CObjArray()170 CObjArray(): _items(0) {}; ~CObjArray()171 ~CObjArray() { delete []_items; } 172 173 operator T *() { return _items; } 174 operator const T *() const { return _items; } 175 Alloc(size_t newSize)176 void Alloc(size_t newSize) 177 { 178 delete []_items; 179 _items = 0; 180 MY_ARRAY_NEW(_items, T, newSize) 181 // _items = new T[newSize]; 182 } 183 }; 184 185 typedef CObjArray<unsigned char> CByteArr; 186 typedef CObjArray<bool> CBoolArr; 187 typedef CObjArray<int> CIntArr; 188 typedef CObjArray<unsigned> CUIntArr; 189 190 191 template <class T> class CObjArray2 192 { 193 T *_items; 194 unsigned _size; 195 196 // we disable copy 197 CObjArray2(const CObjArray2 &buffer); 198 void operator=(const CObjArray2 &buffer); 199 public: 200 Free()201 void Free() 202 { 203 delete []_items; 204 _items = 0; 205 _size = 0; 206 } CObjArray2()207 CObjArray2(): _items(0), _size(0) {}; 208 /* 209 CObjArray2(const CObjArray2 &buffer): _items(0), _size(0) 210 { 211 size_t newSize = buffer._size; 212 if (newSize != 0) 213 { 214 T *newBuffer = new T[newSize];; 215 _items = newBuffer; 216 _size = newSize; 217 const T *src = buffer; 218 for (size_t i = 0; i < newSize; i++) 219 newBuffer[i] = src[i]; 220 } 221 } 222 */ 223 /* 224 CObjArray2(size_t size): _items(0), _size(0) 225 { 226 if (size != 0) 227 { 228 _items = new T[size]; 229 _size = size; 230 } 231 } 232 */ 233 ~CObjArray2()234 ~CObjArray2() { delete []_items; } 235 236 operator T *() { return _items; } 237 operator const T *() const { return _items; } 238 Size()239 unsigned Size() const { return (unsigned)_size; } IsEmpty()240 bool IsEmpty() const { return _size == 0; } 241 242 // SetSize doesn't keep old items. It allocates new array if size is not equal SetSize(unsigned size)243 void SetSize(unsigned size) 244 { 245 if (size == _size) 246 return; 247 T *newBuffer = NULL; 248 if (size != 0) 249 { 250 MY_ARRAY_NEW(newBuffer, T, size) 251 // newBuffer = new T[size]; 252 } 253 delete []_items; 254 _items = newBuffer; 255 _size = size; 256 } 257 258 /* 259 CObjArray2& operator=(const CObjArray2 &buffer) 260 { 261 Free(); 262 size_t newSize = buffer._size; 263 if (newSize != 0) 264 { 265 T *newBuffer = new T[newSize];; 266 _items = newBuffer; 267 _size = newSize; 268 const T *src = buffer; 269 for (size_t i = 0; i < newSize; i++) 270 newBuffer[i] = src[i]; 271 } 272 return *this; 273 } 274 */ 275 }; 276 277 #endif 278