00001 #ifndef __INSTANTIO_BUFFEREDINSLOT_H
00002 #define __INSTANTIO_BUFFEREDINSLOT_H
00003
00004 #ifdef _MSC_VER
00005 # pragma once
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "InstantIODef.h"
00027 #include "InSlot.h"
00028 #include "Data.h"
00029 #include "InterruptedException.h"
00030 #include <string>
00031 #include <vector>
00032
00033
00034 namespace InstantIO
00035 {
00036
00037
00058 template <class T> class BufferedInSlot: public InSlot<T>
00059 {
00060 public:
00061
00067 inline explicit BufferedInSlot(unsigned int numValues = 1)
00068 : InSlot<T>(), buffer_(numValues), readPos_(buffer_.begin()), writePos_(buffer_.begin()), numValues_(0)
00069 {}
00070
00080 inline explicit BufferedInSlot(const std::string &description, unsigned int numValues = 1)
00081 : InSlot<T>(description), buffer_(numValues),
00082 readPos_(buffer_.begin()), writePos_(buffer_.begin()), numValues_(0)
00083 {}
00084
00088 virtual ~BufferedInSlot() {};
00089
00098 virtual bool empty() const
00099 {
00100 this->lock();
00101 bool result = numValues_ == 0;
00102 this->unlock();
00103 return result;
00104 };
00105
00114 inline unsigned int getNumValues() const
00115 {
00116 this->lock();
00117 unsigned int result = numValues_;
00118 this->unlock();
00119 return result;
00120 }
00121
00133 virtual const Data<T> top()
00134
00135 {
00136 this->lock();
00137 if (this->interrupted_ == true)
00138 {
00139 this->interrupted_ = false;
00140 this->unlock();
00141 throw InterruptedException();
00142 }
00143 Data<T> value = *readPos_;
00144 this->unlock();
00145 return value;
00146 };
00147
00167 virtual const Data<T> pop()
00168
00169 {
00170 this->down();
00171 this->lock();
00172 if (this->interrupted_ == true)
00173 {
00174 this->interrupted_ = false;
00175 this->unlock();
00176 throw InterruptedException();
00177 }
00178 Data<T> value = *readPos_;
00179 incrementPos(readPos_);
00180 --numValues_;
00181 this->unlock();
00182 return value;
00183 };
00184
00195 virtual void push(const Data<T> &data)
00196 {
00197 this->lock();
00198 if ((writePos_ == readPos_) && (numValues_ > 0))
00199 incrementPos(readPos_);
00200 else
00201 {
00202 ++numValues_;
00203 this->up();
00204 }
00205 *writePos_ = data;
00206 incrementPos(writePos_);
00207 this->unlock();
00208 this->newData();
00209 };
00210
00211 private:
00212
00214 typedef std::vector<Data<T> > DataVector;
00215
00217
00218 DataVector buffer_;
00219
00221
00222 typename DataVector::iterator readPos_;
00223
00225
00226 typename DataVector::iterator writePos_;
00227
00229
00230 unsigned int numValues_;
00231
00238 inline void incrementPos(typename DataVector::iterator &pos)
00239 {
00240
00241 if (++pos == buffer_.end())
00242 pos = buffer_.begin();
00243 }
00244
00246 BufferedInSlot(const BufferedInSlot<T> &);
00247
00249 const BufferedInSlot<T> &operator=(const BufferedInSlot<T> &);
00250 };
00251
00252
00253 }
00254
00255
00256 #endif //__INSTANTIO_BUFFEREDINSLOT_H