-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppQueue.h
196 lines (170 loc) · 7.81 KB
/
cppQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*!\file cppQueue.h
** \author SMFSW
** \copyright BSD 3-Clause License (c) 2017-2019, SMFSW
** \brief Queue handling library (designed on Arduino)
** \details Queue handling library (designed on Arduino)
** This library was designed for Arduino, yet may be compiled without change with gcc for other purposes/targets
**/
/****************************************************************/
#ifndef __CPPQUEUE_H
#define __CPPQUEUE_H
#include <inttypes.h>
/****************************************************************/
#define QUEUE_INITIALIZED 0x5AA5 //!< Queue initialized control value
/*!\enum enumQueueType
** \brief Queue behavior enumeration (FIFO, LIFO)
**/
typedef enum enumQueueType {
FIFO = 0, //!< First In First Out behavior
LIFO = 1 //!< Last In First Out behavior
} QueueType;
/*! \class Queue
** \brief Class containing the required methods for handling the queue
**/
class Queue
{
private:
QueueType impl; //!< Queue implementation: FIFO LIFO
bool ovw; //!< Overwrite previous records when queue is full allowed
uint16_t rec_nb; //!< number of records in the queue
uint16_t rec_sz; //!< Size of a record
uint32_t queue_sz; //!< Size of the full queue
uint8_t * queue; //!< Queue start pointer (when allocated)
uint16_t in; //!< number of records pushed into the queue
uint16_t out; //!< number of records pulled from the queue (only for FIFO)
uint16_t cnt; //!< number of records not retrieved from the queue
uint16_t init; //!< set to QUEUE_INITIALIZED after successful init of the queue, 0 otherwise
public:
/*! \brief Queue constructor
** \param [in] size_rec - size of a record in the queue
** \param [in] nb_recs - number of records in the queue
** \param [in] type - Queue implementation type: FIFO, LIFO
** \param [in] overwrite - Overwrite previous records when queue is full
** \return nothing
**/
Queue(const uint16_t size_rec, const uint16_t nb_recs=20, const QueueType type=FIFO, const bool overwrite=false);
/*! \brief Queue destructor: release dynamically allocated queue
**/
~Queue();
/*! \brief Flush queue, restarting from empty queue
**/
void flush(void);
/*! \brief Clean queue, restarting from empty queue
** \deprecated clean was already used in Queue lib, alias is made to keep compatibility with earlier versions
**/
inline void clean(void) __attribute__((always_inline)) {
flush(); }
/*! \brief get initialization state of the queue
** \return Queue initialization status
** \retval true if queue is allocated
** \retval false is queue is not allocated
**/
inline bool isInitialized(void) __attribute__((always_inline)) {
return (init == QUEUE_INITIALIZED) ? true : false; }
/*! \brief get emptiness state of the queue
** \return Queue emptiness status
** \retval true if queue is empty
** \retval false is not empty
**/
inline bool isEmpty(void) __attribute__((always_inline)) {
return (!cnt) ? true : false; }
/*! \brief get fullness state of the queue
** \return Queue fullness status
** \retval true if queue is full
** \retval false is not full
**/
inline bool isFull(void) __attribute__((always_inline)) {
return (cnt == rec_nb) ? true : false; }
/*! \brief get size of queue
** \remark Size in bytes (like sizeof)
** \return Size of queue in bytes
**/
inline uint32_t sizeOf(void) __attribute__((always_inline)) {
return queue_sz; }
/*! \brief get number of records in the queue
** \return Number of records stored in the queue
**/
inline uint16_t getCount(void) __attribute__((always_inline)) {
return cnt; }
/*! \brief get number of records in the queue (same as getCount)
** \deprecated nbRecs was already used in Queue lib, alias is made to keep compatibility with earlier versions
** \return Number of records stored in the queue
**/
inline uint16_t nbRecs(void) __attribute__((always_inline)) {
return getCount(); }
/*! \brief get number of records left in the queue
** \return Number of records left in the queue
**/
inline uint16_t getRemainingCount(void) __attribute__((always_inline)) {
return rec_nb - cnt; }
/*! \brief Push record to queue
** \param [in] record - pointer to record to be pushed into queue
** \return Push status
** \retval true if successfully pushed into queue
** \retval false if queue is full
**/
bool push(const void * const record) __attribute__((nonnull));
/*! \brief Pop record from queue
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \param [in,out] record - pointer to record to be popped from queue
** \return Pop status
** \retval true if successfully popped from queue
** \retval false if queue is empty
**/
bool pop(void * const record) __attribute__((nonnull));
/*! \brief Pull record from queue (same as pop)
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \deprecated pull was already used in Queue lib, alias is made to keep compatibility with earlier versions
** \param [in,out] record - pointer to record to be pulled from queue
** \return Pull status
** \retval true if successfully pulled from queue
** \retval false if queue is empty
**/
inline bool pull(void * const record) __attribute__((nonnull,always_inline)) {
return pop(record); }
/*! \brief Peek record from queue
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \note This function is most likely to be used in conjunction with drop
** \param [in,out] record - pointer to record to be peeked from queue
** \return Peek status
** \retval true if successfully peeked from queue
** \retval false if queue is empty
**/
bool peek(void * const record) __attribute__((nonnull));
/*! \brief Drop current record from queue
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \note This method is most likely to be used in conjunction with peek
** \return drop status
** \retval true if successfully dropped from queue
** \retval false if queue is empty
**/
bool drop(void);
/*! \brief Peek record at index from queue
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \note This function is only useful if searching for a duplicate record and shouldn't be used in conjunction with drop
** \param [in,out] record - pointer to record to be peeked from queue
** \param [in] idx - index of the record to pick
** \return Peek status
** \retval true if successfully peeked from queue
** \retval false if index is out of range
**/
bool peekIdx(void * const record, const uint16_t idx) __attribute__((nonnull));
/*! \brief Peek previous record from queue
** \warning If using push, pop, peek, drop, peekItem and/or peekPrevious in both interrupts and main application,
** you shall disable interrupts in main application when using these functions
** \note This inline is only useful with FIFO implementation, use peek instead with a LIFO (will lead to the same result)
** \param [in,out] record - pointer to record to be peeked from queue
** \return Peek status
** \retval true if successfully peeked from queue
** \retval false if queue is empty
**/
inline bool peekPrevious(void * const record) __attribute__((nonnull,always_inline)) {
const uint16_t idx = getCount() - 1; // No worry about count - 1 when queue is empty, test is done by peekIdx
return peekIdx(record, idx); }
};
#endif /* __CPPQUEUE_H */