main_ubi_sdk v 0.1.3
This is UBI4 documentation
FIFO.h
Go to the documentation of this file.
1/*
2 * FIFO.h
3 *
4 * Created on: Jun 28, 2022
5 * Author: PC
6 */
7
8//#ifndef INC_FIFO_H_
9//#define INC_FIFO_H_
10
11
12/*
13#ifdef __cplusplus
14extern "C" {
15#endif
16*/
17
18#pragma once
19
20
21#include "stdint.h"
22#include "stdbool.h"
23
24
25template<int SIZE, class DATA_T=unsigned char>
27{
28public:
29 typedef uint16_t INDEX_T;
30
31private:
32 DATA_T _data[SIZE];
33 DATA_T nullValue;
34 volatile INDEX_T _readCount;
35 volatile INDEX_T _writeCount;
36 static const INDEX_T _mask = SIZE - 1;
37
38
39public:
40
41 inline bool Write(DATA_T value)
42 {
43 if(IsFull())
44 return 0;
45 _data[_writeCount++ & _mask] = value;
46 return true;
47 }
48
49 inline bool Read(DATA_T &value)
50 {
51 if(IsEmpty())
52 return 0;
53 value = _data[_readCount++ & _mask];
54 return true;
55 }
56
57 inline DATA_T First()const
58 {
59 return operator[](0);
60 }
61
62 inline DATA_T Last()const
63 {
64 return operator[](Count());
65 }
66
67 inline DATA_T& operator[] (INDEX_T i)
68 {
69 if(IsEmpty() || i > Count())
70 return nullValue;
71 return _data[(_readCount + i) & _mask];
72 }
73
74 inline const DATA_T operator[] (INDEX_T i)const
75 {
76 if(IsEmpty()|| i > Count())
77 return nullValue;
78 return _data[(_readCount + i) & _mask];
79 }
80
81 inline bool IsEmpty()const
82 {
83 return _writeCount == _readCount;
84 }
85
86 inline bool IsFull()const
87 {
88 return ((_writeCount - _readCount) & (INDEX_T)~(_mask)) != 0;
89 }
90
92 {
93 return (_writeCount - _readCount) & _mask;
94 }
95
96 inline void Clear()
97 {
98 _readCount=0;
99 _writeCount=0;
100 }
101
102 inline unsigned Size()
103 {return SIZE;}
104};
105
106
107
108/*
109#ifdef __cplusplus
110}
111#endif
112
113*/
114
115
116//#endif /* INC_FIFO_H_ */
Definition FIFO.h:27
DATA_T Last() const
Definition FIFO.h:62
bool Read(DATA_T &value)
Definition FIFO.h:49
DATA_T First() const
Definition FIFO.h:57
DATA_T & operator[](INDEX_T i)
Definition FIFO.h:67
INDEX_T Count() const
Definition FIFO.h:91
uint16_t INDEX_T
Definition FIFO.h:29
unsigned Size()
Definition FIFO.h:102
bool IsEmpty() const
Definition FIFO.h:81
void Clear()
Definition FIFO.h:96
bool Write(DATA_T value)
Definition FIFO.h:41
bool IsFull() const
Definition FIFO.h:86