42 #ifndef ORO_CORELIB_BUFFER_LOCKED_HPP 43 #define ORO_CORELIB_BUFFER_LOCKED_HPP 45 #include "../os/Mutex.hpp" 46 #include "../os/MutexLock.hpp" 78 : cap(size), buf(), mcircular(circular), droppedSamples(0)
85 buf.resize(cap, sample);
103 if ( cap == (size_type)buf.size() ) {
113 buf.push_back( item );
117 size_type
Push(
const std::vector<T>& items)
120 typename std::vector<T>::const_iterator itl( items.begin() );
121 if (mcircular && (size_type)items.size() >= cap ) {
125 droppedSamples += cap;
126 itl = items.begin() + ( items.size() - cap );
127 }
else if ( mcircular && (size_type)(buf.size() + items.size()) > cap) {
129 assert( (size_type)items.size() < cap );
130 while ( (size_type)(buf.size() + items.size()) > cap )
137 while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
138 buf.push_back( *itl );
142 size_type writtenSamples = itl - items.begin();
145 assert( writtenSamples == (size_type)items.size() );
147 droppedSamples += items.size() - writtenSamples;
149 return writtenSamples;
151 bool Pop( reference_t item )
162 size_type
Pop(std::vector<T>& items )
167 while ( !buf.empty() ) {
168 items.push_back( buf.front() );
184 lastSample = buf.front();
193 assert(item == &lastSample &&
"Wrong pointer given back to buffer");
218 return (size_type)buf.size() == cap;
223 return droppedSamples;
230 const bool mcircular;
231 size_type droppedSamples;
235 #endif // BUFFERSIMPLE_HPP virtual T data_sample() const
Reads back a data sample.
boost::call_traits< T >::reference reference_t
BufferLocked(size_type size, const T &initial_value=T(), bool circular=false)
Create a buffer of size size, with preallocated data storage.
A Buffer is an object which is used to store (Push) and retrieve (Pop) values from.
virtual void data_sample(const T &sample)
Initializes this buffer with a data sample, such that for dynamical allocated types T...
boost::call_traits< T >::param_type param_t
size_type dropped() const
Returns the number of dropped samples, because the buffer was full.
void Release(value_t *item)
Releases the pointer.
value_t * PopWithoutRelease()
Returns a pointer to the first element in the buffer.
bool Push(param_t item)
Write a single value to the buffer.
size_type Push(const std::vector< T > &items)
Write a sequence of values to the buffer.
bool empty() const
Check if this buffer is empty.
BufferBase::size_type size_type
BufferInterface< T >::size_type size_type
BufferInterface< T >::param_t param_t
bool full() const
Check if this buffer is full.
BufferInterface< T >::reference_t reference_t
size_type size() const
Returns the actual number of items that are stored in the buffer.
Implements a very simple blocking thread-safe buffer, using mutexes (locks).
~BufferLocked()
Destructor.
void clear()
Clears all contents of this buffer.
size_type capacity() const
Returns the maximum number of items that can be stored in the buffer.
bool Pop(reference_t item)
Read the oldest value from the buffer.
An object oriented wrapper around a non recursive mutex.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
size_type Pop(std::vector< T > &items)
Read the whole buffer.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...