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 empty() const
43  {
44  return numberOfElements == 0;
45  }
46 
47  bool full() const
48  {
49  return (count() >= rawSize);
50  }
51 
52  const T& operator[](unsigned int index) const override
53  {
54  return raw[index]; /* unsafe */
55  }
56 
57  T& operator[](unsigned int index) override
58  {
59  return raw[index]; /* unsafe */
60  }
61 
62 protected:
63  volatile int numberOfElements;
64  int nextIn;
65  int nextOut;
66  T raw[rawSize];
67 };
68 
69 template <typename T, int rawSize> FIFO<T, rawSize>::FIFO() : size(rawSize)
70 {
71  flush();
72 }
73 
74 template <typename T, int rawSize> bool FIFO<T, rawSize>::enqueue(T element)
75 {
76  if(full()) {
77  return false;
78  }
79  numberOfElements++;
80  raw[nextIn] = element;
81  // advance to next index, wrap if needed
82  if(++nextIn >= rawSize) {
83  nextIn = 0;
84  }
85  return true;
86 }
87 
88 template <typename T, int rawSize> T FIFO<T, rawSize>::dequeue()
89 {
90  T item;
91  numberOfElements--;
92  item = raw[nextOut];
93  if(++nextOut >= rawSize) // advance to next index, wrap if needed
94  nextOut = 0;
95  return item;
96 }
97 
98 template <typename T, int rawSize> T FIFO<T, rawSize>::peek() const
99 {
100  return raw[nextOut];
101 }
102 
103 template <typename T, int rawSize> void FIFO<T, rawSize>::flush()
104 {
105  nextIn = nextOut = numberOfElements = 0;
106 }
107 
108 #endif
109 // FIFO_H
Definition: Countable.h:20
Definition: FIFO.h:25
T & operator[](unsigned int index) override
Definition: FIFO.h:57
int nextIn
Definition: FIFO.h:64
T raw[rawSize]
Definition: FIFO.h:66
const T & operator[](unsigned int index) const override
Definition: FIFO.h:52
const int size
Definition: FIFO.h:27
unsigned int count() const override
Definition: FIFO.h:37
volatile int numberOfElements
Definition: FIFO.h:63
T peek() const
Definition: FIFO.h:98
T dequeue()
Definition: FIFO.h:88
bool empty() const
Definition: FIFO.h:42
bool enqueue(T element)
Definition: FIFO.h:74
FIFO()
Definition: FIFO.h:69
int nextOut
Definition: FIFO.h:65
bool full() const
Definition: FIFO.h:47
void flush()
Definition: FIFO.h:103