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

Files at this revision

API Documentation at this revision

Comitter:
johnb
Date:
Thu Jan 30 20:15:33 2014 +0000
Parent:
7:e2d532183250
Commit message:
Add [] operator

Changed in this revision

CircularBuffer.h Show annotated file Show diff for this revision Revisions of this file
diff -r e2d532183250 -r d318a6948091 CircularBuffer.h
--- a/CircularBuffer.h	Sat Jan 18 22:32:30 2014 +0000
+++ b/CircularBuffer.h	Thu Jan 30 20:15:33 2014 +0000
@@ -81,6 +81,15 @@
                   just be emptied
         */
         void chomp( uint32_t length );
+  
+        /* Read a single byte from the buffer.  The byte is not removed.
+           The called must be responsible for ensuring that p_length < getSize()
+        
+           \param p_length The index of the byte to read, relative to the start 
+                           of the circulat buffer
+           \returns The byte at the specified position or a garbage byte in the 
+                    case that the buffer is too short to support the request */ 
+        uint8_t operator[]( uint32_t p_length ) const;
         
     private :
 
@@ -177,6 +186,13 @@
     return getSize() == T;
 }
 
+template<size_t T>
+uint8_t CircularBuffer<T>::operator[]( uint32_t p_length ) const
+{
+    return buffer[(readIndex + p_length) % T];
+}
+
+
 typedef CircularBuffer<32> SmallCircularBuffer;
 typedef CircularBuffer<128> MediumCircularBuffer;
 typedef CircularBuffer<512> BigCircularBuffer;