Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TFifoClass.h
Go to the documentation of this file.
1#pragma once
2
3namespace sf
4{
5
12template<typename T>
14{
15 public:
19 typedef int size_type;
20
25 {
26 initialize();
27 }
28
33 {
34 initialize();
35 set(size);
36 }
37
42
48 bool push(const T& item);
49
53 bool push();
54
58 bool push(const T* item, size_type count);
59
63 const T& peek() const;
64
68 const T& latest() const;
69
73 T pop();
74
78 bool pop(T& item);
79
83 [[nodiscard]] bool empty() const;
84
88 [[nodiscard]] size_type size() const;
89
93 [[nodiscard]] size_type sizeMax() const
94 {
95 return (_valid) ? (_bufSize - 1) : 0;
96 }
97
101 [[nodiscard]] size_type sizeRemain() const
102 {
103 return _valid ? (sizeMax() - size()) : 0;
104 }
105
109 [[nodiscard]] bool isValid() const
110 {
111 return _valid;
112 }
113
117 [[nodiscard]] bool isFull() const
118 {
119 return sizeRemain() ? 0 : 1;
120 }
121
125 void clear()
126 {
127 _head = _tail = 0;
128 }
129
136
141
145 const T& operator[](size_type pos) const;
146
150 [[nodiscard]] size_type getTail() const;
151
155 [[nodiscard]] size_type getHead() const;
156
160 const T* getBuffer(size_type pos = 0) const;
161
162 private:
163 void initialize();
164
165 size_type _tail{0};
166 size_type _head{0};
167 T* _buffer{nullptr};
168 size_type _valid{0};
169 size_type _bufSize{0};
170 T _zero;
171};
172
178template<typename T>
180{
181 if (_valid)
182 {
183 delete[] _buffer;
184 }
185}
186
187template<typename T>
188void TFifoClass<T>::initialize()
189{
190 _valid = 0;
191 _bufSize = 0;
192 _head = 0;
193 _tail = 0;
194 _buffer = nullptr;
195 memset(&_zero, 0, sizeof(T));
196}
197
198template<typename T>
200{
201 if (size < 1)
202 {
203 return false;
204 }
205 _bufSize = size + 1;
206 _head = _tail = 0;
207 if (_buffer)
208 {
209 delete[] _buffer;
210 }
211 _buffer = new T[_bufSize];
212 memset(_buffer, 0, sizeof(T) * _bufSize);
213 _valid = true;
214 return true;
215}
216
217template<typename T>
218bool TFifoClass<T>::push(const T& item)
219{
220 if ((((_tail + 1) % _bufSize)) == _head)
221 {
222 return false;
223 }
224 _buffer[_tail++] = item;
225 _tail %= _bufSize;
226 return true;
227}
228
229template<typename T>
230bool TFifoClass<T>::push(const T* item, TFifoClass<T>::size_type count)
231{
232 if (sizeRemain() < count)
233 {
234 return true;
235 }
236 for (size_type i = 0; i < count; push(item[i++]))
237 {
238 }
239 return false;
240}
241
242template<typename T>
244{
245 return push(_zero);
246}
247
248template<typename T>
249const T& TFifoClass<T>::peek() const
250{
251 if (_head == _tail)
252 {
253 return _zero;
254 }
255 return _buffer[_head];
256}
257
258template<typename T>
259const T& TFifoClass<T>::latest() const
260{
261 if (_head == _tail)
262 {
263 return _zero;
264 }
265 return _buffer[(((_tail + _bufSize - 1) % _bufSize))];
266}
267
268template<typename T>
270{
271 if (_tail == _head)
272 {
273 return _zero;
274 }
275 T tmp = _buffer[_head++];
276 _head %= _bufSize;
277 return tmp;
278}
279
280template<typename T>
281bool TFifoClass<T>::pop(T& item)
282{
283 if (_tail == _head)
284 {
285 item = _zero;
286 return false;
287 }
288 item = _buffer[_head++];
289 _head %= _bufSize;
290 return true;
291}
292
293template<typename T>
294bool TFifoClass<T>::empty() const
295{
296 return size() == 0;
297}
298
299template<typename T>
301{
302 return (_tail < _head) ? (_bufSize + (_tail - _head)) : (_tail - _head);
303}
304
305template<typename T>
307{
308 return _buffer[(pos + _head) % _bufSize];
309}
310
311template<typename T>
313{
314 return _buffer[(pos + _head) % _bufSize];
315}
316
317template<typename T>
318inline const T* TFifoClass<T>::getBuffer(TFifoClass<T>::size_type pos) const
319{
320 return &_buffer[pos];
321}
322
323template<typename T>
325{
326 return _tail;
327}
328
329template<typename T>
331{
332 return _head;
333}
334
336}// namespace sf
Fifo template for classes and structure pointers.
Definition TFifoClass.h:14
size_type size() const
Returns current buffer contained data size.
size_type sizeMax() const
Returns maximum containable size.
Definition TFifoClass.h:93
bool isValid() const
Returns 1 if object is valid else 0.
Definition TFifoClass.h:109
bool empty() const
Returns if current contained buffer data size is zero.
const T & operator[](size_type pos) const
Const array operator offset from head of buffer.
size_type getHead() const
Access function for private member.
size_type sizeRemain() const
Returns remaining available size.
Definition TFifoClass.h:101
bool push(const T *item, size_type count)
Pushes multiple items if possible else it returns '0' and no items are inserted at all.
void clear()
Resets instance to the initial state.
Definition TFifoClass.h:125
T & operator[](size_type pos)
Array operator offset from head of buffer.
TFifoClass(size_type size)
Constructor that will setup the object immediately.
Definition TFifoClass.h:32
const T & latest() const
Gives latest item pushed into the fifo.
const T * getBuffer(size_type pos=0) const
Access function for private member.
TFifoClass()
Default constructor which needs Set() to validate object.
Definition TFifoClass.h:24
bool isFull() const
Returns '1' if object is full else 0.
Definition TFifoClass.h:117
bool push()
Push item of zero in fifo.
bool push(const T &item)
Pushes item into buffer.
~TFifoClass()
Destructor.
int size_type
Definition TFifoClass.h:19
bool pop(T &item)
Same as pop(void) but returns 'false' when the buffer is empty.
T pop()
Read item at head of buffer and removes it.
const T & peek() const
Returns next item without removing it.
bool set(size_type size)
InitializeBase object after default construction or to increase size.
size_type getTail() const
Access function for private member.
Definition Application.h:10