A template for ring buffer implementation

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

RingBuffer.h

Committer:
rba90
Date:
2016-09-01
Revision:
3:21ee07b29eb7
Parent:
2:8949ad751081
Child:
7:3e06927ef5ec

File content as of revision 3:21ee07b29eb7:

#ifndef RINGBUFFER_H_
#define RINGBUFFER_H_

#define DEFAULT_MAX_BUFFER_SZ 64

#include <stdint.h>
#include <stdlib.h>

template <typename T>
class CircularBuffer
{
private:
    const size_t buffer_size;
    size_t read_ptr;
    size_t write_ptr;
    size_t count;
    
    // mutex lock
    bool mux; 
    
    // overflow
    bool is_over_flow;
    
    // container
    T *data;
    
    
public:
    CircularBuffer(const size_t size=DEFAULT_MAX_BUFFER_SZ);
    ~CircularBuffer();
    
    // psudo mutex
    bool isLocked();
    void lock();
    void unlock();
    
    // enqueue and dequeue
    void enqueue(T in);
    T dequeue();
    
    // pointer operation
    size_t getReadPtr();
    size_t getWritePtr();
    size_t getCounter();
    
    // overflow
    bool getOverFlow();
    void clearOverFlow();
    
    // operation
    T first();
    T last();
    
    // random access
    T operator[](size_t idx);
};

#endif