A template for ring buffer implementation
Dependents: AwsomeStation LoRaBaseStation LoRaTerminal
RingBuffer.h@1:5f641cf190da, 2016-07-23 (annotated)
- Committer:
- rba90
- Date:
- Sat Jul 23 07:39:05 2016 +0000
- Revision:
- 1:5f641cf190da
- Parent:
- buffer.h@0:36b372831d9e
- Child:
- 2:8949ad751081
Rename buffer to RingBuffer to avoid potential building error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:36b372831d9e | 1 | #ifndef BUFFER_H_ |
rba90 | 0:36b372831d9e | 2 | #define BUFFER_H_ |
rba90 | 0:36b372831d9e | 3 | |
rba90 | 0:36b372831d9e | 4 | #define DEFAULT_MAX_BUFFER_SZ 64 |
rba90 | 0:36b372831d9e | 5 | |
rba90 | 0:36b372831d9e | 6 | #include "stdint.h" |
rba90 | 0:36b372831d9e | 7 | |
rba90 | 0:36b372831d9e | 8 | template <typename T> |
rba90 | 0:36b372831d9e | 9 | class CircularBuffer |
rba90 | 0:36b372831d9e | 10 | { |
rba90 | 0:36b372831d9e | 11 | private: |
rba90 | 0:36b372831d9e | 12 | const uint32_t buffer_size; |
rba90 | 0:36b372831d9e | 13 | uint32_t read_ptr; |
rba90 | 0:36b372831d9e | 14 | uint32_t write_ptr; |
rba90 | 0:36b372831d9e | 15 | uint32_t count; |
rba90 | 0:36b372831d9e | 16 | |
rba90 | 0:36b372831d9e | 17 | // mutex lock |
rba90 | 0:36b372831d9e | 18 | bool mux; |
rba90 | 0:36b372831d9e | 19 | |
rba90 | 0:36b372831d9e | 20 | // overflow |
rba90 | 0:36b372831d9e | 21 | bool is_over_flow; |
rba90 | 0:36b372831d9e | 22 | |
rba90 | 0:36b372831d9e | 23 | // container |
rba90 | 0:36b372831d9e | 24 | T *data; |
rba90 | 0:36b372831d9e | 25 | |
rba90 | 0:36b372831d9e | 26 | |
rba90 | 0:36b372831d9e | 27 | public: |
rba90 | 0:36b372831d9e | 28 | CircularBuffer(const uint32_t size=DEFAULT_MAX_BUFFER_SZ); |
rba90 | 0:36b372831d9e | 29 | ~CircularBuffer(); |
rba90 | 0:36b372831d9e | 30 | |
rba90 | 0:36b372831d9e | 31 | // psudo mutex |
rba90 | 0:36b372831d9e | 32 | bool isLocked(); |
rba90 | 0:36b372831d9e | 33 | void lock(); |
rba90 | 0:36b372831d9e | 34 | void unlock(); |
rba90 | 0:36b372831d9e | 35 | |
rba90 | 0:36b372831d9e | 36 | // enqueue and dequeue |
rba90 | 0:36b372831d9e | 37 | void enqueue(T in); |
rba90 | 0:36b372831d9e | 38 | T dequeue(); |
rba90 | 0:36b372831d9e | 39 | |
rba90 | 0:36b372831d9e | 40 | // pointer operation |
rba90 | 0:36b372831d9e | 41 | uint32_t getReadPtr(); |
rba90 | 0:36b372831d9e | 42 | uint32_t getWritePtr(); |
rba90 | 0:36b372831d9e | 43 | uint32_t getCounter(); |
rba90 | 0:36b372831d9e | 44 | |
rba90 | 0:36b372831d9e | 45 | // overflow |
rba90 | 0:36b372831d9e | 46 | bool getOverFlow(); |
rba90 | 0:36b372831d9e | 47 | void clearOverFlow(); |
rba90 | 0:36b372831d9e | 48 | |
rba90 | 0:36b372831d9e | 49 | // operation |
rba90 | 0:36b372831d9e | 50 | T first(); |
rba90 | 0:36b372831d9e | 51 | T last(); |
rba90 | 0:36b372831d9e | 52 | |
rba90 | 0:36b372831d9e | 53 | // random access |
rba90 | 0:36b372831d9e | 54 | T operator[](uint32_t idx); |
rba90 | 0:36b372831d9e | 55 | }; |
rba90 | 0:36b372831d9e | 56 | |
rba90 | 0:36b372831d9e | 57 | #endif |