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