diff src/win32/7zip/7z/CPP/Common/Buffer.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Common/Buffer.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,77 @@
     1.4 +// Common/Buffer.h
     1.5 +
     1.6 +#ifndef __COMMON_BUFFER_H
     1.7 +#define __COMMON_BUFFER_H
     1.8 +
     1.9 +#include "Defs.h"
    1.10 +
    1.11 +template <class T> class CBuffer
    1.12 +{
    1.13 +protected:
    1.14 +  size_t _capacity;
    1.15 +  T *_items;
    1.16 +public:
    1.17 +  void Free()
    1.18 +  {
    1.19 +    delete []_items;
    1.20 +    _items = 0;
    1.21 +    _capacity = 0;
    1.22 +  }
    1.23 +  CBuffer(): _capacity(0), _items(0) {};
    1.24 +  CBuffer(const CBuffer &buffer): _capacity(0), _items(0) { *this = buffer; }
    1.25 +  CBuffer(size_t size): _items(0),  _capacity(0) {  SetCapacity(size); }
    1.26 +  virtual ~CBuffer() { delete []_items; }
    1.27 +  operator T *() { return _items; };
    1.28 +  operator const T *() const { return _items; };
    1.29 +  size_t GetCapacity() const { return  _capacity; }
    1.30 +  void SetCapacity(size_t newCapacity)
    1.31 +  {
    1.32 +    if (newCapacity == _capacity)
    1.33 +      return;
    1.34 +    T *newBuffer;
    1.35 +    if (newCapacity > 0)
    1.36 +    {
    1.37 +      newBuffer = new T[newCapacity];
    1.38 +      if (_capacity > 0)
    1.39 +        memmove(newBuffer, _items, MyMin(_capacity, newCapacity) * sizeof(T));
    1.40 +    }
    1.41 +    else
    1.42 +      newBuffer = 0;
    1.43 +    delete []_items;
    1.44 +    _items = newBuffer;
    1.45 +    _capacity = newCapacity;
    1.46 +  }
    1.47 +  CBuffer& operator=(const CBuffer &buffer)
    1.48 +  {
    1.49 +    Free();
    1.50 +    if (buffer._capacity > 0)
    1.51 +    {
    1.52 +      SetCapacity(buffer._capacity);
    1.53 +      memmove(_items, buffer._items, buffer._capacity * sizeof(T));
    1.54 +    }
    1.55 +    return *this;
    1.56 +  }
    1.57 +};
    1.58 +
    1.59 +template <class T>
    1.60 +bool operator==(const CBuffer<T>& b1, const CBuffer<T>& b2)
    1.61 +{
    1.62 +  if (b1.GetCapacity() != b2.GetCapacity())
    1.63 +    return false;
    1.64 +  for (size_t i = 0; i < b1.GetCapacity(); i++)
    1.65 +    if (b1[i] != b2[i])
    1.66 +      return false;
    1.67 +  return true;
    1.68 +}
    1.69 +
    1.70 +template <class T>
    1.71 +bool operator!=(const CBuffer<T>& b1, const CBuffer<T>& b2)
    1.72 +{
    1.73 +  return !(b1 == b2);
    1.74 +}
    1.75 +
    1.76 +typedef CBuffer<char> CCharBuffer;
    1.77 +typedef CBuffer<wchar_t> CWCharBuffer;
    1.78 +typedef CBuffer<unsigned char> CByteBuffer;
    1.79 +
    1.80 +#endif