FILO.h
Go to the documentation of this file.
1 /* $Id: FILO.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 FILO / stack class, mostly for primitive types but can be used
9 || | with classes if assignment to int is allowed.
10 || | This FILO 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 #pragma once
20 
21 #include "Countable.h"
22 
23 template <typename T, int rawSize> class FILO : public Countable<T>
24 {
25 public:
26  const int size; // speculative feature, in case it's needed
27 
28  FILO();
29 
30  T pop(); // get next element
31  bool push(T element); // add an element
32  T peek() const; // get the next element without releasing it from the FILO
33  void flush(); // reset to default state
34 
35  // how many elements are currently in the FILO?
36  unsigned int count() const override
37  {
38  return numberOfElements;
39  }
40 
41 private:
42  volatile int numberOfElements;
43  int nextIn;
44  int nextOut;
45  T raw[rawSize];
46 };
47 
48 template <typename T, int rawSize> FILO<T, rawSize>::FILO() : size(rawSize)
49 {
50  flush();
51 }
52 
53 template <typename T, int rawSize> bool FILO<T, rawSize>::push(T element)
54 {
55  if(count() >= rawSize) {
56  return false;
57  }
58  raw[numberOfElements++] = element;
59  return true;
60 }
61 
62 template <typename T, int rawSize> T FILO<T, rawSize>::pop()
63 {
64  if(numberOfElements > 0) {
65  return raw[--numberOfElements];
66  }
67  return raw[0];
68 }
69 
70 template <typename T, int rawSize> T FILO<T, rawSize>::peek() const
71 {
72  if(numberOfElements > 0) {
73  return raw[numberOfElements - 1];
74  }
75  return raw[0];
76 }
77 
78 template <typename T, int rawSize> void FILO<T, rawSize>::flush()
79 {
80  nextIn = nextOut = numberOfElements = 0;
81 }
void flush()
Definition: FILO.h:78
unsigned int count() const override
Definition: FILO.h:36
Definition: Countable.h:19
FILO()
Definition: FILO.h:48
T peek() const
Definition: FILO.h:70
bool push(T element)
Definition: FILO.h:53
Definition: FILO.h:23
const int size
Definition: FILO.h:26
T pop()
Definition: FILO.h:62