EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
queue.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_QUEUE_H
24 #define ETCPAL_CPP_QUEUE_H
25 
26 #include <algorithm>
27 #include <chrono>
28 #include <cstddef>
29 #include <limits>
30 #include "etcpal/common.h"
31 #include "etcpal/queue.h"
32 #include "etcpal/cpp/common.h"
33 
34 namespace etcpal
35 {
87 
92 template <class T>
93 class Queue
94 {
95 public:
96  explicit Queue(size_t size);
97  ~Queue();
98 
99  Queue(const Queue& other) = delete;
100  Queue& operator=(const Queue& other) = delete;
101  Queue(Queue&& other) = delete;
102  Queue& operator=(Queue&& other) = delete;
103 
104  bool Send(const T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
105  bool SendFromIsr(const T& data);
106 
107  bool Receive(T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
108  template <class Rep, class Period>
109  bool Receive(T& data, const std::chrono::duration<Rep, Period>& timeout);
110  bool ReceiveFromIsr(T& data);
111  bool Reset();
112  bool IsEmpty() const;
113  bool IsEmptyFromIsr() const;
114  bool IsFull() const;
115  bool IsFullFromIsr() const;
116  size_t SlotsUsed() const;
117  size_t SlotsUsedFromIsr() const;
118  size_t SlotsAvailable() const;
119 
120 private:
121  etcpal_queue_t queue_{};
122 };
123 
127 template <class T>
128 inline Queue<T>::Queue(size_t size)
129 {
130  etcpal_queue_create(&queue_, size, sizeof(T));
131 }
132 
134 template <class T>
136 {
137  etcpal_queue_destroy(&queue_);
138 }
139 
148 template <class T>
149 inline bool Queue<T>::Send(const T& data, int timeout_ms)
150 {
151  return etcpal_queue_timed_send(&queue_, &data, timeout_ms);
152 }
153 
157 template <class T>
158 inline bool Queue<T>::SendFromIsr(const T& data)
159 {
160  return etcpal_queue_send_from_isr(&queue_, &data);
161 }
162 
167 template <class T>
168 inline bool Queue<T>::Receive(T& data, int timeout_ms)
169 {
170  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms);
171 }
172 
179 template <class T>
180 template <class Rep, class Period>
181 inline bool Queue<T>::Receive(T& data, const std::chrono::duration<Rep, Period>& timeout)
182 {
183  int timeout_ms_clamped =
184  static_cast<int>(std::min(std::chrono::milliseconds(timeout).count(),
185  static_cast<std::chrono::milliseconds::rep>(std::numeric_limits<int>::max())));
186  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms_clamped);
187 }
188 
192 template <class T>
193 inline bool Queue<T>::ReceiveFromIsr(T& data)
194 {
195  return etcpal_queue_receive_from_isr(&queue_, &data);
196 }
197 
201 template <class T>
202 inline bool Queue<T>::Reset()
203 {
204  return etcpal_queue_reset(&queue_);
205 }
206 
210 template <class T>
211 inline bool Queue<T>::IsEmpty() const
212 {
213  return etcpal_queue_is_empty(&queue_);
214 };
215 
219 template <class T>
220 inline bool Queue<T>::IsEmptyFromIsr() const
221 {
222  return etcpal_queue_is_empty_from_isr(&queue_);
223 };
224 
228 template <class T>
229 inline bool Queue<T>::IsFull() const
230 {
231  return etcpal_queue_is_full(&queue_);
232 };
233 
237 template <class T>
238 inline bool Queue<T>::IsFullFromIsr() const
239 {
240  return etcpal_queue_is_full_from_isr(&queue_);
241 };
242 
246 template <class T>
247 inline size_t Queue<T>::SlotsUsed() const
248 {
249  return etcpal_queue_slots_used(&queue_);
250 };
251 
255 template <class T>
256 inline size_t Queue<T>::SlotsUsedFromIsr() const
257 {
258  return etcpal_queue_slots_used_from_isr(&queue_);
259 };
260 
264 template <class T>
265 inline size_t Queue<T>::SlotsAvailable() const
266 {
267  return etcpal_queue_slots_available(&queue_);
268 };
269 
270 }; // namespace etcpal
271 
272 #endif // ETCPAL_CPP_RTOS_ERROR_H
An RTOS queue class.
Definition: queue.h:94
~Queue()
Destroy a queue.
Definition: queue.h:135
bool SendFromIsr(const T &data)
Add to a queue from an interrupt service routine.
Definition: queue.h:158
bool Reset()
Resets queue to empty state.
Definition: queue.h:202
bool ReceiveFromIsr(T &data)
Get an item from the queue from an interrupt context.
Definition: queue.h:193
bool IsEmpty() const
Check if a queue is empty.
Definition: queue.h:211
bool IsEmptyFromIsr() const
Check if a queue is empty from an interrupt service routine.
Definition: queue.h:220
bool IsFullFromIsr() const
Check if a queue is full from an interrupt service routine.
Definition: queue.h:238
bool Send(const T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Add some data to the queue.
Definition: queue.h:149
bool Receive(T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Get an item from the queue.
Definition: queue.h:168
Queue(size_t size)
Create a new queue.
Definition: queue.h:128
size_t SlotsUsedFromIsr() const
Get number of slots being stored in the queue from an interrupt service routine.
Definition: queue.h:256
size_t SlotsUsed() const
Get number of slots being stored in the queue.
Definition: queue.h:247
size_t SlotsAvailable() const
Get number of remaining slots in the queue.
Definition: queue.h:265
bool IsFull() const
Check if a queue is full.
Definition: queue.h:229
Common definitions used by EtcPal C++ wrappers.
bool etcpal_queue_is_empty_from_isr(const etcpal_queue_t *id)
Determine whether a queue is currently empty from an interrupt context.
Definition: queue.c:298
size_t etcpal_queue_slots_available(const etcpal_queue_t *id)
Get number of remaining slots in the queue.
Definition: queue.c:349
bool etcpal_queue_timed_receive(etcpal_queue_t *id, void *data, int timeout_ms)
Retrieve the first item from a queue, giving up after a timeout.
Definition: queue.c:247
bool etcpal_queue_send_from_isr(etcpal_queue_t *id, const void *data)
Add an item to a queue from an interrupt context.
Definition: queue.c:256
bool etcpal_queue_is_full_from_isr(const etcpal_queue_t *id)
Determine whether a queue is currently full from an interrupt context.
Definition: queue.c:318
bool etcpal_queue_is_empty(const etcpal_queue_t *id)
Determine whether a queue is currently empty.
Definition: queue.c:289
PLATFORM_DEFINED etcpal_queue_t
The queue identifier.
Definition: queue.dox:82
bool etcpal_queue_is_full(const etcpal_queue_t *id)
Determine whether a queue is currently full.
Definition: queue.c:309
bool etcpal_queue_reset(const etcpal_queue_t *id)
Resets queue to empty state.
bool etcpal_queue_receive_from_isr(etcpal_queue_t *id, void *data)
Retrieve the first item from a queue from an interrupt context.
Definition: queue.c:265
void etcpal_queue_destroy(etcpal_queue_t *id)
Destroy a queue.
Definition: queue.c:191
size_t etcpal_queue_slots_used(const etcpal_queue_t *id)
Get number of slots being stored in the queue.
Definition: queue.c:329
size_t etcpal_queue_slots_used_from_isr(const etcpal_queue_t *id)
Get number of slots being stored in the queue from an interrupt context.
Definition: queue.c:339
bool etcpal_queue_timed_send(etcpal_queue_t *id, const void *data, int timeout_ms)
Add an item to a queue, giving up after a timeout.
Definition: queue.c:229
bool etcpal_queue_create(etcpal_queue_t *id, size_t size, size_t item_size)
Create a new queue.
Definition: queue.c:142
#define ETCPAL_WAIT_FOREVER
For etcpal_ functions that take a millisecond timeout, this means to wait indefinitely.
Definition: common.h:111