Ringbuffer class

Files at this revision

API Documentation at this revision

Comitter:
chris215
Date:
Wed Jan 13 05:31:28 2016 +0000
Parent:
1:fa4c2377a741
Commit message:
Removed templates usage and moved to byte pointers. This makes the ring buffer more generic and removes the requirement to know all templates in .cpp

Changed in this revision

RingBuffer.cpp Show annotated file Show diff for this revision Revisions of this file
RingBuffer.h Show annotated file Show diff for this revision Revisions of this file
diff -r fa4c2377a741 -r 959aad917307 RingBuffer.cpp
--- a/RingBuffer.cpp	Wed Jan 13 02:34:23 2016 +0000
+++ b/RingBuffer.cpp	Wed Jan 13 05:31:28 2016 +0000
@@ -1,7 +1,61 @@
 #include "RingBuffer.h"
 
+RingBuffer::RingBuffer(uint32_t NumberOfElements,uint32_t SizeOfElement)
+{
+    m_ElementSize = SizeOfElement;
+    m_size = NumberOfElements;
+    m_buf = new uint8_t[m_size*m_ElementSize];
+    m_latchingbuffer = new uint8_t[m_ElementSize];
+    clear();
+}
+RingBuffer::~RingBuffer()
+{
+    delete [] m_buf;
+    delete [] m_latchingbuffer;
+}
+void RingBuffer::clear(void)
+{
+    m_wloc = 0;
+    m_rloc = 0;
+    m_ActualCapacity = 0;
+    memset(m_buf, 0, m_size * m_ElementSize);   
+    memset(m_latchingbuffer, 0,  m_ElementSize); 
+    return;
+}
+void RingBuffer::put(void* data)
+{
+    uint8_t* srcData = (uint8_t*)data;
+    uint8_t* dstData = m_buf + (m_wloc*m_ElementSize);
+    
+    memcpy(dstData,srcData,m_ElementSize);
+    
+    m_wloc++;
+    m_wloc %= (m_size);
+    m_ActualCapacity++;       
+}
 
+int RingBuffer::get(void* data)
+{
+    uint8_t* SrcData =  m_buf + (m_rloc*m_ElementSize);
+    uint8_t* dstData = (uint8_t*)data;
+    memcpy(dstData,SrcData,m_ElementSize);
+    m_rloc++;
+    m_rloc %= (m_size);
+    m_ActualCapacity--;
+    return 1;
+}
 
+void RingBuffer::LatchBuffer(void* DstBuffer)
+{
+    uint8_t* src = (uint8_t*)DstBuffer;
+    do
+    {
+        get(src);
+        src++;
+    }
+    while(m_ActualCapacity > 0);
+}
+/*
 template <class T>
 RingBuffer<T>::RingBuffer(uint32_t size)
 {
@@ -103,4 +157,4 @@
 template class RingBuffer<int64_t>;
 template class RingBuffer<char>;
 template class RingBuffer<wchar_t>;
-
+*/
diff -r fa4c2377a741 -r 959aad917307 RingBuffer.h
--- a/RingBuffer.h	Wed Jan 13 02:34:23 2016 +0000
+++ b/RingBuffer.h	Wed Jan 13 05:31:28 2016 +0000
@@ -5,6 +5,34 @@
 #include <stdint.h>
 #include <string.h>
 
+class RingBuffer
+{
+    private:
+    uint8_t*            m_buf;
+    uint8_t*            m_latchingbuffer;
+    volatile uint32_t   m_wloc;
+    volatile uint32_t   m_rloc;
+    volatile uint32_t   m_ActualCapacity;
+    uint32_t            m_size;
+    uint32_t            m_ElementSize;
+    
+    public:
+    RingBuffer(uint32_t NumberOfElements = 128,uint32_t SizeOfElement = 1);
+    ~RingBuffer();
+    
+    void put(void* data);
+    int get(void* data);
+    
+    void clear(void);
+    uint32_t GetSize(){return m_size;}; //Return the size of the ring buffer
+    
+    uint32_t GetCapacity(){return m_ActualCapacity;}; //Return the number of elements stored in the buffer
+    
+    uint32_t GetElementSize(){return m_ElementSize;};
+    
+    void LatchBuffer(void* DstBuffer);
+};
+/*
 template <typename T>
 class RingBuffer
 {
@@ -36,5 +64,5 @@
     
     
 };
-
+*/
 #endif
\ No newline at end of file