FIFO.h
Go to the documentation of this file.
1 /* $Id: FIFO.h 1151 2011-06-06 21:13:05Z bhagman $
2 ||
3 || @author Alexander Brevig <abrevig@wiring.org.co>
4 || @url http://wiring.org.co/
5 || @contribution Brett Hagman <bhagman@wiring.org.co>
6 ||
7 || @description
8 || | A simple FIFO class, mostly for primitive types but can be used with
9 || | classes if assignment to int is allowed.
10 || | This FIFO is not dynamic, so be sure to choose an appropriate size for it.
11 || |
12 || | Wiring Common API
13 || #
14 ||
15 || @license Please see cores/Common/License.txt.
16 ||
17 */
18 
19 #ifndef FIFO_H
20 #define FIFO_H
21 
22 #include "Countable.h"
23 
24 template <typename T, int rawSize> class FIFO : public Countable<T>
25 {
26 public:
27  const int size; // speculative feature, in case it's needed
28 
29  FIFO();
30 
31  T dequeue(); // get next element
32  bool enqueue(T element); // add an element
33  T peek() const; // get the next element without releasing it from the FIFO
34  void flush(); // reset to default state
35 
36  //how many elements are currently in the FIFO?
37  unsigned int count() const override
38  {
39  return numberOfElements;
40  }
41 
42  bool full() const
43  {
44  return (count() >= rawSize);
45  }
46 
47  const T& operator[](unsigned int index) const override
48  {
49  return raw[index]; /* unsafe */
50  }
51 
52  T& operator[](unsigned int index) override
53  {
54  return raw[index]; /* unsafe */
55  }
56 
57 protected:
58  volatile int numberOfElements;
59  int nextIn;
60  int nextOut;
61  T raw[rawSize];
62 };
63 
64 template <typename T, int rawSize> FIFO<T, rawSize>::FIFO() : size(rawSize)
65 {
66  flush();
67 }
68 
69 template <typename T, int rawSize> bool FIFO<T, rawSize>::enqueue(T element)
70 {
71  if(full()) {
72  return false;
73  }
75  raw[nextIn] = element;
76  // advance to next index, wrap if needed
77  if(++nextIn >= rawSize) {
78  nextIn = 0;
79  }
80  return true;
81 }
82 
83 template <typename T, int rawSize> T FIFO<T, rawSize>::dequeue()
84 {
85  T item;
87  item = raw[nextOut];
88  if(++nextOut >= rawSize) // advance to next index, wrap if needed
89  nextOut = 0;
90  return item;
91 }
92 
93 template <typename T, int rawSize> T FIFO<T, rawSize>::peek() const
94 {
95  return raw[nextOut];
96 }
97 
98 template <typename T, int rawSize> void FIFO<T, rawSize>::flush()
99 {
101 }
102 
103 #endif
104 // FIFO_H
volatile int numberOfElements
Definition: FIFO.h:58
Definition: Countable.h:19
unsigned int count() const override
Definition: FIFO.h:37
int nextOut
Definition: FIFO.h:60
const int size
Definition: FIFO.h:27
T raw[rawSize]
Definition: FIFO.h:61
FIFO()
Definition: FIFO.h:64
T dequeue()
Definition: FIFO.h:83
bool full() const
Definition: FIFO.h:42
T peek() const
Definition: FIFO.h:93
int nextIn
Definition: FIFO.h:59
T & operator[](unsigned int index) override
Definition: FIFO.h:52
bool enqueue(T element)
Definition: FIFO.h:69
Definition: FIFO.h:24
const T & operator[](unsigned int index) const override
Definition: FIFO.h:47
void flush()
Definition: FIFO.h:98