diff --git a/Foundation/include/Poco/Buffer.h b/Foundation/include/Poco/Buffer.h index d35fff7fa..ed5c6fb5e 100644 --- a/Foundation/include/Poco/Buffer.h +++ b/Foundation/include/Poco/Buffer.h @@ -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; diff --git a/Foundation/testsuite/src/CoreTest.cpp b/Foundation/testsuite/src/CoreTest.cpp index 2ebaa32a9..c6879a583 100644 --- a/Foundation/testsuite/src/CoreTest.cpp +++ b/Foundation/testsuite/src/CoreTest.cpp @@ -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());