27
27
28
28
namespace arduino {
29
29
30
-
31
30
#define FIFO_DEFAULT_SIZE 64
32
31
33
32
template <typename T, uint32_t size = FIFO_DEFAULT_SIZE>
@@ -38,28 +37,30 @@ class FifoBuffer
38
37
synchronized {
39
38
return (uint32_t )(index + 1 ) % size;
40
39
}
40
+ return 0 ; // Fallback return
41
41
}
42
+
42
43
inline bool isEmpty () const { return (_numElems == 0 ); }
43
- T _aucBuffer[size] ;
44
- uint32_t _iHead ;
45
- uint32_t _iTail ;
44
+ T _aucBuffer[size];
45
+ uint32_t _iHead;
46
+ uint32_t _iTail;
46
47
uint32_t _numElems;
48
+
47
49
public:
48
50
/* ---------------------------------------------------------------------- */
49
- FifoBuffer ( void ) {
50
- memset ( _aucBuffer, 0 , size * sizeof (T) ) ;
51
+ FifoBuffer (void ) {
52
+ memset (_aucBuffer, 0 , size * sizeof (T)) ;
51
53
clear ();
52
54
}
53
55
/* ---------------------------------------------------------------------- */
54
- bool store ( T c ) {
56
+ bool store (T c) {
55
57
bool rv = true ;
56
58
synchronized {
57
59
if (!isFull ()) {
58
- _aucBuffer[_iHead] = c ;
60
+ _aucBuffer[_iHead] = c;
59
61
_iHead = nextIndex (_iHead);
60
62
_numElems++;
61
- }
62
- else {
63
+ } else {
63
64
rv = false ;
64
65
}
65
66
}
@@ -72,36 +73,39 @@ class FifoBuffer
72
73
_numElems = 0 ;
73
74
}
74
75
/* ---------------------------------------------------------------------- */
75
- T read (bool * read_ok) {
76
+ T read (bool * read_ok) {
76
77
*read_ok = true ;
77
78
if (isEmpty ()) {
78
79
*read_ok = false ;
79
- return _aucBuffer[ 0 ];
80
+ return T (); // Return default-constructed object
80
81
}
81
82
synchronized {
82
83
T value = _aucBuffer[_iTail];
83
84
_iTail = nextIndex (_iTail);
84
85
_numElems--;
85
-
86
+
86
87
return value;
87
88
}
89
+ return T (); // Fallback return
88
90
}
89
91
/* ---------------------------------------------------------------------- */
90
92
int available () {
91
93
synchronized {
92
94
return _numElems;
93
95
}
96
+ return 0 ; // Fallback return
94
97
}
95
98
/* ---------------------------------------------------------------------- */
96
99
int freePositions () {
97
100
synchronized {
98
101
return (size - _numElems);
99
102
}
103
+ return 0 ; // Fallback return
100
104
}
101
105
/* ---------------------------------------------------------------------- */
102
106
T peek () {
103
107
if (isEmpty ())
104
- return - 1 ;
108
+ return T (); // Return default-constructed object
105
109
106
110
return _aucBuffer[_iTail];
107
111
}
@@ -110,15 +114,13 @@ class FifoBuffer
110
114
synchronized {
111
115
return (_numElems == size);
112
116
}
117
+ return false ; // Fallback return
113
118
}
114
119
/* ---------------------------------------------------------------------- */
115
- uint32_t lenght () const { return size; }
116
-
117
-
120
+ uint32_t lenght () const { return size; }
118
121
};
119
122
120
-
121
- } // namespace arduino
123
+ } // namespace arduino
122
124
123
125
#endif /* _ARDUINO_FIFO_BUFFER_DH_ */
124
- #endif /* __cplusplus */
126
+ #endif /* __cplusplus */
0 commit comments