AEM
staticQ.h
Go to the documentation of this file.
1 #ifndef Q_H
2 #define Q_H
3 
4 #include <Arduino.h>
5 
15 #define Q_OVERRUN_DELETE_OLDEST (false)
16 #define Q_Q_LENGTH (700) // 700 seems to be the max size possible
17 
18 template <class T>
19 class Q{
20  protected:
21  unsigned int pptr = 0,
22  gptr = 0,
23  qNbObj = 0;
24  static const unsigned int qLen = Q_Q_LENGTH;
25  static const boolean overrunDeleteOldest = (boolean) Q_OVERRUN_DELETE_OLDEST;
27  T q[qLen]; //<! define the q as being a vector of objects, physically there, not pointers
28  T nullObject; //<! the null object is needed to be returned when q is empty
29 
30  public:
32  Q(T nullOb) : nullObject(nullOb){
33  }
37  boolean push(T elt){
38  if (qNbObj == qLen){
39  // the q is full, now apply deletion strategy
40  if (overrunDeleteOldest){
41  // in this case pop the oldest and continue with the enqueing of the current elt.
42  //Serial.println("* Q Full! deleting oldest! *");
43  pop();
44  }
45  else{
46  // in this case skip the elt that we are attempted to enqueue and return!
47  //Serial.println("* Q Full! no push! *");
48  return false;
49  }
50  }
51  // this happens if either the Q is not full, or the Q was full and the oldest element has been deleted to make room for one more!
52  q[pptr] = elt;
53  pptr = (pptr+1) % qLen;
54  qNbObj++;
55  return true;
56  }
60  T pop(){
61  T res = nullObject;
62  if (qNbObj){
63  res = q[gptr];
64  gptr = (gptr+1) % qLen;
65  qNbObj--;
66  }
67  return res;
68  }
72  unsigned int qNbObjects() const{
73  return qNbObj;
74  }
78  boolean full() const{
79  return (qNbObj == qLen);
80  }
84  int qLength() const{
85  return qLen;
86  }
87 };
88 
89 #endif
90 
#define Q_Q_LENGTH
Definition: staticQ.h:16
boolean full() const
Definition: staticQ.h:78
T pop()
Definition: staticQ.h:60
T nullObject
Definition: staticQ.h:28
unsigned int qNbObj
Definition: staticQ.h:23
Q(T nullOb)
Definition: staticQ.h:32
unsigned int pptr
Definition: staticQ.h:21
int qLength() const
Definition: staticQ.h:84
static const unsigned int qLen
Definition: staticQ.h:24
T q[qLen]
Definition: staticQ.h:27
static const boolean overrunDeleteOldest
Definition: staticQ.h:25
#define Q_OVERRUN_DELETE_OLDEST
Definition: staticQ.h:15
unsigned int gptr
Definition: staticQ.h:22
boolean push(T elt)
Definition: staticQ.h:37
Definition: staticQ.h:19
unsigned int qNbObjects() const
Definition: staticQ.h:72