clear() and prevention of resizing for same size

This commit is contained in:
Aleksandar Fabijanic 2009-03-16 19:14:51 +00:00
parent c2d5c64159
commit 5e72766ea6
2 changed files with 33 additions and 7 deletions

View File

@ -112,6 +112,12 @@ public:
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)
/// Resizes the buffer. If preserve is true, the contents
/// of the buffer is preserved. If the newSize is smaller
@ -122,6 +128,7 @@ public:
{
if (preserve)
{
if (_size == newSize) return;
Buffer tmp;
tmp = *this;
recreate(newSize);
@ -129,6 +136,11 @@ public:
std::memcpy(_ptr, tmp._ptr, size * sizeof(T));
return;
}
else if (_size == newSize)
{
clear();
return;
}
recreate(newSize);
}
@ -148,8 +160,14 @@ public:
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 .
/// Returns the total length of the buffer in bytes.
{
return _size * sizeof(T);
}
@ -202,13 +220,16 @@ private:
void recreate(std::size_t newSize)
{
delete [] _ptr;
if (0 == newSize)
_ptr = 0;
else
_ptr = new T[newSize];
if (newSize != _size)
{
delete [] _ptr;
if (0 == newSize)
_ptr = 0;
else
_ptr = new T[newSize];
_size = newSize;
_size = newSize;
}
}
std::size_t _size;

View File

@ -216,6 +216,11 @@ void CoreTest::testBuffer()
for (int j = 0; i < s; ++i, ++j)
assert (b1[i] == j);
b1.clear();
pi = b1.begin();
for (; pi != b1.end(); ++pi)
assert (*pi == 0);
b1.resize(0);
assert (b1.isEmpty());