OpenRTM-aist  1.2.0
RingBuffer.h
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifndef RTC_RINGBUFFER_H
21 #define RTC_RINGBUFFER_H
22 
23 #include <vector>
24 #include <algorithm>
25 #include <iostream>
26 
27 #include <coil/TimeValue.h>
28 #include <coil/Mutex.h>
29 #include <coil/Guard.h>
30 #include <coil/Condition.h>
31 #include <coil/stringutil.h>
32 
33 #include <rtm/BufferBase.h>
34 #include <rtm/BufferStatus.h>
35 
36 #define RINGBUFFER_DEFAULT_LENGTH 8
37 
51 namespace RTC
52 {
88  template <class DataType>
89  class RingBuffer
90  : public BufferBase<DataType>
91  {
92  public:
119  : m_overwrite(true), m_readback(true),
120  m_timedwrite(false), m_timedread(false),
121  m_wtimeout(1, 0), m_rtimeout(1, 0),
122  m_length(length),
123  m_wpos(0), m_rpos(0), m_fillcount(0), m_wcount(0),
124  m_buffer(m_length)
125  {
126  this->reset();
127  }
128 
144  virtual ~RingBuffer(void)
145  {
146  }
147 
187  virtual void init(const coil::Properties& prop)
188  {
189  initLength(prop);
190  initWritePolicy(prop);
191  initReadPolicy(prop);
192  }
193 
214  virtual size_t length(void) const
215  {
216  Guard guard(m_posmutex);
217  return m_length;
218  }
219 
242  virtual ReturnCode length(size_t n)
243  {
244  m_buffer.resize(n);
245  m_length = n;
246  this->reset();
247  return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
248  }
249 
272  virtual ReturnCode reset()
273  {
274  Guard guard(m_posmutex);
275  m_fillcount = 0;
276  m_wcount = 0;
277  m_wpos = 0;
278  m_rpos = 0;
279  return ::RTC::BufferStatus::BUFFER_OK;
280  }
281 
282 
283 
284  //----------------------------------------------------------------------
305  virtual DataType* wptr(long int n = 0)
306  {
307  Guard guard(m_posmutex);
308  return &m_buffer[(m_wpos + n + m_length) % m_length];
309  }
310 
335  virtual ReturnCode advanceWptr(long int n = 1, bool unlock_enable = true)
336  {
337  bool empty_ = false;
338  if(unlock_enable && n > 0)
339  {
340  Guard fguard(m_empty.mutex);
341  empty_ = empty();
342  }
343  // n > 0 :
344  // n satisfies n <= writable elements
345  // n <= m_length - m_fillcout
346  // n < 0 : -n = n'
347  // n satisfies n'<= readable elements
348  // n'<= m_fillcount
349  // n >= - m_fillcount
350  {
351  Guard guard(m_posmutex);
352  if ((n > 0 && n > static_cast<long int>(m_length - m_fillcount)) ||
353  (n < 0 && n < static_cast<long int>(-m_fillcount)))
354  {
355  return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
356  }
357 
358  m_wpos = (m_wpos + n + m_length) % m_length;
359  m_fillcount += n;
360  m_wcount += n;
361  }
362 
363  if(unlock_enable)
364  {
365  if(empty_)
366  {
367  Guard fguard(m_empty.mutex);
368  m_empty.cond.signal();
369  }
370  }
371 
372  return ::RTC::BufferStatus::BUFFER_OK;
373  }
401  virtual ReturnCode put(const DataType& value)
402  {
403  Guard guard(m_posmutex);
404  m_buffer[m_wpos] = value;
405  return ::RTC::BufferStatus::BUFFER_OK;
406  }
407 
449  virtual ReturnCode write(const DataType& value,
450  long int sec = -1, long int nsec = 0)
451  {
452  {
453  Guard guard(m_full.mutex);
454 
455  if (full())
456  {
457 
458  bool timedwrite(m_timedwrite);
459  bool overwrite(m_overwrite);
460 
461  if (!(sec < 0)) // if second arg is set -> block mode
462  {
463  timedwrite = true;
464  overwrite = false;
465  }
466 
467  if (overwrite && !timedwrite) // "overwrite" mode
468  {
469  advanceRptr(1,false);
470  }
471  else if (!overwrite && !timedwrite) // "do_nothing" mode
472  {
473  return ::RTC::BufferStatus::BUFFER_FULL;
474  }
475  else if (!overwrite && timedwrite) // "block" mode
476  {
477  if (sec < 0)
478  {
479  sec = m_wtimeout.sec();
480  nsec = m_wtimeout.usec() * 1000;
481  }
482  // true: signaled, false: timeout
483  if (!m_full.cond.wait(sec, nsec))
484  {
485  return ::RTC::BufferStatus::TIMEOUT;
486  }
487  }
488  else // unknown condition
489  {
490  return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
491  }
492  }
493  }
494 
495  put(value);
496 
497  advanceWptr(1);
498 
499 
500  return ::RTC::BufferStatus::BUFFER_OK;
501  }
502 
524  virtual size_t writable() const
525  {
526  Guard guard(m_posmutex);
527  return m_length - m_fillcount;
528  }
529 
549  virtual bool full(void) const
550  {
551  Guard guard(m_posmutex);
552  return m_length == m_fillcount;
553  }
554 
555  //----------------------------------------------------------------------
576  virtual DataType* rptr(long int n = 0)
577  {
578  Guard guard(m_posmutex);
579  return &(m_buffer[(m_rpos + n + m_length) % m_length]);
580  }
581 
604  virtual ReturnCode advanceRptr(long int n = 1, bool unlock_enable = true)
605  {
606  bool full_ = false;
607  if(unlock_enable && n > 0)
608  {
609  Guard fguard(m_full.mutex);
610  full_ = full();
611  }
612  // n > 0 :
613  // n satisfies n <= readable elements
614  // n <= m_fillcout
615  // n < 0 : -n = n'
616  // n satisfies n'<= m_length - m_fillcount
617  // n >= m_fillcount - m_length
618  {
619  Guard guard(m_posmutex);
620  if ((n > 0 && n > static_cast<long int>(m_fillcount)) ||
621  (n < 0 && n < static_cast<long int>(m_fillcount - m_length)))
622  {
623  return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
624  }
625 
626  m_rpos = (m_rpos + n + m_length) % m_length;
627  m_fillcount -= n;
628  }
629 
630  if(unlock_enable)
631  {
632  if(full_)
633  {
634  Guard fguard(m_full.mutex);
635  m_full.cond.signal();
636  }
637  }
638 
639  return ::RTC::BufferStatus::BUFFER_OK;
640  }
641 
666  virtual ReturnCode get(DataType& value)
667  {
668  Guard gaurd(m_posmutex);
669  value = m_buffer[m_rpos];
670  return ::RTC::BufferStatus::BUFFER_OK;
671  }
672 
673 
691  virtual DataType& get()
692  {
693  Guard gaurd(m_posmutex);
694  return m_buffer[m_rpos];
695  }
696 
697 
739  virtual ReturnCode read(DataType& value,
740  long int sec = -1, long int nsec = 0)
741  {
742  {
743  Guard gaurd(m_empty.mutex);
744 
745  if (empty())
746  {
747  bool timedread(m_timedread);
748  bool readback(m_readback);
749 
750  if (!(sec < 0)) // if second arg is set -> block mode
751  {
752  timedread = true;
753  readback = false;
754  sec = m_rtimeout.sec();
755  nsec = m_rtimeout.usec() * 1000;
756  }
757 
758  if (readback && !timedread) // "readback" mode
759  {
760  if (!(m_wcount > 0))
761  {
762  return ::RTC::BufferStatus::BUFFER_EMPTY;
763  }
764  advanceRptr(-1,false);
765  }
766  else if (!readback && !timedread) // "do_nothing" mode
767  {
768  return ::RTC::BufferStatus::BUFFER_EMPTY;
769  }
770  else if (!readback && timedread) // "block" mode
771  {
772  if (sec < 0)
773  {
774  sec = m_rtimeout.sec();
775  nsec = m_rtimeout.usec() * 1000;
776  }
777  // true: signaled, false: timeout
778  if (!m_empty.cond.wait(sec, nsec))
779  {
780  return ::RTC::BufferStatus::TIMEOUT;
781  }
782  }
783  else // unknown condition
784  {
785  return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
786  }
787  }
788  }
789 
790  get(value);
791 
792  advanceRptr(1);
793 
794  return ::RTC::BufferStatus::BUFFER_OK;
795  }
796 
821  virtual size_t readable() const
822  {
823  Guard guard(m_posmutex);
824  return m_fillcount;
825  }
826 
846  virtual bool empty(void) const
847  {
848  Guard guard(m_posmutex);
849  return m_fillcount == 0;
850  }
851 
852  private:
853  inline void initLength(const coil::Properties& prop)
854  {
855  if (!prop["length"].empty())
856  {
857  size_t n;
858  if (coil::stringTo(n, prop["length"].c_str()))
859  {
860  if (n > 0)
861  {
862  this->length(n);
863  }
864  }
865  }
866  }
867 
868  inline void initWritePolicy(const coil::Properties& prop)
869  {
870  std::string policy(prop["write.full_policy"]);
871  coil::normalize(policy);
872  if (policy == "overwrite")
873  {
874  m_overwrite = true;
875  m_timedwrite = false;
876  }
877  else if (policy == "do_nothing")
878  {
879  m_overwrite = false;
880  m_timedwrite = false;
881  }
882  else if (policy == "block")
883  {
884  m_overwrite = false;
885  m_timedwrite = true;
886 
887  double tm;
888  if (coil::stringTo(tm, prop["write.timeout"].c_str()))
889  {
890  if (!(tm < 0))
891  {
892  m_wtimeout = tm;
893  }
894  }
895  }
896  }
897 
898  inline void initReadPolicy(const coil::Properties& prop)
899  {
900  std::string policy(prop["read.empty_policy"]);
901  if (policy == "readback")
902  {
903  m_readback = true;
904  m_timedread = false;
905  }
906  else if (policy == "do_nothing")
907  {
908  m_readback = false;
909  m_timedread = false;
910  }
911  else if (policy == "block")
912  {
913  m_readback = false;
914  m_timedread = true;
915  double tm;
916  if (coil::stringTo(tm, prop["read.timeout"].c_str()))
917  {
918  m_rtimeout = tm;
919  }
920  }
921  }
922 
923  private:
931  bool m_overwrite;
932 
940  bool m_readback;
941 
949  bool m_timedwrite;
957  bool m_timedread;
958 
966  coil::TimeValue m_wtimeout;
967 
975  coil::TimeValue m_rtimeout;
976 
984  size_t m_length;
985 
993  size_t m_wpos;
994 
1002  size_t m_rpos;
1003 
1011  size_t m_fillcount;
1012 
1020  size_t m_wcount;
1021 
1029  std::vector<DataType> m_buffer;
1030 
1038  struct condition
1039  {
1040  condition() : cond(mutex) {}
1042  coil::Mutex mutex;
1043  };
1044 
1052  mutable coil::Mutex m_posmutex;
1053 
1061  condition m_empty;
1062 
1070  condition m_full;
1071  };
1072 }; // namespace RTC
1073 
1074 #endif // RTC_RINGBUFFER_H
std::string normalize(std::string &str)
Erase the head/tail blank and replace upper case to lower case.
RT-Component.
virtual DataType * rptr(long int n=0)
Get the buffer length.
Definition: RingBuffer.h:576
virtual ~RingBuffer(void)
Virtual destractor.
Definition: RingBuffer.h:144
virtual ReturnCode read(DataType &value, long int sec=-1, long int nsec=0)
Readout data from the buffer.
Definition: RingBuffer.h:739
bool stringTo(To &val, const char *str)
Convert the given std::string to object.
Definition: stringutil.h:633
virtual ReturnCode length(size_t n)
Get the buffer length.
Definition: RingBuffer.h:242
Mutex class.
Definition: Mutex.h:40
virtual bool full(void) const
Check on whether the buffer is full.
Definition: RingBuffer.h:549
#define RINGBUFFER_DEFAULT_LENGTH
Definition: RingBuffer.h:36
virtual void init(const coil::Properties &prop)
Set the buffer.
Definition: RingBuffer.h:187
virtual DataType * wptr(long int n=0)
Get the buffer length.
Definition: RingBuffer.h:305
TimeValue class.
Definition: TimeValue.h:40
virtual ReturnCode reset()
Get the buffer length.
Definition: RingBuffer.h:272
RingBuffer(long int length=RINGBUFFER_DEFAULT_LENGTH)
Constructor.
Definition: RingBuffer.h:118
virtual ReturnCode advanceRptr(long int n=1, bool unlock_enable=true)
Get the buffer length.
Definition: RingBuffer.h:604
#define BUFFERSTATUS_ENUM
Importing RTC::BufferStatus macro.
Definition: BufferStatus.h:157
BUFFERSTATUS_ENUM typedef coil::Guard< coil::Mutex > Guard
Definition: RingBuffer.h:94
Buffer status enum definition.
virtual bool empty(void) const
Check on whether the buffer is empty.
Definition: RingBuffer.h:846
virtual size_t writable() const
Write data into the buffer.
Definition: RingBuffer.h:524
Guard template class.
virtual size_t readable() const
Write data into the buffer.
Definition: RingBuffer.h:821
Ring buffer implementation class.
Definition: RingBuffer.h:89
Buffer abstract class.
virtual ReturnCode write(const DataType &value, long int sec=-1, long int nsec=0)
Write data into the buffer.
Definition: RingBuffer.h:449
Class represents a set of properties.
Definition: Properties.h:101
virtual ReturnCode advanceWptr(long int n=1, bool unlock_enable=true)
Get the buffer length.
Definition: RingBuffer.h:335
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
virtual ReturnCode put(const DataType &value)
Write data into the buffer.
Definition: RingBuffer.h:401
BufferBase abstract class.
Definition: BufferBase.h:104
virtual size_t length(void) const
Get the buffer length.
Definition: RingBuffer.h:214