A template for ring buffer implementation
Dependents: AwsomeStation LoRaBaseStation LoRaTerminal
RingBuffer.cpp
- Committer:
- rba90
- Date:
- 2016-09-01
- Revision:
- 4:e996c0644148
- Parent:
- 3:21ee07b29eb7
- Child:
- 5:021f688bfdaa
File content as of revision 4:e996c0644148:
#include "RingBuffer.h" #include <stdio.h> #include <stdlib.h> template <typename T> CircularBuffer<T>::CircularBuffer(const size_t size) :buffer_size(size) { read_ptr = 0; write_ptr = 0; count = 0; // mutex lock mux = false; // overflow is_over_flow = false; // container // data = new T[buffer_size]; // use c styled memory allocation instead data = (T *)malloc(buffer_size * sizeof(T)); } template <typename T> CircularBuffer<T>::~CircularBuffer() { // delete[] data; free(data); } template <typename T> bool CircularBuffer<T>::isLocked() { return mux; } template <typename T> void CircularBuffer<T>::lock() { mux = true; } template <typename T> void CircularBuffer<T>::unlock() { mux = false; } template <typename T> void CircularBuffer<T>::enqueue(T in) { data[write_ptr++] = in; write_ptr %= buffer_size; count++; } template <typename T> T CircularBuffer<T>::dequeue() { T temp = data[read_ptr++]; read_ptr %= buffer_size; count--; return temp; } template <typename T> size_t CircularBuffer<T>::getReadPtr() { return read_ptr; } template <typename T> size_t CircularBuffer<T>::getWritePtr() { return write_ptr; } template <typename T> size_t CircularBuffer<T>::getCounter() { return count; } template <typename T> bool CircularBuffer<T>::getOverFlow() { return is_over_flow; } template <typename T> void CircularBuffer<T>::clearOverFlow() { is_over_flow = false; } template <typename T> T CircularBuffer<T>::first() { if (read_ptr > 0) { return data[read_ptr - 1]; } else { return data[read_ptr]; } } template <typename T> T CircularBuffer<T>::last() { if (write_ptr > 0) { return data[write_ptr - 1]; } else { return data[write_ptr]; } } template <typename T> T CircularBuffer<T>::operator[](size_t idx) { return data[idx]; } // force compiler to create code for template template class CircularBuffer<uint8_t>;