2012-04-25 06:43:01 +02:00
|
|
|
//
|
|
|
|
// 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"
|
2012-04-27 06:41:50 +02:00
|
|
|
#include "Poco/BasicEvent.h"
|
2012-04-25 06:43:01 +02:00
|
|
|
#include "Poco/Mutex.h"
|
|
|
|
#include "Poco/Format.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2012-04-29 04:37:54 +02:00
|
|
|
class BasicFIFOBuffer
|
2012-04-25 06:43:01 +02:00
|
|
|
/// A simple buffer class with support for re-entrant,
|
2012-04-27 06:41:50 +02:00
|
|
|
/// FIFO-style read/write operations. as well as
|
|
|
|
/// empty/full transition notifications. Buffer size
|
|
|
|
/// introspection as well as amount of unread data and
|
|
|
|
/// available space are supported as well.
|
2012-04-25 06:43:01 +02:00
|
|
|
///
|
2012-04-27 06:41:50 +02:00
|
|
|
/// This class is useful anywhere where a FIFO functionality
|
2012-04-25 06:43:01 +02:00
|
|
|
/// is needed.
|
|
|
|
{
|
|
|
|
public:
|
2012-04-29 04:37:54 +02:00
|
|
|
typedef T Type;
|
|
|
|
|
2012-04-29 04:03:09 +02:00
|
|
|
mutable Poco::BasicEvent<bool> Writable;
|
2012-04-27 06:41:50 +02:00
|
|
|
/// Event indicating "writeability" of the buffer,
|
|
|
|
/// triggerred as follows:
|
|
|
|
///
|
|
|
|
/// * when buffer transitions from non-full to full,
|
2012-04-29 04:03:09 +02:00
|
|
|
/// Writable event observers are notified, with
|
2012-04-27 06:41:50 +02:00
|
|
|
/// false value as the argument
|
|
|
|
///
|
|
|
|
/// * when buffer transitions from full to non-full,
|
2012-04-29 04:03:09 +02:00
|
|
|
/// Writable event observers are notified, with
|
2012-04-27 06:41:50 +02:00
|
|
|
/// true value as the argument
|
|
|
|
|
|
|
|
mutable Poco::BasicEvent<bool> Readable;
|
|
|
|
/// Event indicating "readability" of the buffer,
|
|
|
|
/// triggerred as follows:
|
|
|
|
///
|
|
|
|
/// * when buffer transitions from non-empty to empty,
|
|
|
|
/// Readable event observers are notified, with false
|
|
|
|
/// value as the argument
|
|
|
|
///
|
|
|
|
/// * when FIFOBuffer transitions from empty to non-empty,
|
|
|
|
/// Readable event observers are notified, with true value
|
|
|
|
/// as the argument
|
|
|
|
|
2012-04-29 04:37:54 +02:00
|
|
|
BasicFIFOBuffer(std::size_t size, bool notify = false):
|
2012-04-25 06:43:01 +02:00
|
|
|
_buffer(size),
|
2012-04-28 06:32:35 +02:00
|
|
|
_begin(0),
|
2012-04-27 06:41:50 +02:00
|
|
|
_used(0),
|
|
|
|
_notify(notify)
|
2012-04-25 06:43:01 +02:00
|
|
|
/// Creates and allocates the FIFOBuffer.
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 04:37:54 +02:00
|
|
|
~BasicFIFOBuffer()
|
2012-04-25 06:43:01 +02:00
|
|
|
/// 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.");
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
std::size_t usedBefore = _used;
|
2012-04-25 06:43:01 +02:00
|
|
|
_buffer.resize(newSize, preserveContent);
|
2012-04-26 02:08:53 +02:00
|
|
|
if (!preserveContent) _used = 0;
|
2012-04-27 06:41:50 +02:00
|
|
|
if (_notify) notify(usedBefore);
|
2012-04-25 06:43:01 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
std::size_t peek(Poco::Buffer<T>& buffer, std::size_t length = 0) const
|
2012-04-25 06:43:01 +02:00
|
|
|
/// Peeks into the data currently in the FIFO
|
|
|
|
/// without actually extracting it.
|
|
|
|
/// Resizes the supplied buffer to the size of
|
2012-04-28 06:32:35 +02:00
|
|
|
/// data written to it. If length is not
|
2012-04-25 06:43:01 +02:00
|
|
|
/// 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);
|
2012-04-28 06:32:35 +02:00
|
|
|
std::memcpy(buffer.begin(), _buffer.begin() + _begin, length * sizeof(T));
|
2012-04-26 02:08:53 +02:00
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
return length;
|
2012-04-25 06:43:01 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
std::size_t read(Poco::Buffer<T>& buffer, std::size_t length = 0)
|
2012-04-25 06:43:01 +02:00
|
|
|
/// Copies the data currently in the FIFO
|
|
|
|
/// into the supplied buffer.
|
|
|
|
/// Resizes the supplied buffer to the size of
|
2012-04-28 06:32:35 +02:00
|
|
|
/// data written to it.
|
2012-04-25 06:43:01 +02:00
|
|
|
///
|
|
|
|
/// Returns the reference to the buffer.
|
|
|
|
{
|
|
|
|
Mutex::ScopedLock lock(_mutex);
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
if (0 == _used) return 0;
|
|
|
|
|
2012-04-25 06:43:01 +02:00
|
|
|
std::size_t usedBefore = _used;
|
2012-04-27 06:41:50 +02:00
|
|
|
std::size_t readLen = peek(buffer, length);
|
|
|
|
poco_assert (_used >= readLen);
|
|
|
|
_used -= readLen;
|
2012-04-28 06:32:35 +02:00
|
|
|
if (0 == _used) _begin = 0;
|
|
|
|
else _begin += length;
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
if (_notify) notify(usedBefore);
|
2012-04-25 06:43:01 +02:00
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
return readLen;
|
2012-04-25 06:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-04-26 02:08:53 +02:00
|
|
|
|
2012-04-28 06:32:35 +02:00
|
|
|
if (_buffer.size() - (_begin + _used) < buffer.size())
|
|
|
|
{
|
|
|
|
std::memmove(_buffer.begin(), _buffer.begin() + _begin, _used);
|
|
|
|
_begin = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-27 06:41:50 +02:00
|
|
|
std::size_t usedBefore = _used;
|
2012-04-28 06:32:35 +02:00
|
|
|
std::size_t available = _buffer.size() - _used - _begin;
|
2012-04-25 06:43:01 +02:00
|
|
|
std::size_t len = buffer.size() > available ? available : buffer.size();
|
2012-04-28 06:32:35 +02:00
|
|
|
std::memcpy(_buffer.begin() + _begin + _used, buffer.begin(), len * sizeof(T));
|
2012-04-25 06:43:01 +02:00
|
|
|
_used += len;
|
|
|
|
poco_assert (_used <= _buffer.size());
|
2012-04-27 06:41:50 +02:00
|
|
|
if (_notify) notify(usedBefore);
|
2012-04-26 02:08:53 +02:00
|
|
|
|
2012-04-25 06:43:01 +02:00
|
|
|
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));
|
|
|
|
|
2012-04-28 06:32:35 +02:00
|
|
|
return _buffer[_begin + index];
|
2012-04-25 06:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2012-04-28 06:32:35 +02:00
|
|
|
return _buffer[_begin + index];
|
2012-04-25 06:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isEmpty() const
|
|
|
|
/// Returns true is buffer is empty, flase otherwise.
|
|
|
|
{
|
|
|
|
return 0 == _used;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-04-27 06:41:50 +02:00
|
|
|
void notify(std::size_t usedBefore)
|
|
|
|
{
|
|
|
|
bool t = true, f = false;
|
|
|
|
if (usedBefore == 0 && _used > 0)
|
|
|
|
Readable.notify(this, t);
|
|
|
|
else if (usedBefore > 0 && 0 == _used)
|
|
|
|
Readable.notify(this, f);
|
2012-04-29 03:51:30 +02:00
|
|
|
|
|
|
|
if (usedBefore == _buffer.size() && _used < _buffer.size())
|
2012-04-29 04:03:09 +02:00
|
|
|
Writable.notify(this, t);
|
2012-04-27 06:41:50 +02:00
|
|
|
else if (usedBefore < _buffer.size() && _used == _buffer.size())
|
2012-04-29 04:03:09 +02:00
|
|
|
Writable.notify(this, f);
|
2012-04-27 06:41:50 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 04:37:54 +02:00
|
|
|
BasicFIFOBuffer();
|
|
|
|
BasicFIFOBuffer(const BasicFIFOBuffer&);
|
|
|
|
BasicFIFOBuffer& operator = (const BasicFIFOBuffer&);
|
2012-04-25 06:43:01 +02:00
|
|
|
|
|
|
|
Buffer<T> _buffer;
|
2012-04-28 06:32:35 +02:00
|
|
|
std::size_t _begin;
|
2012-04-25 06:43:01 +02:00
|
|
|
std::size_t _used;
|
2012-04-27 06:41:50 +02:00
|
|
|
bool _notify;
|
2012-04-25 06:43:01 +02:00
|
|
|
mutable Mutex _mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-29 04:37:54 +02:00
|
|
|
//
|
|
|
|
// We provide an instantiation for char
|
|
|
|
//
|
|
|
|
typedef BasicFIFOBuffer<char> FIFOBuffer;
|
|
|
|
|
|
|
|
|
2012-04-25 06:43:01 +02:00
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_FIFOBuffer_INCLUDED
|