Small library for using circular buffers (forked from François Berder's implementation in order to add methods and fix problems)

Dependents:   CircularBufferTest XBeeApi

Fork of CircularBuffer by Francois Berder

Test suite can be found in this application, CircularBufferTest

Revision:
3:9a45d6675e65
Parent:
2:6f3630f5fa06
Child:
4:e15dee1d59ee
--- a/CircularBuffer.h	Fri Sep 20 09:16:45 2013 +0000
+++ b/CircularBuffer.h	Fri Sep 20 10:04:23 2013 +0000
@@ -10,8 +10,11 @@
         
         uint32_t read(uint8_t *data, uint32_t length);
         uint32_t write(uint8_t *data, uint32_t length);
+        
         uint32_t getCapacity() const;
-        uint32_t getSize() const;
+        uint32_t getSize() const;  
+        bool isEmpty() const;
+        bool isFull() const;
         
     private :
     
@@ -22,7 +25,7 @@
 
 template<size_t T>
 CircularBuffer<T>::CircularBuffer():
-readIndex(0),
+readIndex(T),
 writeIndex(0)
 {
 }
@@ -31,7 +34,7 @@
 uint32_t CircularBuffer<T>::read(uint8_t *data, uint32_t length)
 {
     uint32_t n = 0;
-    while(n < length && getSize() < T)
+    while(n < length && getSize() > 0)
     {
         if(readIndex == T)
             readIndex = 0;
@@ -45,7 +48,7 @@
 uint32_t CircularBuffer<T>::write(uint8_t *data, uint32_t length)
 {
     uint32_t n = 0;
-    while(n < length && getSize() > 0)
+    while(n < length && getSize() < T)
     {
         if(writeIndex == T)
             writeIndex = 0;
@@ -64,7 +67,19 @@
 template<size_t T>
 uint32_t CircularBuffer<T>::getSize() const
 {
-    return ((writeIndex >= readIndex) ? (T - writeIndex + readIndex) : (readIndex - writeIndex));
+    return ((writeIndex > readIndex) ? (writeIndex - readIndex) : (T + writeIndex - readIndex));
+}
+
+template<size_t T>
+bool CircularBuffer<T>::isEmpty() const
+{
+    return getSize() == 0;
+}
+
+template<size_t T>
+bool CircularBuffer<T>::isFull() const
+{
+    return getSize() == T;
 }
 
 typedef CircularBuffer<32> SmallCircularBuffer;