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