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

RingBuffer.h

説明を見る。
00001 // -*- C++ -*-
00020 /*
00021  * $Log: RingBuffer.h,v $
00022  * Revision 1.5  2007/04/26 15:32:56  n-ando
00023  * The header include order was modified to define _REENTRANT before
00024  * including ace/config-lite.h in Linux systems.
00025  * In ace 5.4.7 or later, _REENTRANT flag should be defined explicitly.
00026  *
00027  * Revision 1.4  2007/01/06 18:02:49  n-ando
00028  * The RingBuffer has been completely re-implemented.
00029  *
00030  *
00031  */
00032 
00033 #ifndef RingBuffer_h
00034 #define RingBuffer_h
00035 
00036 #include <rtm/RTC.h>
00037 
00038 #include <algorithm>
00039 //#include <ace/Synch.h>
00040 #include <rtm/BufferBase.h>
00041 
00042 namespace RTC
00043 {
00044   template <class DataType>
00045   class RingBuffer
00046     : public BufferBase<DataType>
00047   {
00048   public:
00049     RingBuffer(long int length)
00050       : m_length(length < 2 ? 2 : length - 1),
00051         m_oldPtr(0),
00052         m_newPtr(length < 2 ? 2 : length - 1)
00053     {
00054       m_buffer.resize(m_length);
00055     }
00056 
00068     virtual ~RingBuffer(){};
00069     
00070 
00071     void init(DataType& data)
00072     {
00073       for (long int i = 0; i < m_length; ++i)
00074         {
00075           put(data);
00076         }
00077     }
00078 
00090     virtual long int length() const
00091     {
00092       return m_length;
00093     }
00094 
00106     virtual bool write(const DataType& value)
00107     {
00108       put(value);
00109       return true;
00110     }
00111 
00123     virtual bool read(DataType& value)
00124     {
00125       value = get();
00126       return true;
00127     }
00128 
00140     virtual bool isFull() const
00141     {
00142       return false;
00143     }
00155     virtual bool isEmpty() const
00156     {
00157       return !(this->isNew());
00158     }
00159 
00160     bool isNew() const
00161     {
00162       return m_buffer[m_newPtr].isNew();
00163     }
00164 
00165   protected:
00177     virtual void put(const DataType& data)
00178     {
00179       m_buffer[m_oldPtr].write(data);
00180       //      ACE_Guard<ACE_Thread_Mutex> guard(m_Mutex);
00181       m_newPtr = m_oldPtr;
00182       m_oldPtr = (++m_oldPtr) % m_length;
00183     }
00184 
00196     virtual const DataType& get()
00197     {
00198       return m_buffer[m_newPtr].read();
00199     }
00200 
00212     virtual DataType& getRef()
00213     {
00214       return m_buffer[m_newPtr].data;
00215     }
00216 
00217   private:
00218     long int m_length;
00219     long int m_oldPtr;
00220     long int m_newPtr;
00221 
00229     template <class D>
00230     class Data
00231     {
00232     public:
00233       Data() : data(), is_new(false){;}
00234       inline Data& operator=(const D& other)
00235       {
00236         this->data = other;
00237         this->is_new = true;
00238         return *this;
00239       }
00240       inline void write(const D& other)
00241       {
00242         this->is_new = true;
00243         this->data = other;
00244       }
00245       inline D& read()
00246       {
00247         this->is_new = false;
00248         return this->data;
00249       }
00250       inline bool isNew() const
00251       {
00252         return is_new;
00253       }
00254       D    data;
00255       bool is_new;
00256     };
00257 
00258     std::vector<Data<DataType> > m_buffer;
00259 
00260   };
00261 }; // namespace RTC
00262 
00263 #endif // RingBuffer_h

OpenRTMに対してTue May 29 16:45:15 2007に生成されました。  doxygen 1.4.1