renamed FIFOBuffer => BasicFIFOBufer, FIFOBuffer typedef, renamed Bufer::allocated() => Buffer:capacity() to match STL convention

This commit is contained in:
Aleksandar Fabijanic
2012-04-29 02:37:54 +00:00
parent d22ecbaa3e
commit e10566779e
3 changed files with 33 additions and 24 deletions

View File

@@ -59,7 +59,7 @@ class Buffer
{ {
public: public:
Buffer(std::size_t size): Buffer(std::size_t size):
_size(size), _capacity(size),
_used(size), _used(size),
_ptr(new T[size]) _ptr(new T[size])
/// Creates and allocates the Buffer. /// Creates and allocates the Buffer.
@@ -72,32 +72,32 @@ public:
delete [] _ptr; delete [] _ptr;
} }
void resize(std::size_t newSize, bool preserveContent = true) void resize(std::size_t newCapacity, bool preserveContent = true)
/// Resizes the buffer. If preserveContent is true, /// Resizes the buffer. If preserveContent is true,
/// the content of the old buffer is copied over to the /// the content of the old buffer is copied over to the
/// new buffer. NewSize can be larger or smaller than /// new buffer. NewSize can be larger or smaller than
/// the current size, but it must not be 0. /// the current size, but it must not be 0.
{ {
poco_assert(newSize); poco_assert(newCapacity);
if (newSize > _size) if (newCapacity > _capacity)
{ {
T* ptr = new T[newSize]; T* ptr = new T[newCapacity];
if (preserveContent) if (preserveContent)
std::memcpy(ptr, _ptr, newSize); std::memcpy(ptr, _ptr, newCapacity);
delete [] _ptr; delete [] _ptr;
_ptr = ptr; _ptr = ptr;
_size = newSize; _capacity = newCapacity;
} }
_used = newSize; _used = newCapacity;
} }
std::size_t allocated() const std::size_t capacity() const
/// Returns the allocated memory size. /// Returns the allocated memory size.
{ {
return _size; return _capacity;
} }
std::size_t size() const std::size_t size() const
@@ -149,7 +149,7 @@ private:
Buffer(const Buffer&); Buffer(const Buffer&);
Buffer& operator = (const Buffer&); Buffer& operator = (const Buffer&);
std::size_t _size; std::size_t _capacity;
std::size_t _used; std::size_t _used;
T* _ptr; T* _ptr;
}; };

View File

@@ -52,7 +52,7 @@ namespace Poco {
template <class T> template <class T>
class FIFOBuffer class BasicFIFOBuffer
/// A simple buffer class with support for re-entrant, /// A simple buffer class with support for re-entrant,
/// FIFO-style read/write operations. as well as /// FIFO-style read/write operations. as well as
/// empty/full transition notifications. Buffer size /// empty/full transition notifications. Buffer size
@@ -63,6 +63,8 @@ class FIFOBuffer
/// is needed. /// is needed.
{ {
public: public:
typedef T Type;
mutable Poco::BasicEvent<bool> Writable; mutable Poco::BasicEvent<bool> Writable;
/// Event indicating "writeability" of the buffer, /// Event indicating "writeability" of the buffer,
/// triggerred as follows: /// triggerred as follows:
@@ -87,7 +89,7 @@ public:
/// Readable event observers are notified, with true value /// Readable event observers are notified, with true value
/// as the argument /// as the argument
FIFOBuffer(std::size_t size, bool notify = false): BasicFIFOBuffer(std::size_t size, bool notify = false):
_buffer(size), _buffer(size),
_begin(0), _begin(0),
_used(0), _used(0),
@@ -96,7 +98,7 @@ public:
{ {
} }
~FIFOBuffer() ~BasicFIFOBuffer()
/// Destroys the FIFOBuffer. /// Destroys the FIFOBuffer.
{ {
} }
@@ -253,9 +255,9 @@ private:
Writable.notify(this, f); Writable.notify(this, f);
} }
FIFOBuffer(); BasicFIFOBuffer();
FIFOBuffer(const FIFOBuffer&); BasicFIFOBuffer(const BasicFIFOBuffer&);
FIFOBuffer& operator = (const FIFOBuffer&); BasicFIFOBuffer& operator = (const BasicFIFOBuffer&);
Buffer<T> _buffer; Buffer<T> _buffer;
std::size_t _begin; std::size_t _begin;
@@ -265,6 +267,12 @@ private:
}; };
//
// We provide an instantiation for char
//
typedef BasicFIFOBuffer<char> FIFOBuffer;
} // namespace Poco } // namespace Poco

View File

@@ -57,6 +57,7 @@ using Poco::Environment;
using Poco::Thread; using Poco::Thread;
using Poco::Runnable; using Poco::Runnable;
using Poco::Buffer; using Poco::Buffer;
using Poco::BasicFIFOBuffer;
using Poco::FIFOBuffer; using Poco::FIFOBuffer;
using Poco::AtomicCounter; using Poco::AtomicCounter;
using Poco::Nullable; using Poco::Nullable;
@@ -198,7 +199,7 @@ void CoreTest::testBuffer()
std::size_t s = 10; std::size_t s = 10;
Buffer<int> b(s); Buffer<int> b(s);
assert (b.size() == s); assert (b.size() == s);
assert (b.allocated() == s); assert (b.capacity() == s);
std::vector<int> v; std::vector<int> v;
for (int i = 0; i < s; ++i) for (int i = 0; i < s; ++i)
v.push_back(i); v.push_back(i);
@@ -214,7 +215,7 @@ void CoreTest::testBuffer()
assert (b[i] == i); assert (b[i] == i);
assert (b.size() == s/2); assert (b.size() == s/2);
assert (b.allocated() == s); assert (b.capacity() == s);
b.resize(s*2); b.resize(s*2);
v.clear(); v.clear();
@@ -227,7 +228,7 @@ void CoreTest::testBuffer()
assert (b[i] == i); assert (b[i] == i);
assert (b.size() == s*2); assert (b.size() == s*2);
assert (b.allocated() == s*2); assert (b.capacity() == s*2);
#if ENABLE_BUGCHECK_TEST #if ENABLE_BUGCHECK_TEST
try { int i = b[s]; fail ("must fail"); } try { int i = b[s]; fail ("must fail"); }
@@ -238,9 +239,9 @@ void CoreTest::testBuffer()
void CoreTest::testFIFOBufferChar() void CoreTest::testFIFOBufferChar()
{ {
typedef char T; typedef FIFOBuffer::Type T;
FIFOBuffer<T> f(20, true); FIFOBuffer f(20, true);
Buffer<T> b(10); Buffer<T> b(10);
std::vector<T> v; std::vector<T> v;
@@ -414,7 +415,7 @@ void CoreTest::testFIFOBufferInt()
{ {
typedef char T; typedef char T;
FIFOBuffer<T> f(20); BasicFIFOBuffer<T> f(20);
Buffer<T> b(10); Buffer<T> b(10);
std::vector<T> v; std::vector<T> v;