メインページ | ネームスペース一覧 | クラス階層 | 構成 | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ

RtcRingBuffer.h

説明を見る。
00001 
00019 #ifndef __RtcRingBuffer_h__
00020 #define __RtcRingBuffer_h__
00021 
00022 #include <vector>
00023 #include <iostream>
00024 #include <ace/Synch.h>
00025 
00026 namespace RTM {
00027 
00052   template <class T> class RingBuffer
00053   {
00054   public:
00076         RingBuffer(int length)
00077           : m_Oldest(0), m_Newest(length)
00078         {
00079           if (length > 1)
00080                 {
00081                   m_Length = length;
00082                 }
00083           else
00084                 {
00085                   m_Length = 2;
00086                 }
00087           m_Buffer.resize(m_Length);
00088         }
00089         
00113         RingBuffer(int length, T inival)
00114           : m_Oldest(0), m_Newest(length)
00115         {
00116           if (length > 1)
00117                 {
00118                   m_Length = length;
00119                 }
00120           else
00121                 {
00122                   m_Length = 2;
00123                 }
00124           
00125           m_Buffer.resize(m_Length);
00126           
00127           for (int i = 0; i < m_Length; i++)
00128                 {
00129                   m_Buffer[i] = inival;
00130                 }
00131         }
00132         
00152         inline void put(const T& value)
00153         {
00154           m_Buffer[m_Oldest].write(value);
00155           
00156           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00157           m_Newest = m_Oldest;
00158           m_Oldest = (++m_Oldest) % m_Length;
00159 
00160         }
00161 
00177         inline const T& get_new()
00178         {
00179           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00180 
00181           return m_Buffer[m_Newest].read();
00182         }
00183 
00184 
00196         inline std::vector<T> get_new_rlist()
00197         {
00198           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00199 
00200           std::vector<T> data;
00201           int cnt(0);
00202           for (int i(m_Newest);
00203                    m_Buffer[i].is_new() && cnt < m_Length;
00204                    ++cnt, --i < 0 ? (i = m_Length - 1) : 0)
00205                 {
00206                   data.push_back(m_Buffer[i].read());
00207                 }
00208 
00209           return data;
00210         }
00211 
00212 
00224         inline std::vector<T> get_new_list()
00225         {
00226           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00227           
00228           std::vector<T> data;
00229           int len(new_data_len());
00230           data.resize(len);
00231           int npos;
00232           npos = m_Newest - (len - 1) < 0 ?
00233                 m_Newest - (len - 1) + m_Length  : m_Newest - (len - 1);
00234           int cnt(0);
00235           for ( ; cnt < len; ++cnt, ++npos > m_Length - 1 ? npos = 0 : 0)
00236                 {
00237                   data[cnt] = m_Buffer[npos].read();
00238                 }
00239 
00240           return data;
00241         }
00242 
00254         inline int new_data_len()
00255         {
00256           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00257 
00258           int cnt(0);
00259           for (int i(m_Newest);
00260                    m_Buffer[i].is_new() && cnt < m_Length;
00261                    ++cnt, --i < 0 ? (i = m_Length - 1) : 0)
00262                 {;}
00263 
00264           return cnt;
00265         }
00266 
00267 
00283         inline T& get_old()
00284         {
00285           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00286 
00287           return m_Buffer[m_Oldest].read();
00288         }
00289 
00305         virtual T& get_back(int pos)
00306         {
00307           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00308           pos = pos % m_Length;
00309 
00310           return m_Buffer[(m_Length + m_Newest - pos) % m_Length].read();
00311         }
00312 
00328         inline T& get_front(int pos)
00329         {
00330           ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00331           pos = pos % m_Length;
00332           
00333           return m_Buffer[(m_Oldest + pos) % m_Length].read();
00334         }
00335 
00351         inline int buff_length()
00352         {
00353           return m_Length;
00354         }
00355 
00367         inline bool is_new()
00368         {
00369           return m_Buffer[m_Newest].is_new();
00370         }
00371 
00372 
00373   protected:
00381         ACE_Thread_Mutex m_Mutex;
00382 
00390         int m_Length;
00391 
00399         template <class D> class Data
00400         {
00401         public:
00402           Data() : _data(), _is_new(false){;}
00403           inline Data& operator=(const D& other)
00404           {
00405                 this->_data = other;
00406                 this->_is_new = true;
00407                 return *this;
00408           }
00409           inline void write(const D& other)
00410           {
00411                 this->_is_new = true;
00412                 this->_data = other;
00413           }
00414           inline D& read()
00415           {
00416                 this->_is_new = false;
00417                 return this->_data;
00418           }
00419           inline bool is_new()
00420           {
00421                 return _is_new;
00422           }
00423         protected:
00424           D _data;
00425           bool _is_new;
00426         };
00427         std::vector<Data<T> > m_Buffer;
00428 
00436         int m_Newest;
00437 
00445         int m_Oldest;
00446   };
00447  
00448 };
00449 
00450 
00451 #endif // end of __RtcRingBuffer_h__

OpenRTMに対してWed Apr 6 14:22:11 2005に生成されました。 doxygen 1.3.6