FreeRTOS Addons
Loading...
Searching...
No Matches
queue.hpp
1/****************************************************************************
2 *
3 * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4 *
5 * This file is part of the FreeRTOS Add-ons project.
6 *
7 * Source Code:
8 * https://github.com/michaelbecker/freertos-addons
9 *
10 * Project Page:
11 * http://michaelbecker.github.io/freertos-addons/
12 *
13 * On-line Documentation:
14 * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files
18 * (the "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so,subject to the
22 * following conditions:
23 *
24 * + The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
26 * + Credit is appreciated, but not required, if you find this project
27 * useful enough to include in your application, product, device, etc.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 ***************************************************************************/
38
39
40#include "freertos-config.h"
41
42#ifndef QUEUE_HPP_
43#define QUEUE_HPP_
44
52#ifndef CPP_FREERTOS_NO_EXCEPTIONS
53#include <exception>
54#include <cstdio>
55#include <string>
56#ifdef CPP_FREERTOS_NO_CPP_STRINGS
57#error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
58#endif
59#endif
60#include "FreeRTOS.h"
61#include "queue.h"
62
63
64namespace cpp_freertos {
65
66
67#ifndef CPP_FREERTOS_NO_EXCEPTIONS
71class QueueCreateException : public std::exception {
72
73 public:
78 {
79 sprintf(errorString, "Queue Constructor Failed");
80 }
81
85 explicit QueueCreateException(const char *info)
86 {
87 snprintf(errorString, sizeof(errorString),
88 "Queue Constructor Failed %s", info);
89 }
90
95 virtual const char *what() const throw()
96 {
97 return errorString;
98 }
99
100 private:
104 char errorString[80];
105};
106#endif
107
108
117class Queue {
118
120 //
121 // Public API
122 //
124 public:
133 Queue(UBaseType_t maxItems, UBaseType_t itemSize);
134
138 virtual ~Queue();
139
146 virtual bool Enqueue(const void *item);
147
156 virtual bool Enqueue(const void *item, TickType_t Timeout);
157
166 bool Dequeue(void *item, TickType_t Timeout = portMAX_DELAY);
167
177 bool Peek(void *item, TickType_t Timeout = portMAX_DELAY);
178
187 virtual bool EnqueueFromISR(const void *item, BaseType_t *pxHigherPriorityTaskWoken);
188
197 bool DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
198
206 bool PeekFromISR(void *item);
207
213 bool IsEmpty();
214
220 bool IsFull();
221
225 void Flush();
226
231 UBaseType_t NumItems();
232
237 UBaseType_t NumSpacesLeft();
238
240 //
241 // Protected API
242 // Not intended for use by application code.
243 //
245 protected:
249 QueueHandle_t handle;
250};
251
252
262class Deque : public Queue {
263
265 //
266 // Public API
267 //
269 public:
278 Deque(UBaseType_t maxItems, UBaseType_t itemSize);
279
290 bool EnqueueToFront(void *item, TickType_t Timeout = portMAX_DELAY);
291
302 bool EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
303};
304
305
315class BinaryQueue : public Queue {
316
318 //
319 // Public API
320 //
322 public:
330 explicit BinaryQueue(UBaseType_t itemSize);
331
338 virtual bool Enqueue(void *item);
339
348 virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
349};
350
351
352}
353#endif
Definition: queue.hpp:315
virtual bool Enqueue(void *item)
Definition: cqueue.cpp:202
virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:209
Definition: queue.hpp:262
bool EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:186
bool EnqueueToFront(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:176
Definition: queue.hpp:71
virtual const char * what() const
Definition: queue.hpp:95
QueueCreateException(const char *info)
Definition: queue.hpp:85
QueueCreateException()
Definition: queue.hpp:77
Definition: queue.hpp:117
virtual ~Queue()
Definition: cqueue.cpp:60
bool PeekFromISR(void *item)
Definition: cqueue.cpp:126
bool IsFull()
Definition: cqueue.cpp:144
void Flush()
Definition: cqueue.cpp:152
bool Peek(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:96
bool Dequeue(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:86
QueueHandle_t handle
Definition: queue.hpp:249
UBaseType_t NumSpacesLeft()
Definition: cqueue.cpp:164
bool DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:116
virtual bool Enqueue(const void *item)
Definition: cqueue.cpp:66
UBaseType_t NumItems()
Definition: cqueue.cpp:158
virtual bool EnqueueFromISR(const void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:106
bool IsEmpty()
Definition: cqueue.cpp:136
Definition: condition_variable.hpp:57