mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-06 00:31:10 +01:00
clear() and prevention of resizing for same size
This commit is contained in:
parent
c2d5c64159
commit
5e72766ea6
@ -112,6 +112,12 @@ public:
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
/// Sets the contents of the bufer to zero.
|
||||||
|
{
|
||||||
|
std::memset(_ptr, 0, _size * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
void resize(std::size_t newSize, bool preserve = false)
|
void resize(std::size_t newSize, bool preserve = false)
|
||||||
/// Resizes the buffer. If preserve is true, the contents
|
/// Resizes the buffer. If preserve is true, the contents
|
||||||
/// of the buffer is preserved. If the newSize is smaller
|
/// of the buffer is preserved. If the newSize is smaller
|
||||||
@ -122,6 +128,7 @@ public:
|
|||||||
{
|
{
|
||||||
if (preserve)
|
if (preserve)
|
||||||
{
|
{
|
||||||
|
if (_size == newSize) return;
|
||||||
Buffer tmp;
|
Buffer tmp;
|
||||||
tmp = *this;
|
tmp = *this;
|
||||||
recreate(newSize);
|
recreate(newSize);
|
||||||
@ -129,6 +136,11 @@ public:
|
|||||||
std::memcpy(_ptr, tmp._ptr, size * sizeof(T));
|
std::memcpy(_ptr, tmp._ptr, size * sizeof(T));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (_size == newSize)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
recreate(newSize);
|
recreate(newSize);
|
||||||
}
|
}
|
||||||
@ -148,8 +160,14 @@ public:
|
|||||||
append(buf.begin(), buf.size());
|
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
|
std::size_t byteCount() const
|
||||||
/// Returns the total length of the buffer in bytes .
|
/// Returns the total length of the buffer in bytes.
|
||||||
{
|
{
|
||||||
return _size * sizeof(T);
|
return _size * sizeof(T);
|
||||||
}
|
}
|
||||||
@ -202,13 +220,16 @@ private:
|
|||||||
|
|
||||||
void recreate(std::size_t newSize)
|
void recreate(std::size_t newSize)
|
||||||
{
|
{
|
||||||
delete [] _ptr;
|
if (newSize != _size)
|
||||||
if (0 == newSize)
|
{
|
||||||
_ptr = 0;
|
delete [] _ptr;
|
||||||
else
|
if (0 == newSize)
|
||||||
_ptr = new T[newSize];
|
_ptr = 0;
|
||||||
|
else
|
||||||
|
_ptr = new T[newSize];
|
||||||
|
|
||||||
_size = newSize;
|
_size = newSize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t _size;
|
std::size_t _size;
|
||||||
|
@ -216,6 +216,11 @@ void CoreTest::testBuffer()
|
|||||||
for (int j = 0; i < s; ++i, ++j)
|
for (int j = 0; i < s; ++i, ++j)
|
||||||
assert (b1[i] == j);
|
assert (b1[i] == j);
|
||||||
|
|
||||||
|
b1.clear();
|
||||||
|
pi = b1.begin();
|
||||||
|
for (; pi != b1.end(); ++pi)
|
||||||
|
assert (*pi == 0);
|
||||||
|
|
||||||
b1.resize(0);
|
b1.resize(0);
|
||||||
assert (b1.isEmpty());
|
assert (b1.isEmpty());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user