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>
24 class FILO : public Countable<T>
25 {
26  public:
27  const int size; // speculative feature, in case it's needed
28 
29  FILO();
30 
31  T pop(); // get next element
32  bool push(T element); // add an element
33  T peek() const; // get the next element without releasing it from the FILO
34  void flush(); // reset to default state
35 
36  // how many elements are currently in the FILO?
37  unsigned int count() const override
38  {
39  return numberOfElements;
40  }
41 
42  private:
43  volatile int numberOfElements;
44  int nextIn;
45  int nextOut;
46  T raw[rawSize];
47 };
48 
49 template<typename T, int rawSize>
51 {
52  flush();
53 }
54 
55 template<typename T, int rawSize>
56 bool FILO<T, rawSize>::push(T element)
57 {
58  if (count() >= rawSize)
59  {
60  return false;
61  }
62  raw[numberOfElements++] = element;
63  return true;
64 }
65 
66 template<typename T, int rawSize>
68 {
69  if (numberOfElements > 0)
70  {
71  return raw[--numberOfElements];
72  }
73  return raw[0];
74 }
75 
76 template<typename T, int rawSize>
78 {
79  if (numberOfElements > 0)
80  {
81  return raw[numberOfElements - 1];
82  }
83  return raw[0];
84 }
85 
86 template<typename T, int rawSize>
88 {
89  nextIn = nextOut = numberOfElements = 0;
90 }
void flush()
Definition: FILO.h:87
unsigned int count() const override
Definition: FILO.h:37
Definition: Countable.h:20
FILO()
Definition: FILO.h:50
T peek() const
Definition: FILO.h:77
bool push(T element)
Definition: FILO.h:56
Definition: FILO.h:24
const int size
Definition: FILO.h:27
T pop()
Definition: FILO.h:67