Ringbuffer class

Committer:
chris215
Date:
Mon Jan 11 04:46:57 2016 +0000
Revision:
0:fed94e516719
Child:
1:fa4c2377a741
Simple RingBuffer class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris215 0:fed94e516719 1 #include "RingBuffer.h"
chris215 0:fed94e516719 2
chris215 0:fed94e516719 3 // make the linker aware of some possible types
chris215 0:fed94e516719 4 template class RingBuffer<uint8_t>;
chris215 0:fed94e516719 5 template class RingBuffer<int8_t>;
chris215 0:fed94e516719 6 template class RingBuffer<uint16_t>;
chris215 0:fed94e516719 7 template class RingBuffer<int16_t>;
chris215 0:fed94e516719 8 template class RingBuffer<uint32_t>;
chris215 0:fed94e516719 9 template class RingBuffer<int32_t>;
chris215 0:fed94e516719 10 template class RingBuffer<uint64_t>;
chris215 0:fed94e516719 11 template class RingBuffer<int64_t>;
chris215 0:fed94e516719 12 template class RingBuffer<char>;
chris215 0:fed94e516719 13 template class RingBuffer<wchar_t>;
chris215 0:fed94e516719 14
chris215 0:fed94e516719 15 template <class T>
chris215 0:fed94e516719 16 RingBuffer<T>::RingBuffer(uint32_t size)
chris215 0:fed94e516719 17 {
chris215 0:fed94e516719 18 m_buf = new T[size];
chris215 0:fed94e516719 19 m_LatchedBuf = new T[size];
chris215 0:fed94e516719 20 m_size = size;
chris215 0:fed94e516719 21 clear();
chris215 0:fed94e516719 22
chris215 0:fed94e516719 23 return;
chris215 0:fed94e516719 24 }
chris215 0:fed94e516719 25
chris215 0:fed94e516719 26 template <class T>
chris215 0:fed94e516719 27 RingBuffer<T>::~RingBuffer()
chris215 0:fed94e516719 28 {
chris215 0:fed94e516719 29 delete [] m_buf;
chris215 0:fed94e516719 30
chris215 0:fed94e516719 31 return;
chris215 0:fed94e516719 32 }
chris215 0:fed94e516719 33
chris215 0:fed94e516719 34 template <class T>
chris215 0:fed94e516719 35 void RingBuffer<T>::clear(void)
chris215 0:fed94e516719 36 {
chris215 0:fed94e516719 37 m_SetBufferToReadOnly = false;
chris215 0:fed94e516719 38 m_wloc = 0;
chris215 0:fed94e516719 39 m_rloc = 0;
chris215 0:fed94e516719 40 m_ActualCapacity = 0;
chris215 0:fed94e516719 41 memset(m_buf, 0, m_size * sizeof(T));
chris215 0:fed94e516719 42 return;
chris215 0:fed94e516719 43 }
chris215 0:fed94e516719 44
chris215 0:fed94e516719 45 template <class T>
chris215 0:fed94e516719 46 uint32_t RingBuffer<T>::getSize()
chris215 0:fed94e516719 47 {
chris215 0:fed94e516719 48 return m_size;
chris215 0:fed94e516719 49 } //Return the size of the ring buffer
chris215 0:fed94e516719 50
chris215 0:fed94e516719 51 template <class T>
chris215 0:fed94e516719 52 uint32_t RingBuffer<T>::getCapacity()
chris215 0:fed94e516719 53 {
chris215 0:fed94e516719 54 return m_ActualCapacity;
chris215 0:fed94e516719 55 } //Return the number of elements stored in the buffer
chris215 0:fed94e516719 56
chris215 0:fed94e516719 57 template <class T>
chris215 0:fed94e516719 58 void RingBuffer<T>::put(T data)
chris215 0:fed94e516719 59 {
chris215 0:fed94e516719 60 if(!m_SetBufferToReadOnly)
chris215 0:fed94e516719 61 {
chris215 0:fed94e516719 62 m_buf[m_wloc++] = data;
chris215 0:fed94e516719 63 m_wloc %= (m_size);
chris215 0:fed94e516719 64 if(m_ActualCapacity >= m_size)
chris215 0:fed94e516719 65 {
chris215 0:fed94e516719 66 //we are overriding old data! lets increment the read pointer accordingly
chris215 0:fed94e516719 67 m_rloc++;
chris215 0:fed94e516719 68 m_rloc %= (m_size);
chris215 0:fed94e516719 69 }
chris215 0:fed94e516719 70 else
chris215 0:fed94e516719 71 m_ActualCapacity++;
chris215 0:fed94e516719 72
chris215 0:fed94e516719 73 }
chris215 0:fed94e516719 74 return;
chris215 0:fed94e516719 75 }
chris215 0:fed94e516719 76
chris215 0:fed94e516719 77 template <class T>
chris215 0:fed94e516719 78 T RingBuffer<T>::get(void)
chris215 0:fed94e516719 79 {
chris215 0:fed94e516719 80 T data_pos = 0;
chris215 0:fed94e516719 81 if(m_ActualCapacity > 0)
chris215 0:fed94e516719 82 {
chris215 0:fed94e516719 83 data_pos = m_buf[m_rloc++];
chris215 0:fed94e516719 84 m_rloc %= (m_size);
chris215 0:fed94e516719 85 m_ActualCapacity--;
chris215 0:fed94e516719 86 }
chris215 0:fed94e516719 87 else
chris215 0:fed94e516719 88 m_SetBufferToReadOnly = false; //if we have and empty buffer ReadOnly is disabled automatically
chris215 0:fed94e516719 89 return data_pos;
chris215 0:fed94e516719 90 }
chris215 0:fed94e516719 91
chris215 0:fed94e516719 92 template <class T>
chris215 0:fed94e516719 93 T* RingBuffer<T>::LatchBuffer(void)
chris215 0:fed94e516719 94 {
chris215 0:fed94e516719 95 //Data is latched, there is no use in keeping it any longer
chris215 0:fed94e516719 96 return LatchBufferPartial(m_ActualCapacity);
chris215 0:fed94e516719 97 }
chris215 0:fed94e516719 98
chris215 0:fed94e516719 99 template <class T>
chris215 0:fed94e516719 100 T* RingBuffer<T>::LatchBufferPartial(uint32_t SizeToLatch)
chris215 0:fed94e516719 101 {
chris215 0:fed94e516719 102 if(SizeToLatch <= m_ActualCapacity)
chris215 0:fed94e516719 103 {
chris215 0:fed94e516719 104 for(int x = 0; x < m_ActualCapacity; x++)
chris215 0:fed94e516719 105 {
chris215 0:fed94e516719 106 m_LatchedBuf[x] = this->get();
chris215 0:fed94e516719 107 }
chris215 0:fed94e516719 108 return m_LatchedBuf;
chris215 0:fed94e516719 109 }
chris215 0:fed94e516719 110 return NULL;
chris215 0:fed94e516719 111 }