BufferLocked.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef ORO_CORELIB_BUFFER_LOCKED_HPP
00043 #define ORO_CORELIB_BUFFER_LOCKED_HPP
00044
00045 #include "os/Mutex.hpp"
00046 #include "os/MutexLock.hpp"
00047 #include "BufferInterface.hpp"
00048 #include "BufferPolicy.hpp"
00049 #include <deque>
00050
00051 namespace RTT
00052 {
00053
00060 template<class T, class ReadPolicy = NonBlockingPolicy, class WritePolicy = NonBlockingPolicy>
00061 class BufferLocked
00062 :public BufferInterface<T>
00063 {
00064 public:
00065
00066 typedef typename ReadInterface<T>::reference_t reference_t;
00067 typedef typename WriteInterface<T>::param_t param_t;
00068 typedef typename BufferInterface<T>::size_type size_type;
00069 typedef T value_t;
00070
00074 BufferLocked( size_type size, const T& initial_value = T() )
00075 : cap(size), buf(), write_policy(size), read_policy(0)
00076 {
00077 buf.resize(size, initial_value);
00078 buf.resize(0);
00079 }
00080
00084 ~BufferLocked() {}
00085
00086 bool Push( param_t item )
00087 {
00088 write_policy.pop();
00089 OS::MutexLock locker(lock);
00090 if (cap == (size_type)buf.size() ) {
00091 write_policy.push();
00092 return false;
00093 }
00094 buf.push_back( item );
00095 read_policy.push();
00096 return true;
00097 }
00098
00099 size_type Push(const std::vector<T>& items)
00100 {
00101 write_policy.pop( items.size() );
00102 OS::MutexLock locker(lock);
00103 typename std::vector<T>::const_iterator itl( items.begin() );
00104 while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
00105 buf.push_back( *itl );
00106 ++itl;
00107 read_policy.push();
00108 }
00109 write_policy.push( itl - items.begin() );
00110 return (itl - items.begin());
00111
00112 }
00113 bool Pop( reference_t item )
00114 {
00115 read_policy.pop();
00116 OS::MutexLock locker(lock);
00117 if ( buf.empty() ) {
00118 read_policy.push();
00119 return false;
00120 }
00121 item = buf.front();
00122 buf.pop_front();
00123 write_policy.push();
00124 return true;
00125 }
00126
00127 size_type Pop(std::vector<T>& items )
00128 {
00129 OS::MutexLock locker(lock);
00130 int quant = 0;
00131 while ( !buf.empty() ) {
00132 items.push_back( buf.front() );
00133 buf.pop_front();
00134 ++quant;
00135 read_policy.pop();
00136 write_policy.push();
00137 }
00138 return quant;
00139 }
00140
00141 value_t front() const
00142 {
00143 OS::MutexLock locker(lock);
00144 value_t item = value_t();
00145 if ( !buf.empty() )
00146 item = buf.front();
00147 return item;
00148 }
00149
00150 size_type capacity() const {
00151 OS::MutexLock locker(lock);
00152 return cap;
00153 }
00154
00155 size_type size() const {
00156 OS::MutexLock locker(lock);
00157 return buf.size();
00158 }
00159
00160 void clear() {
00161 OS::MutexLock locker(lock);
00162 buf.clear();
00163 }
00164
00165 bool empty() const {
00166 OS::MutexLock locker(lock);
00167 return buf.empty();
00168 }
00169
00170 bool full() const {
00171 OS::MutexLock locker(lock);
00172 return (size_type)buf.size() == cap;
00173 }
00174 private:
00175 size_type cap;
00176 std::deque<T> buf;
00177 mutable OS::Mutex lock;
00178 WritePolicy write_policy;
00179 ReadPolicy read_policy;
00180 };
00181 }
00182
00183 #endif // BUFFERSIMPLE_HPP