20 #ifndef RTC_RINGBUFFER_H 21 #define RTC_RINGBUFFER_H 36 #define RINGBUFFER_DEFAULT_LENGTH 8 88 template <
class DataType>
119 : m_overwrite(true), m_readback(true),
120 m_timedwrite(false), m_timedread(false),
121 m_wtimeout(1, 0), m_rtimeout(1, 0),
123 m_wpos(0), m_rpos(0), m_fillcount(0), m_wcount(0),
190 initWritePolicy(prop);
191 initReadPolicy(prop);
216 Guard guard(m_posmutex);
247 return ::RTC::BufferStatus::BUFFER_OK;
274 Guard guard(m_posmutex);
279 return ::RTC::BufferStatus::BUFFER_OK;
305 virtual DataType*
wptr(
long int n = 0)
307 Guard guard(m_posmutex);
308 return &m_buffer[(m_wpos + n + m_length) % m_length];
335 virtual ReturnCode
advanceWptr(
long int n = 1,
bool unlock_enable =
true)
338 if(unlock_enable && n > 0)
340 Guard fguard(m_empty.mutex);
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)))
355 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
358 m_wpos = (m_wpos + n + m_length) % m_length;
367 Guard fguard(m_empty.mutex);
368 m_empty.cond.signal();
372 return ::RTC::BufferStatus::BUFFER_OK;
401 virtual ReturnCode
put(
const DataType& value)
403 Guard guard(m_posmutex);
404 m_buffer[m_wpos] = value;
405 return ::RTC::BufferStatus::BUFFER_OK;
449 virtual ReturnCode
write(
const DataType& value,
450 long int sec = -1,
long int nsec = 0)
453 Guard guard(m_full.mutex);
458 bool timedwrite(m_timedwrite);
459 bool overwrite(m_overwrite);
467 if (overwrite && !timedwrite)
471 else if (!overwrite && !timedwrite)
473 return ::RTC::BufferStatus::BUFFER_FULL;
475 else if (!overwrite && timedwrite)
479 sec = m_wtimeout.
sec();
480 nsec = m_wtimeout.
usec() * 1000;
483 if (!m_full.cond.wait(sec, nsec))
485 return ::RTC::BufferStatus::TIMEOUT;
490 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
500 return ::RTC::BufferStatus::BUFFER_OK;
526 Guard guard(m_posmutex);
527 return m_length - m_fillcount;
551 Guard guard(m_posmutex);
552 return m_length == m_fillcount;
576 virtual DataType*
rptr(
long int n = 0)
578 Guard guard(m_posmutex);
579 return &(m_buffer[(m_rpos + n + m_length) % m_length]);
604 virtual ReturnCode
advanceRptr(
long int n = 1,
bool unlock_enable =
true)
607 if(unlock_enable && n > 0)
609 Guard fguard(m_full.mutex);
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)))
623 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
626 m_rpos = (m_rpos + n + m_length) % m_length;
634 Guard fguard(m_full.mutex);
635 m_full.cond.signal();
639 return ::RTC::BufferStatus::BUFFER_OK;
666 virtual ReturnCode
get(DataType& value)
668 Guard gaurd(m_posmutex);
669 value = m_buffer[m_rpos];
670 return ::RTC::BufferStatus::BUFFER_OK;
691 virtual DataType&
get()
693 Guard gaurd(m_posmutex);
694 return m_buffer[m_rpos];
739 virtual ReturnCode
read(DataType& value,
740 long int sec = -1,
long int nsec = 0)
743 Guard gaurd(m_empty.mutex);
747 bool timedread(m_timedread);
748 bool readback(m_readback);
754 sec = m_rtimeout.
sec();
755 nsec = m_rtimeout.
usec() * 1000;
758 if (readback && !timedread)
762 return ::RTC::BufferStatus::BUFFER_EMPTY;
766 else if (!readback && !timedread)
768 return ::RTC::BufferStatus::BUFFER_EMPTY;
770 else if (!readback && timedread)
774 sec = m_rtimeout.
sec();
775 nsec = m_rtimeout.
usec() * 1000;
778 if (!m_empty.cond.wait(sec, nsec))
780 return ::RTC::BufferStatus::TIMEOUT;
785 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
794 return ::RTC::BufferStatus::BUFFER_OK;
823 Guard guard(m_posmutex);
848 Guard guard(m_posmutex);
849 return m_fillcount == 0;
855 if (!prop[
"length"].
empty())
870 std::string policy(prop[
"write.full_policy"]);
872 if (policy ==
"overwrite")
875 m_timedwrite =
false;
877 else if (policy ==
"do_nothing")
880 m_timedwrite =
false;
882 else if (policy ==
"block")
900 std::string policy(prop[
"read.empty_policy"]);
901 if (policy ==
"readback")
906 else if (policy ==
"do_nothing")
911 else if (policy ==
"block")
1029 std::vector<DataType> m_buffer;
1040 condition() : cond(mutex) {}
1074 #endif // RTC_RINGBUFFER_H std::string normalize(std::string &str)
文字列を正規化する
virtual DataType * rptr(long int n=0)
バッファの現在の読み出し要素のポインタ
Definition: RingBuffer.h:576
virtual ~RingBuffer(void)
仮想デストラクタ
Definition: RingBuffer.h:144
virtual ReturnCode read(DataType &value, long int sec=-1, long int nsec=0)
バッファから読み出す
Definition: RingBuffer.h:739
bool stringTo(To &val, const char *str)
与えられたstd::stringをオブジェクトに変換
Definition: stringutil.h:633
virtual ReturnCode length(size_t n)
バッファの長さをセットする
Definition: RingBuffer.h:242
Mutex クラス
Definition: Mutex.h:40
virtual bool full(void) const
バッファfullチェック
Definition: RingBuffer.h:549
#define RINGBUFFER_DEFAULT_LENGTH
Definition: RingBuffer.h:36
virtual void init(const coil::Properties &prop)
バッファの設定
Definition: RingBuffer.h:187
virtual DataType * wptr(long int n=0)
バッファの現在の書込み要素のポインタ
Definition: RingBuffer.h:305
TimeValue クラス
Definition: TimeValue.h:40
virtual ReturnCode reset()
バッファの状態をリセットする
Definition: RingBuffer.h:272
RingBuffer(long int length=RINGBUFFER_DEFAULT_LENGTH)
コンストラクタ
Definition: RingBuffer.h:118
virtual ReturnCode advanceRptr(long int n=1, bool unlock_enable=true)
読み出しポインタを進める
Definition: RingBuffer.h:604
#define BUFFERSTATUS_ENUM
RTC::BufferStatus 導入
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
バッファemptyチェック
Definition: RingBuffer.h:846
virtual size_t writable() const
バッファに書込み可能な要素数
Definition: RingBuffer.h:524
virtual size_t readable() const
バッファから読み出し可能な要素数
Definition: RingBuffer.h:821
リングバッファ実装クラス
Definition: RingBuffer.h:89
virtual ReturnCode write(const DataType &value, long int sec=-1, long int nsec=0)
バッファに書き込む
Definition: RingBuffer.h:449
プロパティセットを表現するクラス
Definition: Properties.h:101
virtual ReturnCode advanceWptr(long int n=1, bool unlock_enable=true)
書込みポインタを進める
Definition: RingBuffer.h:335
long int sec() const
秒単位の値を取得する
Definition: TimeValue.h:110
long int usec() const
マイクロ秒単位の値を取得する
Definition: TimeValue.h:131
virtual ReturnCode put(const DataType &value)
バッファにデータを書き込む
Definition: RingBuffer.h:401
BufferBase 抽象クラス
Definition: BufferBase.h:104
virtual size_t length(void) const
バッファ長を取得する
Definition: RingBuffer.h:214