xref: /third_party/lzma/CPP/Common/DynamicBuffer.h (revision 370b324c)
1370b324cSopenharmony_ci// Common/DynamicBuffer.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_COMMON_DYNAMIC_BUFFER_H
4370b324cSopenharmony_ci#define ZIP7_INC_COMMON_DYNAMIC_BUFFER_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include <string.h>
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "Common.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_citemplate <class T> class CDynamicBuffer
11370b324cSopenharmony_ci{
12370b324cSopenharmony_ci  T *_items;
13370b324cSopenharmony_ci  size_t _size;
14370b324cSopenharmony_ci  size_t _pos;
15370b324cSopenharmony_ci
16370b324cSopenharmony_ci  CDynamicBuffer(const CDynamicBuffer &buffer);
17370b324cSopenharmony_ci  void operator=(const CDynamicBuffer &buffer);
18370b324cSopenharmony_ci
19370b324cSopenharmony_ci  void Grow(size_t size)
20370b324cSopenharmony_ci  {
21370b324cSopenharmony_ci    size_t delta = _size >= 64 ? _size : 64;
22370b324cSopenharmony_ci    if (delta < size)
23370b324cSopenharmony_ci      delta = size;
24370b324cSopenharmony_ci    size_t newCap = _size + delta;
25370b324cSopenharmony_ci    if (newCap < delta)
26370b324cSopenharmony_ci    {
27370b324cSopenharmony_ci      newCap = _size + size;
28370b324cSopenharmony_ci      if (newCap < size)
29370b324cSopenharmony_ci        throw 20120116;
30370b324cSopenharmony_ci    }
31370b324cSopenharmony_ci
32370b324cSopenharmony_ci    T *newBuffer = new T[newCap];
33370b324cSopenharmony_ci    if (_pos != 0)
34370b324cSopenharmony_ci      memcpy(newBuffer, _items, _pos * sizeof(T));
35370b324cSopenharmony_ci    delete []_items;
36370b324cSopenharmony_ci    _items = newBuffer;
37370b324cSopenharmony_ci    _size = newCap;
38370b324cSopenharmony_ci  }
39370b324cSopenharmony_ci
40370b324cSopenharmony_cipublic:
41370b324cSopenharmony_ci  CDynamicBuffer(): _items(NULL), _size(0), _pos(0) {}
42370b324cSopenharmony_ci  // operator T *() { return _items; }
43370b324cSopenharmony_ci  operator const T *() const { return _items; }
44370b324cSopenharmony_ci  ~CDynamicBuffer() { delete []_items; }
45370b324cSopenharmony_ci
46370b324cSopenharmony_ci  T *GetCurPtrAndGrow(size_t addSize)
47370b324cSopenharmony_ci  {
48370b324cSopenharmony_ci    size_t rem = _size - _pos;
49370b324cSopenharmony_ci    if (rem < addSize)
50370b324cSopenharmony_ci      Grow(addSize - rem);
51370b324cSopenharmony_ci    T *res = _items + _pos;
52370b324cSopenharmony_ci    _pos += addSize;
53370b324cSopenharmony_ci    return res;
54370b324cSopenharmony_ci  }
55370b324cSopenharmony_ci
56370b324cSopenharmony_ci  void AddData(const T *data, size_t size)
57370b324cSopenharmony_ci  {
58370b324cSopenharmony_ci    memcpy(GetCurPtrAndGrow(size), data, size * sizeof(T));
59370b324cSopenharmony_ci  }
60370b324cSopenharmony_ci
61370b324cSopenharmony_ci  size_t GetPos() const { return _pos; }
62370b324cSopenharmony_ci
63370b324cSopenharmony_ci  // void Empty() { _pos = 0; }
64370b324cSopenharmony_ci};
65370b324cSopenharmony_ci
66370b324cSopenharmony_citypedef CDynamicBuffer<unsigned char> CByteDynamicBuffer;
67370b324cSopenharmony_ci
68370b324cSopenharmony_ci#endif
69