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