LockedQueue.hpp

00001 #ifndef ORO_LOCKED_QUEUE_HPP
00002 #define ORO_LOCKED_QUEUE_HPP
00003 
00004 #include <deque>
00005 #include "BufferPolicy.hpp"
00006 #include "os/Mutex.hpp"
00007 #include "os/MutexLock.hpp"
00008 
00009 namespace RTT
00010 {
00025     template< class T, class ReadPolicy = NonBlockingPolicy, class WritePolicy = NonBlockingPolicy>
00026     class LockedQueue
00027     {
00028     public:
00029         typedef T value_t;
00030     private:
00031         typedef std::deque<value_t> BufferType;
00032         typedef typename BufferType::iterator Iterator;
00033         typedef typename BufferType::const_iterator CIterator;
00034         mutable OS::Mutex lock;
00035         BufferType data;
00036 
00037         int cap;
00038         WritePolicy write_policy;
00039         ReadPolicy read_policy;
00040 
00041         int counter;
00042         int dcounter;
00043     public:
00044         typedef unsigned int size_type;
00045 
00050         LockedQueue(unsigned int lsize, unsigned int unused = 0)
00051             : cap(lsize),
00052               write_policy(lsize), read_policy(0),
00053               counter(0), dcounter(0)
00054         {
00055             data.resize(lsize);
00056             data.resize(0);
00057         }
00058 
00059         ~LockedQueue() {
00060         }
00061 
00062         size_type capacity() const
00063         {
00064             return cap;
00065         }
00066 
00067         size_type size() const
00068         {
00069             OS::MutexLock locker(lock);
00070             return data.size();
00071         }
00072 
00077         bool isEmpty() const
00078         {
00079             OS::MutexLock locker(lock);
00080             return data.empty();
00081         }
00082 
00087         bool isFull() const
00088         {
00089             OS::MutexLock locker(lock);
00090             return data.size() ==  cap;
00091         }
00092 
00093         void clear()
00094         {
00095             OS::MutexLock locker(lock);
00096             data.clear();
00097         }
00098 
00104         bool enqueue(const T& value)
00105         {
00106             write_policy.pop();
00107             {
00108                 OS::MutexLock locker(lock);
00109                 if (cap == data.size() )
00110                     return false;
00111                 data.push_back(value);
00112             }
00113             read_policy.push();
00114             return true;
00115         }
00116 
00123         int enqueueCounted(const T& value)
00124         {
00125             int ret;
00126             write_policy.pop();
00127             {
00128                 OS::MutexLock locker(lock);
00129                 if (cap == data.size() )
00130                     return 0;
00131                 data.push_back(value);
00132                 ret = ++counter;
00133             }
00134             read_policy.push();
00135             return ret;
00136         }
00137 
00143         bool dequeue( T& result )
00144         {
00145             read_policy.pop();
00146             {
00147                 OS::MutexLock locker(lock);
00148                 if ( data.empty() )
00149                     return false;
00150                 result = data.front();
00151                 data.pop_front();
00152             }
00153             write_policy.push();
00154             return true;
00155         }
00156 
00163         int dequeueCounted( T& result )
00164         {
00165             int ret;
00166             read_policy.pop();
00167             {
00168                 OS::MutexLock locker(lock);
00169                 if ( data.empty() )
00170                     return 0;
00171                 result = data.front();
00172                 data.pop_front();
00173                 ret = ++dcounter;
00174             }
00175             write_policy.push();
00176             return ret;
00177         }
00178 
00182         value_t front() const
00183         {
00184             OS::MutexLock locker(lock);
00185             value_t item = value_t();
00186             if ( !data.empty() )
00187                 item = data.front();
00188             return item;
00189         }
00190 
00194         value_t back() const
00195         {
00196             OS::MutexLock locker(lock);
00197             value_t item = value_t();
00198             if ( !data.empty() )
00199                 item = data.back();
00200             return item;
00201         }
00202 
00203     };
00204 
00205 }
00206 
00207 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3