EtcPal  HEAD (unstable)
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  static_assert(std::is_trivially_copyable<T>::value, "Type T in etcpal::Queue<T> must be trivially copyable.");
96 
97 public:
98  explicit Queue(size_t size);
99  ~Queue();
100 
101  Queue(const Queue& other) = delete;
102  Queue& operator=(const Queue& other) = delete;
103  Queue(Queue&& other) = delete;
104  Queue& operator=(Queue&& other) = delete;
105 
106  bool Send(const T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
107  bool SendFromIsr(const T& data);
108 
109  bool Receive(T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
110  template <class Rep, class Period>
111  bool Receive(T& data, const std::chrono::duration<Rep, Period>& timeout);
112  bool ReceiveFromIsr(T& data);
113  bool Reset();
114  bool IsEmpty() const;
115  bool IsEmptyFromIsr() const;
116  bool IsFull() const;
117  bool IsFullFromIsr() const;
118  size_t SlotsUsed() const;
119  size_t SlotsUsedFromIsr() const;
120  size_t SlotsAvailable() const;
121 
122 private:
123  etcpal_queue_t queue_{};
124 };
125 
129 template <class T>
130 inline Queue<T>::Queue(size_t size)
131 {
132  etcpal_queue_create(&queue_, size, sizeof(T));
133 }
134 
136 template <class T>
138 {
139  etcpal_queue_destroy(&queue_);
140 }
141 
150 template <class T>
151 inline bool Queue<T>::Send(const T& data, int timeout_ms)
152 {
153  return etcpal_queue_timed_send(&queue_, &data, timeout_ms);
154 }
155 
159 template <class T>
160 inline bool Queue<T>::SendFromIsr(const T& data)
161 {
162  return etcpal_queue_send_from_isr(&queue_, &data);
163 }
164 
169 template <class T>
170 inline bool Queue<T>::Receive(T& data, int timeout_ms)
171 {
172  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms);
173 }
174 
181 template <class T>
182 template <class Rep, class Period>
183 inline bool Queue<T>::Receive(T& data, const std::chrono::duration<Rep, Period>& timeout)
184 {
185  int timeout_ms_clamped =
186  static_cast<int>(std::min(std::chrono::milliseconds(timeout).count(),
187  static_cast<std::chrono::milliseconds::rep>(std::numeric_limits<int>::max())));
188  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms_clamped);
189 }
190 
194 template <class T>
195 inline bool Queue<T>::ReceiveFromIsr(T& data)
196 {
197  return etcpal_queue_receive_from_isr(&queue_, &data);
198 }
199 
203 template <class T>
204 inline bool Queue<T>::Reset()
205 {
206  return etcpal_queue_reset(&queue_);
207 }
208 
212 template <class T>
213 inline bool Queue<T>::IsEmpty() const
214 {
215  return etcpal_queue_is_empty(&queue_);
216 };
217 
221 template <class T>
222 inline bool Queue<T>::IsEmptyFromIsr() const
223 {
224  return etcpal_queue_is_empty_from_isr(&queue_);
225 };
226 
230 template <class T>
231 inline bool Queue<T>::IsFull() const
232 {
233  return etcpal_queue_is_full(&queue_);
234 };
235 
239 template <class T>
240 inline bool Queue<T>::IsFullFromIsr() const
241 {
242  return etcpal_queue_is_full_from_isr(&queue_);
243 };
244 
248 template <class T>
249 inline size_t Queue<T>::SlotsUsed() const
250 {
251  return etcpal_queue_slots_used(&queue_);
252 };
253 
257 template <class T>
258 inline size_t Queue<T>::SlotsUsedFromIsr() const
259 {
260  return etcpal_queue_slots_used_from_isr(&queue_);
261 };
262 
266 template <class T>
267 inline size_t Queue<T>::SlotsAvailable() const
268 {
269  return etcpal_queue_slots_available(&queue_);
270 };
271 
272 }; // namespace etcpal
273 
274 #endif // ETCPAL_CPP_RTOS_ERROR_H
An RTOS queue class.
Definition: queue.h:94
~Queue()
Destroy a queue.
Definition: queue.h:137
bool SendFromIsr(const T &data)
Add to a queue from an interrupt service routine.
Definition: queue.h:160
bool Reset()
Resets queue to empty state.
Definition: queue.h:204
bool ReceiveFromIsr(T &data)
Get an item from the queue from an interrupt context.
Definition: queue.h:195
bool IsEmpty() const
Check if a queue is empty.
Definition: queue.h:213
bool IsEmptyFromIsr() const
Check if a queue is empty from an interrupt service routine.
Definition: queue.h:222
bool IsFullFromIsr() const
Check if a queue is full from an interrupt service routine.
Definition: queue.h:240
bool Send(const T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Add some data to the queue.
Definition: queue.h:151
bool Receive(T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Get an item from the queue.
Definition: queue.h:170
Queue(size_t size)
Create a new queue.
Definition: queue.h:130
size_t SlotsUsedFromIsr() const
Get number of slots being stored in the queue from an interrupt service routine.
Definition: queue.h:258
size_t SlotsUsed() const
Get number of slots being stored in the queue.
Definition: queue.h:249
size_t SlotsAvailable() const
Get number of remaining slots in the queue.
Definition: queue.h:267
bool IsFull() const
Check if a queue is full.
Definition: queue.h:231
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:375
size_t etcpal_queue_slots_available(const etcpal_queue_t *id)
Get number of remaining slots in the queue.
Definition: queue.c:441
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:309
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:321
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:401
bool etcpal_queue_is_empty(const etcpal_queue_t *id)
Determine whether a queue is currently empty.
Definition: queue.c:363
PLATFORM_DEFINED etcpal_queue_t
The queue identifier.
Definition: queue.dox:85
bool etcpal_queue_is_full(const etcpal_queue_t *id)
Determine whether a queue is currently full.
Definition: queue.c:389
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:333
void etcpal_queue_destroy(etcpal_queue_t *id)
Destroy a queue.
Definition: queue.c:241
size_t etcpal_queue_slots_used(const etcpal_queue_t *id)
Get number of slots being stored in the queue.
Definition: queue.c:415
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:428
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:285
bool etcpal_queue_create(etcpal_queue_t *id, size_t size, size_t item_size)
Create a new queue.
Definition: queue.c:194
#define ETCPAL_WAIT_FOREVER
For etcpal_ functions that take a millisecond timeout, this means to wait indefinitely.
Definition: common.h:118