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 bool lock_ = (unlock_enable && n > 0);
341 m_empty.mutex.lock();
352 Guard guard(m_posmutex);
353 if ((n > 0 && n > static_cast<long int>(m_length - m_fillcount)) ||
354 (n < 0 && n < static_cast<long int>(-m_fillcount)))
358 m_empty.mutex.unlock();
360 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
363 m_wpos = (m_wpos + n + m_length) % m_length;
372 m_empty.cond.signal();
374 m_empty.mutex.unlock();
377 return ::RTC::BufferStatus::BUFFER_OK;
406 virtual ReturnCode
put(
const DataType& value)
408 Guard guard(m_posmutex);
409 m_buffer[m_wpos] = value;
410 return ::RTC::BufferStatus::BUFFER_OK;
454 virtual ReturnCode
write(
const DataType& value,
455 long int sec = -1,
long int nsec = 0)
458 Guard guard(m_full.mutex);
463 bool timedwrite(m_timedwrite);
464 bool overwrite(m_overwrite);
472 if (overwrite && !timedwrite)
476 else if (!overwrite && !timedwrite)
478 return ::RTC::BufferStatus::BUFFER_FULL;
480 else if (!overwrite && timedwrite)
484 sec = m_wtimeout.
sec();
485 nsec = m_wtimeout.
usec() * 1000;
488 if (!m_full.cond.wait(sec, nsec))
490 return ::RTC::BufferStatus::TIMEOUT;
495 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
505 return ::RTC::BufferStatus::BUFFER_OK;
531 Guard guard(m_posmutex);
532 return m_length - m_fillcount;
556 Guard guard(m_posmutex);
557 return m_length == m_fillcount;
581 virtual DataType*
rptr(
long int n = 0)
583 Guard guard(m_posmutex);
584 return &(m_buffer[(m_rpos + n + m_length) % m_length]);
609 virtual ReturnCode
advanceRptr(
long int n = 1,
bool unlock_enable =
true)
612 bool lock_ = (unlock_enable && n > 0);
625 Guard guard(m_posmutex);
626 if ((n > 0 && n > static_cast<long int>(m_fillcount)) ||
627 (n < 0 && n < static_cast<long int>(m_fillcount - m_length)))
631 m_full.mutex.unlock();
633 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
636 m_rpos = (m_rpos + n + m_length) % m_length;
644 m_full.cond.signal();
646 m_full.mutex.unlock();
649 return ::RTC::BufferStatus::BUFFER_OK;
676 virtual ReturnCode
get(DataType& value)
678 Guard gaurd(m_posmutex);
679 value = m_buffer[m_rpos];
680 return ::RTC::BufferStatus::BUFFER_OK;
701 virtual DataType&
get()
703 Guard gaurd(m_posmutex);
704 return m_buffer[m_rpos];
749 virtual ReturnCode
read(DataType& value,
750 long int sec = -1,
long int nsec = 0)
753 Guard gaurd(m_empty.mutex);
757 bool timedread(m_timedread);
758 bool readback(m_readback);
764 sec = m_rtimeout.
sec();
765 nsec = m_rtimeout.
usec() * 1000;
768 if (readback && !timedread)
772 return ::RTC::BufferStatus::BUFFER_EMPTY;
776 else if (!readback && !timedread)
778 return ::RTC::BufferStatus::BUFFER_EMPTY;
780 else if (!readback && timedread)
784 sec = m_rtimeout.
sec();
785 nsec = m_rtimeout.
usec() * 1000;
788 if (!m_empty.cond.wait(sec, nsec))
790 return ::RTC::BufferStatus::TIMEOUT;
795 return ::RTC::BufferStatus::PRECONDITION_NOT_MET;
804 return ::RTC::BufferStatus::BUFFER_OK;
833 Guard guard(m_posmutex);
858 Guard guard(m_posmutex);
859 return m_fillcount == 0;
865 if (!prop[
"length"].
empty())
880 std::string policy(prop[
"write.full_policy"]);
882 if (policy ==
"overwrite")
885 m_timedwrite =
false;
887 else if (policy ==
"do_nothing")
890 m_timedwrite =
false;
892 else if (policy ==
"block")
910 std::string policy(prop[
"read.empty_policy"]);
911 if (policy ==
"readback")
916 else if (policy ==
"do_nothing")
921 else if (policy ==
"block")
1039 std::vector<DataType> m_buffer;
1050 condition() : cond(mutex) {}
1084 #endif // RTC_RINGBUFFER_H std::string normalize(std::string &str)
文字列を正規化する
virtual DataType * rptr(long int n=0)
バッファの現在の読み出し要素のポインタ
Definition: RingBuffer.h:581
virtual ~RingBuffer(void)
仮想デストラクタ
Definition: RingBuffer.h:144
long int sec() const
秒単位の値を取得する
Definition: TimeValue.h:110
virtual ReturnCode read(DataType &value, long int sec=-1, long int nsec=0)
バッファから読み出す
Definition: RingBuffer.h:749
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
#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 bool full(void) const
バッファfullチェック
Definition: RingBuffer.h:554
virtual ReturnCode advanceRptr(long int n=1, bool unlock_enable=true)
読み出しポインタを進める
Definition: RingBuffer.h:609
#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.
リングバッファ実装クラス
Definition: RingBuffer.h:89
virtual ReturnCode write(const DataType &value, long int sec=-1, long int nsec=0)
バッファに書き込む
Definition: RingBuffer.h:454
virtual size_t readable() const
バッファから読み出し可能な要素数
Definition: RingBuffer.h:831
プロパティセットを表現するクラス
Definition: Properties.h:101
virtual ReturnCode advanceWptr(long int n=1, bool unlock_enable=true)
書込みポインタを進める
Definition: RingBuffer.h:335
virtual ReturnCode put(const DataType &value)
バッファにデータを書き込む
Definition: RingBuffer.h:406
virtual size_t length(void) const
バッファ長を取得する
Definition: RingBuffer.h:214
virtual bool empty(void) const
バッファemptyチェック
Definition: RingBuffer.h:856
long int usec() const
マイクロ秒単位の値を取得する
Definition: TimeValue.h:131
BufferBase 抽象クラス
Definition: BufferBase.h:104
virtual size_t writable() const
バッファに書込み可能な要素数
Definition: RingBuffer.h:529