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