FIFOBuffer implementation and tests

This commit is contained in:
Aleksandar Fabijanic 2012-04-25 04:43:01 +00:00
parent e245eeed99
commit 886af0816a
10 changed files with 7152 additions and 6730 deletions

File diff suppressed because it is too large Load Diff

View File

@ -988,6 +988,7 @@
<ClInclude Include="include\Poco\Environment_WIN32.h" />
<ClInclude Include="include\Poco\Environment_WIN32U.h" />
<ClInclude Include="include\Poco\Exception.h" />
<ClInclude Include="include\Poco\FIFOBuffer.h" />
<ClInclude Include="include\Poco\Format.h" />
<ClInclude Include="include\Poco\Foundation.h" />
<ClInclude Include="include\Poco\FPEnvironment.h" />

View File

@ -1799,6 +1799,9 @@
<ClInclude Include="include\Poco\Array.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Poco\FIFOBuffer.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="src\pocomsg.rc">

View File

@ -841,6 +841,9 @@
<File
RelativePath=".\include\Poco\Exception.h">
</File>
<File
RelativePath=".\include\Poco\FIFOBuffer.h">
</File>
<File
RelativePath=".\include\Poco\Format.h">
</File>

View File

@ -1131,6 +1131,10 @@
RelativePath=".\include\Poco\Exception.h"
>
</File>
<File
RelativePath=".\include\Poco\FIFOBuffer.h"
>
</File>
<File
RelativePath=".\include\Poco\Format.h"
>

View File

@ -1128,6 +1128,10 @@
RelativePath=".\include\Poco\Exception.h"
>
</File>
<File
RelativePath=".\include\Poco\FIFOBuffer.h"
>
</File>
<File
RelativePath=".\include\Poco\Format.h"
>

View File

@ -77,6 +77,8 @@ public:
/// new buffer. NewSize can be larger or smaller than
/// the current size, but it must not be 0.
{
poco_assert(newSize);
T* ptr = new T[newSize];
if (preserveContent)
{
@ -87,7 +89,7 @@ public:
_ptr = ptr;
_size = newSize;
}
std::size_t size() const
/// Returns the size of the buffer.
{

View File

@ -0,0 +1,211 @@
//
// FIFOBuffer.h
//
// $Id: //poco/1.4/Foundation/include/Poco/FIFOBuffer.h#2 $
//
// Library: Foundation
// Package: Core
// Module: FIFOBuffer
//
// Definition of the FIFOBuffer class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_FIFOBuffer_INCLUDED
#define Foundation_FIFOBuffer_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Buffer.h"
#include "Poco/Mutex.h"
#include "Poco/Format.h"
namespace Poco {
template <class T>
class FIFOBuffer
/// A simple buffer class with support for re-entrant,
/// FIFO-style read/write operations as well as
/// introspection into the size of buffer, amount of unread data
/// and available space.
///
/// This class is useful everywhere where a FIFO functionality
/// is needed.
{
public:
FIFOBuffer(std::size_t size):
_buffer(size),
_used(0)
/// Creates and allocates the FIFOBuffer.
{
}
~FIFOBuffer()
/// Destroys the FIFOBuffer.
{
}
void resize(std::size_t newSize, bool preserveContent = true)
/// Resizes the buffer. If preserveContent is true,
/// the content of the old buffer is preserved.
/// New size can be larger or smaller than
/// the current size, but it must not be 0.
/// Additionally, if the new length is smaller
/// than currently used length and preserveContent
/// is true, InvalidAccessException is thrown.
{
Mutex::ScopedLock lock(_mutex);
if (preserveContent && (newSize < _used))
throw InvalidAccessException("Can not resize FIFO without data loss.");
_buffer.resize(newSize, preserveContent);
if (_used > _buffer.size())
_used = _buffer.size();
}
Buffer<T>& peek(Poco::Buffer<T>& buffer, std::size_t length = 0) const
/// Peeks into the data currently in the FIFO
/// without actually extracting it.
/// Resizes the supplied buffer to the size of
///.data written to it. If length is not
/// supplied by the caller, the current FIFO used length
/// is substituted for it.
///
/// Returns the reference to the buffer.
{
Mutex::ScopedLock lock(_mutex);
if (0 == length || length > _used) length = _used;
poco_assert (length <= _buffer.size());
buffer.resize(length);
std::memcpy(buffer.begin(), _buffer.begin(), length * sizeof(T));
return buffer;
}
Buffer<T>& read(Poco::Buffer<T>& buffer, std::size_t length = 0)
/// Copies the data currently in the FIFO
/// into the supplied buffer.
/// Resizes the supplied buffer to the size of
///.data written to it.
///
/// Returns the reference to the buffer.
{
Mutex::ScopedLock lock(_mutex);
std::size_t usedBefore = _used;
peek(buffer, length);
_used -= buffer.size();
if (_used)
std::memmove(_buffer.begin(), _buffer.begin() + usedBefore - _used, _used);
return buffer;
}
std::size_t write(const Buffer<T>& buffer)
/// Writes data.size() to the FIFO buffer.
/// If there is no sufficient space for the whole
/// buffer to be written, data up to available
/// length is written.
///
/// Returns the length of data written.
{
Mutex::ScopedLock lock(_mutex);
poco_assert (_used <= _buffer.size());
std::size_t available = _buffer.size() - _used;
std::size_t len = buffer.size() > available ? available : buffer.size();
std::memcpy(_buffer.begin() + _used, buffer.begin(), len * sizeof(T));
_used += len;
poco_assert (_used <= _buffer.size());
return len;
}
std::size_t size() const
/// Returns the size of the buffer.
{
return _buffer.size();
}
std::size_t used() const
/// Returns the size of the used portion of the buffer.
{
return _used;
}
std::size_t available() const
/// Returns the size of the available portion of the buffer.
{
return _buffer.size() - _used;
}
T& operator [] (std::size_t index)
/// Returns value at index position.
/// Throws InvalidAccessException if index is larger than
/// the last valid (used) buffer position.
{
if (index >= _used)
throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1));
return _buffer[index];
}
const T& operator [] (std::size_t index) const
/// Returns value at index position.
/// Throws InvalidAccessException if index is larger than
/// the last valid (used) buffer position.
{
if (index >= _used)
throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1));
return _buffer[index];
}
bool isEmpty() const
/// Returns true is buffer is empty, flase otherwise.
{
return 0 == _used;
}
private:
FIFOBuffer();
FIFOBuffer(const FIFOBuffer&);
FIFOBuffer& operator = (const FIFOBuffer&);
Buffer<T> _buffer;
std::size_t _used;
mutable Mutex _mutex;
};
} // namespace Poco
#endif // Foundation_FIFOBuffer_INCLUDED

