Ringbuffer class
RingBuffer.cpp
- Committer:
- chris215
- Date:
- 2016-01-11
- Revision:
- 0:fed94e516719
- Child:
- 1:fa4c2377a741
File content as of revision 0:fed94e516719:
#include "RingBuffer.h" // make the linker aware of some possible types template class RingBuffer<uint8_t>; template class RingBuffer<int8_t>; template class RingBuffer<uint16_t>; template class RingBuffer<int16_t>; template class RingBuffer<uint32_t>; template class RingBuffer<int32_t>; template class RingBuffer<uint64_t>; template class RingBuffer<int64_t>; template class RingBuffer<char>; template class RingBuffer<wchar_t>; template <class T> RingBuffer<T>::RingBuffer(uint32_t size) { m_buf = new T[size]; m_LatchedBuf = new T[size]; m_size = size; clear(); return; } template <class T> RingBuffer<T>::~RingBuffer() { delete [] m_buf; return; } template <class T> void RingBuffer<T>::clear(void) { m_SetBufferToReadOnly = false; m_wloc = 0; m_rloc = 0; m_ActualCapacity = 0; memset(m_buf, 0, m_size * sizeof(T)); return; } template <class T> uint32_t RingBuffer<T>::getSize() { return m_size; } //Return the size of the ring buffer template <class T> uint32_t RingBuffer<T>::getCapacity() { return m_ActualCapacity; } //Return the number of elements stored in the buffer template <class T> void RingBuffer<T>::put(T data) { if(!m_SetBufferToReadOnly) { m_buf[m_wloc++] = data; m_wloc %= (m_size); if(m_ActualCapacity >= m_size) { //we are overriding old data! lets increment the read pointer accordingly m_rloc++; m_rloc %= (m_size); } else m_ActualCapacity++; } return; } template <class T> T RingBuffer<T>::get(void) { T data_pos = 0; if(m_ActualCapacity > 0) { data_pos = m_buf[m_rloc++]; m_rloc %= (m_size); m_ActualCapacity--; } else m_SetBufferToReadOnly = false; //if we have and empty buffer ReadOnly is disabled automatically return data_pos; } template <class T> T* RingBuffer<T>::LatchBuffer(void) { //Data is latched, there is no use in keeping it any longer return LatchBufferPartial(m_ActualCapacity); } template <class T> T* RingBuffer<T>::LatchBufferPartial(uint32_t SizeToLatch) { if(SizeToLatch <= m_ActualCapacity) { for(int x = 0; x < m_ActualCapacity; x++) { m_LatchedBuf[x] = this->get(); } return m_LatchedBuf; } return NULL; }