40 #ifndef ORO_LIST_LOCKED_HPP 41 #define ORO_LIST_LOCKED_HPP 43 #include <boost/intrusive/list.hpp> 44 #include <boost/bind.hpp> 45 #include <boost/bind/protect.hpp> 49 #include "../os/Mutex.hpp" 50 #include "../os/MutexLock.hpp" 52 #ifdef ORO_PRAGMA_INTERFACE 78 struct Cont :
public boost::intrusive::list_base_hook<> {
81 typedef boost::intrusive::list<Cont> BufferType;
82 typedef std::stack<Cont*> StackType;
83 typedef typename BufferType::iterator Iterator;
84 typedef typename BufferType::const_iterator CIterator;
88 unsigned int required;
99 for(
unsigned int i=0; i <lsize; ++i)
100 mreserved.push(
new Cont());
105 while( !mreserved.empty() ) {
106 delete mreserved.top();
114 return mreserved.size() + mlist.size();
126 return mlist.empty();
139 if (required > mreserved.size() + mlist.size() ) {
140 while ( mreserved.size() + mlist.size() < required * 2) {
141 mreserved.push(
new Cont() );
166 while ( mreserved.size() + mlist.size() < lsize) {
167 mreserved.push(
new Cont() );
174 mlist.clear_and_dispose( boost::bind(&ListLocked::give_back,
this, _1) );
185 if ( mreserved.empty() )
187 mlist.push_back( this->get_item(item) );
197 return mlist.front().data;
206 return mlist.back().data;
214 size_t append(
const std::vector<T>& items)
217 unsigned int max = mreserved.size();
218 typename std::vector<T>::const_iterator it = items.begin();
219 for(; it != items.end() && max != 0; ++it, --max )
220 mlist.push_back( this->get_item(*it) );
221 return it - items.begin();
233 mlist.remove_and_dispose_if( boost::bind(&ListLocked::is_item,
this, item, _1), boost::bind(&ListLocked::give_back,
this, _1) );
244 template<
typename Pred>
248 bool deleted =
false;
250 typename BufferType::iterator cur(mlist.begin());
251 typename BufferType::iterator last(mlist.end());
257 cur = mlist.erase_and_dispose(cur, boost::bind(&ListLocked::give_back,
this, _1) );
273 template<
class Function>
278 for (Iterator it = mlist.begin(); it != mlist.end(); ++it)
287 template<
class Function>
291 Iterator it = std::find_if(mlist.begin(), mlist.end(), boost::bind(func, boost::bind(&ListLocked::get_data,
this,_1)));
292 if (it != mlist.end() )
302 void give_back(Cont* cont)
304 mreserved.push( cont );
307 Cont& get_item(value_t item)
309 Cont* c = mreserved.top();
315 value_t& get_data(Cont& c)
327 bool is_item(value_t item,
const Cont& cont)
329 return item == cont.data;
value_t back() const
Returns the last element of the list.
bool erase(value_t item)
Erase a value from the list.
void reserve(size_t lsize)
Reserve a capacity for this list.
value_t front() const
Returns the first element of the list.
ListLocked(unsigned int lsize, unsigned int unused=0)
Create a lock-based list wich can store lsize elements.
void apply(Function func)
Apply a function to the elements of the whole list.
size_t append(const std::vector< T > &items)
Append a sequence of values to the list.
A simple lock-based list implementation to append or erase data of type T.
void grow(size_t items=1)
Grow the capacity to contain at least n additional items.
value_t find_if(Function func, value_t blank=value_t())
Find an item in the list such that func( item ) == true.
bool delete_if(Pred pred)
Erase a value from the list.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
bool append(value_t item)
Append a single value to the list.
An object oriented wrapper around a recursive mutex.
void shrink(size_t items=1)
Shrink the capacity with at most n items.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...