View File

@ -39,9 +39,11 @@
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Buffer.h"
#include "Poco/FIFOBuffer.h"
#include "Poco/AtomicCounter.h"
#include "Poco/Nullable.h"
#include "Poco/Ascii.h"
#include "Poco/Exception.h"
#include <iostream>
#include <vector>
#include <cstring>
@ -53,9 +55,11 @@ using Poco::Environment;
using Poco::Thread;
using Poco::Runnable;
using Poco::Buffer;
using Poco::FIFOBuffer;
using Poco::AtomicCounter;
using Poco::Nullable;
using Poco::Ascii;
using Poco::InvalidAccessException;
namespace
@ -206,6 +210,188 @@ void CoreTest::testBuffer()
}
void CoreTest::testFIFOBufferChar()
{
typedef char T;
FIFOBuffer<T> f(20);
Buffer<T> b(10);
std::vector<T> v;
for (T c = '0'; c < '0' + 10; ++c)
v.push_back(c);
std::memcpy(b.begin(), &v[0], sizeof(T) * v.size());
f.write(b);
assert (20 == f.size());
assert (10 == f.used());
assert (!f.isEmpty());
assert ('0' == f[0]);
assert ('1' == f[1]);
assert ('2' == f[2]);
assert ('3' == f[3]);
assert ('4' == f[4]);
assert ('5' == f[5]);
assert ('6' == f[6]);
assert ('7' == f[7]);
assert ('8' == f[8]);
assert ('9' == f[9]);
b.resize(5);
f.read(b, b.size());
assert (20 == f.size());
assert (5 == f.used());
assert (!f.isEmpty());
assert ('5' == f[0]);
assert ('6' == f[1]);
assert ('7' == f[2]);
assert ('8' == f[3]);
assert ('9' == f[4]);
try { T i = f[10]; fail ("must fail"); }
catch (InvalidAccessException&) { }
v.clear();
for (T c = 'a'; c < 'a' + 10; ++c)
v.push_back(c);
b.resize(10);
std::memcpy(b.begin(), &v[0], sizeof(T) * v.size());
f.write(b);
assert (20 == f.size());
assert (15 == f.used());
assert (!f.isEmpty());
assert ('5' == f[0]);
assert ('6' == f[1]);
assert ('7' == f[2]);
assert ('8' == f[3]);
assert ('9' == f[4]);
assert ('a' == f[5]);
assert ('b' == f[6]);
assert ('c' == f[7]);
assert ('d' == f[8]);
assert ('e' == f[9]);
assert ('f' == f[10]);
assert ('g' == f[11]);
assert ('h' == f[12]);
assert ('i' == f[13]);
assert ('j' == f[14]);
try { T i = f[15]; fail ("must fail"); }
catch (InvalidAccessException&) { }
f.read(b, 10);
assert (20 == f.size());
assert (5 == f.used());
assert (!f.isEmpty());
assert ('f' == f[0]);
assert ('g' == f[1]);
assert ('h' == f[2]);
assert ('i' == f[3]);
assert ('j' == f[4]);
try { T i = f[5]; fail ("must fail"); }
catch (InvalidAccessException&) { }
f.read(b, 6);
assert (5 == b.size());
assert (20 == f.size());
assert (0 == f.used());
try { T i = f[0]; fail ("must fail"); }
catch (InvalidAccessException&) { }
assert (f.isEmpty());
}
void CoreTest::testFIFOBufferInt()
{
typedef char T;
FIFOBuffer<T> f(20);
Buffer<T> b(10);
std::vector<T> v;
for (T c = 0; c < 10; ++c)
v.push_back(c);
std::memcpy(b.begin(), &v[0], sizeof(T) * v.size());
f.write(b);
assert (20 == f.size());
assert (10 == f.used());
assert (!f.isEmpty());
assert (0 == f[0]);
assert (1 == f[1]);
assert (2 == f[2]);
assert (3 == f[3]);
assert (4 == f[4]);
assert (5 == f[5]);
assert (6 == f[6]);
assert (7 == f[7]);
assert (8 == f[8]);
assert (9 == f[9]);
b.resize(5);
f.read(b, b.size());
assert (20 == f.size());
assert (5 == f.used());
assert (!f.isEmpty());
assert (5 == f[0]);
assert (6 == f[1]);
assert (7 == f[2]);
assert (8 == f[3]);
assert (9 == f[4]);
try { T i = f[10]; fail ("must fail"); }
catch (InvalidAccessException&) { }
v.clear();
for (T c = 10; c < 20; ++c)
v.push_back(c);
b.resize(10);
std::memcpy(b.begin(), &v[0], sizeof(T) * v.size());
f.write(b);
assert (20 == f.size());
assert (15 == f.used());
assert (!f.isEmpty());
assert (5 == f[0]);
assert (6 == f[1]);
assert (7 == f[2]);
assert (8 == f[3]);
assert (9 == f[4]);
assert (10 == f[5]);
assert (11 == f[6]);
assert (12 == f[7]);
assert (13 == f[8]);
assert (14 == f[9]);
assert (15 == f[10]);
assert (16 == f[11]);
assert (17 == f[12]);
assert (18 == f[13]);
assert (19 == f[14]);
try { T i = f[15]; fail ("must fail"); }
catch (InvalidAccessException&) { }
f.read(b, 10);
assert (20 == f.size());
assert (5 == f.used());
assert (!f.isEmpty());
assert (15 == f[0]);
assert (16 == f[1]);
assert (17 == f[2]);
assert (18 == f[3]);
assert (19 == f[4]);
try { T i = f[5]; fail ("must fail"); }
catch (InvalidAccessException&) { }
f.read(b, 6);
assert (5 == b.size());
assert (20 == f.size());
assert (0 == f.used());
try { T i = f[0]; fail ("must fail"); }
catch (InvalidAccessException&) { }
assert (f.isEmpty());
}
void CoreTest::testAtomicCounter()
{
AtomicCounter ac;
@ -376,6 +562,8 @@ CppUnit::Test* CoreTest::suite()
CppUnit_addTest(pSuite, CoreTest, testBugcheck);
CppUnit_addTest(pSuite, CoreTest, testEnvironment);
CppUnit_addTest(pSuite, CoreTest, testBuffer);
CppUnit_addTest(pSuite, CoreTest, testFIFOBufferChar);
CppUnit_addTest(pSuite, CoreTest, testFIFOBufferInt);
CppUnit_addTest(pSuite, CoreTest, testAtomicCounter);
CppUnit_addTest(pSuite, CoreTest, testNullable);
CppUnit_addTest(pSuite, CoreTest, testAscii);

View File

@ -52,6 +52,8 @@ public:
void testFPE();
void testEnvironment();
void testBuffer();
void testFIFOBufferChar();
void testFIFOBufferInt();
void testAtomicCounter();
void testNullable();
void testAscii();