mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-25 06:36:37 +01:00
Backport Buffer from the old trunk
This commit is contained in:
@@ -41,8 +41,8 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Foundation.h"
|
#include "Poco/Foundation.h"
|
||||||
#include <cstring>
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
namespace Poco {
|
namespace Poco {
|
||||||
@@ -50,65 +50,131 @@ namespace Poco {
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class Buffer
|
class Buffer
|
||||||
/// A very simple buffer class that allocates a buffer of
|
/// A buffer class that allocates a buffer of a given type and size.
|
||||||
/// a given type and size in the constructor and
|
/// Buffer can be zero-size, resized, copy-constructed and assigned.
|
||||||
/// deallocates the buffer in the destructor.
|
/// Memory allocation, resizing and deallocation is managed automatically.
|
||||||
///
|
///
|
||||||
/// This class is useful everywhere where a temporary buffer
|
/// This class is useful everywhere where a temporary buffer
|
||||||
/// is needed.
|
/// is needed.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Buffer(std::size_t capacity):
|
Buffer(std::size_t size):
|
||||||
_capacity(capacity),
|
_size(size),
|
||||||
_used(capacity),
|
_ptr(size ? new T[size] : 0)
|
||||||
_ptr(new T[capacity])
|
/// Creates and allocates the Buffer.
|
||||||
/// Creates and allocates the Buffer.
|
{
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
Buffer(): _size(0), _ptr(0)
|
||||||
~Buffer()
|
/// Creates the Buffer.
|
||||||
/// Destroys the Buffer.
|
{
|
||||||
{
|
}
|
||||||
delete [] _ptr;
|
|
||||||
}
|
Buffer(const Buffer& other):
|
||||||
|
/// Copy constructor.
|
||||||
void resize(std::size_t newCapacity, bool preserveContent = true)
|
_size(other._size),
|
||||||
/// Resizes the buffer. If preserveContent is true,
|
_ptr(other._size ? new T[other._size] : 0)
|
||||||
/// the content of the old buffer is copied over to the
|
{
|
||||||
/// new buffer. The new capacity can be larger or smaller than
|
if (_size)
|
||||||
/// the current one, but it must not be 0.
|
std::memcpy(_ptr, other._ptr, _size * sizeof(T));
|
||||||
{
|
}
|
||||||
poco_assert(newCapacity);
|
|
||||||
|
|
||||||
if (newCapacity > _capacity)
|
Buffer& operator = (const Buffer& other)
|
||||||
{
|
/// Assignment operator.
|
||||||
T* ptr = new T[newCapacity];
|
{
|
||||||
if (preserveContent)
|
if (this != &other)
|
||||||
std::memcpy(ptr, _ptr, newCapacity);
|
{
|
||||||
|
Buffer tmp(other);
|
||||||
|
swap(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
delete [] _ptr;
|
return *this;
|
||||||
_ptr = ptr;
|
}
|
||||||
_capacity = newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
_used = newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t capacity() const
|
~Buffer()
|
||||||
/// Returns the allocated memory size.
|
/// Destroys the Buffer.
|
||||||
|
{
|
||||||
|
delete [] _ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(Buffer& other)
|
||||||
|
/// Swaps the buffer with another one.
|
||||||
|
{
|
||||||
|
using std::swap;
|
||||||
|
|
||||||
|
swap(_ptr, other._ptr);
|
||||||
|
swap(_size, other._size);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t size() const
|
||||||
|
/// Returns the size of the buffer.
|
||||||
{
|
{
|
||||||
return _capacity;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
/// Sets the contents of the bufer to zero.
|
||||||
|
{
|
||||||
|
std::memset(_ptr, 0, _size * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
std::size_t size() const
|
void resize(std::size_t newSize, bool preserve = false)
|
||||||
/// Returns the used size of the buffer.
|
/// Resizes the buffer. If preserve is true, the contents
|
||||||
{
|
/// of the buffer is preserved. If the newSize is smaller
|
||||||
return _used;
|
/// than the current size, data truncation will occur.
|
||||||
}
|
///
|
||||||
|
/// For efficiency sake, the default behavior is not to
|
||||||
T* begin()
|
/// preserve the contents.
|
||||||
/// Returns a pointer to the beginning of the buffer.
|
{
|
||||||
{
|
if (preserve)
|
||||||
|
{
|
||||||
|
if (_size == newSize) return;
|
||||||
|
Buffer tmp;
|
||||||
|
tmp = *this;
|
||||||
|
recreate(newSize);
|
||||||
|
std::size_t size = _size < tmp._size ? _size : tmp._size;
|
||||||
|
std::memcpy(_ptr, tmp._ptr, size * sizeof(T));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (_size == newSize)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
recreate(newSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(const T* buf, std::size_t sz)
|
||||||
|
/// Resizes this buffer and appends the argument buffer.
|
||||||
|
{
|
||||||
|
if (0 == sz) return;
|
||||||
|
std::size_t oldSize = _size;
|
||||||
|
resize(_size + sz, true);
|
||||||
|
std::memcpy(_ptr + oldSize, buf, sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(const Buffer& buf)
|
||||||
|
/// Resizes this buffer and appends the argument buffer.
|
||||||
|
{
|
||||||
|
append(buf.begin(), buf.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t elementSize() const
|
||||||
|
/// Returns the size of the buffer element.
|
||||||
|
{
|
||||||
|
return sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t byteCount() const
|
||||||
|
/// Returns the total length of the buffer in bytes.
|
||||||
|
{
|
||||||
|
return _size * sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
T* begin()
|
||||||
|
/// Returns a pointer to the beginning of the buffer.
|
||||||
|
{
|
||||||
return _ptr;
|
return _ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,37 +187,53 @@ public:
|
|||||||
T* end()
|
T* end()
|
||||||
/// Returns a pointer to end of the buffer.
|
/// Returns a pointer to end of the buffer.
|
||||||
{
|
{
|
||||||
return _ptr + _used;
|
return _ptr + _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
const T* end() const
|
const T* end() const
|
||||||
/// Returns a pointer to the end of the buffer.
|
/// Returns a pointer to the end of the buffer.
|
||||||
{
|
{
|
||||||
return _ptr + _used;
|
return _ptr + _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
T& operator [] (std::size_t index)
|
bool isEmpty() const
|
||||||
{
|
/// Return true if buffer is empty.
|
||||||
poco_assert (index < _used);
|
{
|
||||||
|
return 0 == _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& operator [] (std::size_t index)
|
||||||
|
{
|
||||||
|
poco_assert (index < _size);
|
||||||
|
|
||||||
return _ptr[index];
|
return _ptr[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
const T& operator [] (std::size_t index) const
|
const T& operator [] (std::size_t index) const
|
||||||
{
|
{
|
||||||
poco_assert (index < _used);
|
poco_assert (index < _size);
|
||||||
|
|
||||||
return _ptr[index];
|
return _ptr[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Buffer();
|
|
||||||
Buffer(const Buffer&);
|
|
||||||
Buffer& operator = (const Buffer&);
|
|
||||||
|
|
||||||
std::size_t _capacity;
|
void recreate(std::size_t newSize)
|
||||||
std::size_t _used;
|
{
|
||||||
T* _ptr;
|
if (newSize != _size)
|
||||||
|
{
|
||||||
|
delete [] _ptr;
|
||||||
|
if (0 == newSize)
|
||||||
|
_ptr = 0;
|
||||||
|
else
|
||||||
|
_ptr = new T[newSize];
|
||||||
|
|
||||||
|
_size = newSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t _size;
|
||||||
|
T* _ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user