FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
queue.hpp
Go to the documentation of this file.
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 #ifndef QUEUE_HPP_
41 #define QUEUE_HPP_
42 
50 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
51 #include <exception>
52 #include <cstdio>
53 #include <string>
54 #ifdef CPP_FREERTOS_NO_CPP_STRINGS
55 #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
56 #endif
57 #endif
58 #include "FreeRTOS.h"
59 #include "queue.h"
60 
61 
62 namespace cpp_freertos {
63 
64 
65 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
66 
69 class QueueCreateException : public std::exception {
70 
71  public:
76  {
77  sprintf(errorString, "Queue Constructor Failed");
78  }
79 
83  explicit QueueCreateException(const char *info)
84  {
85  snprintf(errorString, sizeof(errorString),
86  "Queue Constructor Failed %s", info);
87  }
88 
93  virtual const char *what() const throw()
94  {
95  return errorString;
96  }
97 
98  private:
102  char errorString[80];
103 };
104 #endif
105 
106 
115 class Queue {
116 
118  //
119  // Public API
120  //
122  public:
131  Queue(UBaseType_t maxItems, UBaseType_t itemSize);
132 
136  virtual ~Queue();
137 
144  virtual bool Enqueue(void *item);
145 
154  virtual bool Enqueue(void *item, TickType_t Timeout);
155 
164  bool Dequeue(void *item, TickType_t Timeout = portMAX_DELAY);
165 
175  bool Peek(void *item, TickType_t Timeout = portMAX_DELAY);
176 
185  virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
186 
195  bool DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
196 
204  bool PeekFromISR(void *item);
205 
211  bool IsEmpty();
212 
218  bool IsFull();
219 
223  void Flush();
224 
229  UBaseType_t NumItems();
230 
235  UBaseType_t NumSpacesLeft();
236 
238  //
239  // Protected API
240  // Not intended for use by application code.
241  //
243  protected:
247  QueueHandle_t handle;
248 };
249 
250 
260 class Deque : public Queue {
261 
263  //
264  // Public API
265  //
267  public:
276  Deque(UBaseType_t maxItems, UBaseType_t itemSize);
277 
288  bool EnqueueToFront(void *item, TickType_t Timeout = portMAX_DELAY);
289 
300  bool EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
301 };
302 
303 
313 class BinaryQueue : public Queue {
314 
316  //
317  // Public API
318  //
320  public:
328  explicit BinaryQueue(UBaseType_t itemSize);
329 
336  virtual bool Enqueue(void *item);
337 
346  virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
347 };
348 
349 
350 }
351 #endif
QueueHandle_t handle
Definition: queue.hpp:247
QueueCreateException(const char *info)
Definition: queue.hpp:83
virtual const char * what() const
Definition: queue.hpp:93