FreeRTOS-Cpp
Loading...
Searching...
No Matches
Queue.hpp
1/*
2 * FreeRTOS-Cpp
3 * Copyright (C) 2021 Jon Enz. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * https://github.com/jonenz/FreeRTOS-Cpp
26 */
27
28#ifndef FREERTOS_QUEUE_HPP
29#define FREERTOS_QUEUE_HPP
30
31#include <optional>
32
33#include "FreeRTOS.h"
34#include "queue.h"
35
36namespace FreeRTOS {
37
49template <class T>
50class QueueBase {
51 public:
52 template <class>
53 friend class Queue;
54
55 template <class, UBaseType_t>
56 friend class StaticQueue;
57
58 QueueBase(const QueueBase&) = delete;
59 QueueBase& operator=(const QueueBase&) = delete;
60
61 static void* operator new(size_t) = delete;
62 static void* operator new[](size_t) = delete;
63
64 static void* operator new(size_t, void* ptr) {
65 return ptr;
66 }
67
68 static void* operator new[](size_t, void* ptr) {
69 return ptr;
70 }
71
81 inline bool isValid() const {
82 return (handle != NULL);
83 }
84
110 inline bool sendToBack(const T& item,
111 const TickType_t ticksToWait = portMAX_DELAY) const {
112 return (xQueueSendToBack(handle, &item, ticksToWait) == pdTRUE);
113 }
114
133 inline bool sendToBackFromISR(bool& higherPriorityTaskWoken,
134 const T& item) const {
135 BaseType_t taskWoken = pdFALSE;
136 bool result =
137 (xQueueSendToBackFromISR(handle, &item, &taskWoken) == pdPASS);
138 if (taskWoken == pdTRUE) {
139 higherPriorityTaskWoken = true;
140 }
141 return result;
142 }
143
154 inline bool sendToBackFromISR(const T& item) const {
155 return (xQueueSendToBackFromISR(handle, &item, NULL) == pdPASS);
156 }
157
178 inline bool sendToFront(const T& item,
179 const TickType_t ticksToWait = portMAX_DELAY) const {
180 return (xQueueSendToFront(handle, &item, ticksToWait) == pdTRUE);
181 }
182
201 inline bool sendToFrontFromISR(bool& higherPriorityTaskWoken,
202 const T& item) const {
203 BaseType_t taskWoken = pdFALSE;
204 bool result =
205 (xQueueSendToFrontFromISR(handle, &item, &taskWoken) == pdPASS);
206 if (taskWoken == pdTRUE) {
207 higherPriorityTaskWoken = true;
208 }
209 return result;
210 }
211
222 inline bool sendToFrontFromISR(const T& item) const {
223 return (xQueueSendToFrontFromISR(handle, &item, NULL) == pdPASS);
224 }
225
250 inline std::optional<T> receive(
251 const TickType_t ticksToWait = portMAX_DELAY) const {
252 T buffer;
253 return (xQueueReceive(handle, &buffer, ticksToWait) == pdTRUE)
254 ? std::optional<T>(buffer)
255 : std::nullopt;
256 }
257
279 inline std::optional<T> receiveFromISR(bool& higherPriorityTaskWoken) const {
280 T buffer;
281 BaseType_t taskWoken = pdFALSE;
282 bool result = (xQueueReceiveFromISR(handle, &buffer, &taskWoken) == pdTRUE);
283 if (taskWoken == pdTRUE) {
284 higherPriorityTaskWoken = true;
285 }
286 return result ? std::optional<T>(buffer) : std::nullopt;
287 }
288
300 inline std::optional<T> receiveFromISR() const {
301 T buffer;
302 return (xQueueReceiveFromISR(handle, &buffer, NULL) == pdTRUE)
303 ? std::optional<T>(buffer)
304 : std::nullopt;
305 }
306
319 inline UBaseType_t messagesWaiting() const {
320 return uxQueueMessagesWaiting(handle);
321 }
322
336 inline UBaseType_t messagesWaitingFromISR() const {
337 return uxQueueMessagesWaitingFromISR(handle);
338 }
339
352 inline UBaseType_t spacesAvailable() const {
353 return uxQueueSpacesAvailable(handle);
354 }
355
366 inline void reset() const {
367 xQueueReset(handle);
368 }
369
392 inline void overwrite(const T& item) const {
393 xQueueOverwrite(handle, &item);
394 }
395
422 inline void overwriteFromISR(bool& higherPriorityTaskWoken,
423 const T& item) const {
424 BaseType_t taskWoken = pdFALSE;
425 xQueueOverwriteFromISR(handle, &item, &taskWoken);
426 if (taskWoken == pdTRUE) {
427 higherPriorityTaskWoken = true;
428 }
429 }
430
442 inline void overwriteFromISR(const T& item) const {
443 xQueueOverwriteFromISR(handle, &item, NULL);
444 }
445
475 inline std::optional<T> peek(
476 const TickType_t ticksToWait = portMAX_DELAY) const {
477 T buffer;
478 return (xQueuePeek(handle, &buffer, ticksToWait) == pdTRUE)
479 ? std::optional<T>(buffer)
480 : std::nullopt;
481 }
482
502 inline std::optional<T> peekFromISR() const {
503 T buffer;
504 return (xQueuePeekFromISR(handle, &buffer) == pdTRUE)
505 ? std::optional<T>(buffer)
506 : std::nullopt;
507 }
508
538 inline void addToRegistry(const char* name) const {
539 vQueueAddToRegistry(handle, name);
540 }
541
557 inline void unregister() const {
558 vQueueUnregisterQueue(handle);
559 }
560
576 inline const char* getName() const {
577 return pcQueueGetName(handle);
578 }
579
594 inline bool isFullFromISR() const {
595 return (xQueueIsQueueFullFromISR(handle) == pdTRUE);
596 }
597
612 inline bool isEmptyFromISR() const {
613 return (xQueueIsQueueEmptyFromISR(handle) == pdTRUE);
614 }
615
616 private:
626 QueueBase() = default;
627
640 vQueueDelete(this->handle);
641 }
642
643 QueueBase(QueueBase&&) noexcept = default;
644 QueueBase& operator=(QueueBase&&) noexcept = default;
645
649 QueueHandle_t handle = NULL;
650};
651
652#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
653
666template <class T>
667class Queue : public QueueBase<T> {
668 public:
687 explicit Queue(const UBaseType_t length) {
688 this->handle = xQueueCreate(length, sizeof(T));
689 }
690 ~Queue() = default;
691
692 Queue(const Queue&) = delete;
693 Queue& operator=(const Queue&) = delete;
694
695 Queue(Queue&&) noexcept = default;
696 Queue& operator=(Queue&&) noexcept = default;
697};
698
699#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
700
701#if (configSUPPORT_STATIC_ALLOCATION == 1)
702
715template <class T, UBaseType_t N>
716class StaticQueue : public QueueBase<T> {
717 public:
736 this->handle = xQueueCreateStatic(N, sizeof(T), storage, &staticQueue);
737 }
738 ~StaticQueue() = default;
739
740 StaticQueue(const StaticQueue&) = delete;
741 StaticQueue& operator=(const StaticQueue&) = delete;
742
743 StaticQueue(StaticQueue&&) noexcept = default;
744 StaticQueue& operator=(StaticQueue&&) noexcept = default;
745
746 private:
747 StaticQueue_t staticQueue;
748 uint8_t storage[N * sizeof(T)];
749};
750
751#endif /* configSUPPORT_STATIC_ALLOCATION */
752
753} // namespace FreeRTOS
754
755#endif // FREERTOS_QUEUE_HPP
Base class that provides the standard queue interface to FreeRTOS::Queue and FreeRTOS::StaticQueue.
Definition Queue.hpp:50
bool sendToBackFromISR(bool &higherPriorityTaskWoken, const T &item) const
Function that calls xQueueSendToBackFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
Definition Queue.hpp:133
bool isFullFromISR() const
Function that calls BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
Definition Queue.hpp:594
bool sendToFrontFromISR(const T &item) const
Function that calls xQueueSendToFrontFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
Definition Queue.hpp:222
std::optional< T > peekFromISR() const
Function that calls BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void *pvBuffer )
Definition Queue.hpp:502
void overwriteFromISR(bool &higherPriorityTaskWoken, const T &item) const
Function that calls BaseType_t xQueueOverwriteFromISR( QueueHandle_t xQueue, const void * pvItemToQue...
Definition Queue.hpp:422
QueueHandle_t handle
Handle used to refer to the queue when using the FreeRTOS interface.
Definition Queue.hpp:649
bool sendToBackFromISR(const T &item) const
Function that calls xQueueSendToBackFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
Definition Queue.hpp:154
void reset() const
Function that calls BaseType_t xQueueReset( QueueHandle_t xQueue )
Definition Queue.hpp:366
UBaseType_t messagesWaitingFromISR() const
Function that calls UBaseType_t uxQueueMessagesWaitingFromISR( QueueHandle_t xQueue )
Definition Queue.hpp:336
std::optional< T > receiveFromISR(bool &higherPriorityTaskWoken) const
Function that calls BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer,...
Definition Queue.hpp:279
UBaseType_t messagesWaiting() const
Function that calls UBaseType_t uxQueueMessagesWaiting( QueueHandle_t xQueue )
Definition Queue.hpp:319
UBaseType_t spacesAvailable() const
Function that calls UBaseType_t uxQueueSpacesAvailable( QueueHandle_t xQueue )
Definition Queue.hpp:352
std::optional< T > receive(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls BaseType_t xQueueReceive( QueueHandle_t xQueue, void *pvBuffer,...
Definition Queue.hpp:250
QueueBase()=default
Construct a new QueueBase object.
void addToRegistry(const char *name) const
Function that calls void vQueueAddToRegistry( QueueHandle_t xQueue, char *pcQueueName )
Definition Queue.hpp:538
std::optional< T > receiveFromISR() const
Function that calls BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer,...
Definition Queue.hpp:300
void unregister() const
Function that calls void vQueueUnregisterQueue( QueueHandle_t xQueue )
Definition Queue.hpp:557
void overwriteFromISR(const T &item) const
Function that calls BaseType_t xQueueOverwriteFromISR( QueueHandle_t xQueue, const void * pvItemToQue...
Definition Queue.hpp:442
bool sendToBack(const T &item, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xQueueSendToBack( xQueue, pvItemToQueue, xTicksToWait )
Definition Queue.hpp:110
const char * getName() const
Function that calls const char *pcQueueGetName( QueueHandle_t xQueue )
Definition Queue.hpp:576
void overwrite(const T &item) const
Function that calls BaseType_t xQueueOverwrite( QueueHandle_t xQueue, const void * pvItemToQueue )
Definition Queue.hpp:392
bool sendToFrontFromISR(bool &higherPriorityTaskWoken, const T &item) const
Function that calls xQueueSendToFrontFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
Definition Queue.hpp:201
bool isEmptyFromISR() const
Function that calls BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
Definition Queue.hpp:612
std::optional< T > peek(const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls BaseType_t xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer,...
Definition Queue.hpp:475
bool sendToFront(const T &item, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls xQueueSendToFront( xQueue, pvItemToQueue, xTicksToWait )
Definition Queue.hpp:178
bool isValid() const
Function that checks if the underlying queue handle is not NULL. This should be used to ensure a queu...
Definition Queue.hpp:81
~QueueBase()
Destroy the QueueBase object by calling void vQueueDelete( QueueHandle_t xQueue )
Definition Queue.hpp:639
Class that encapsulates the functionality of a FreeRTOS queue.
Definition Queue.hpp:667
Queue(const UBaseType_t length)
Construct a new Queue object by calling QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength,...
Definition Queue.hpp:687
Class that encapsulates the functionality of a FreeRTOS queue.
Definition Queue.hpp:716
StaticQueue()
Construct a new StaticQueue object by calling QueueHandle_t xQueueCreateStatic( UBaseType_t uxQueueLe...
Definition Queue.hpp:735