EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
static_queue.h
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_STATIC_QUEUE_H
24 #define ETCPAL_CPP_STATIC_QUEUE_H
25 
26 #include "etcpal/common.h"
27 #include "etcpal/cpp/common.h"
28 #include "etcpal/queue.h"
29 
30 #include <algorithm>
31 #include <chrono>
32 #include <cstddef>
33 #include <limits>
34 #include <assert.h>
35 
36 namespace etcpal
37 {
91 
96 template <class T, unsigned N>
98 {
99  static_assert(std::is_trivially_copyable<T>::value, "Type T in etcpal::StaticQueue<T> must be trivially copyable.");
100  static_assert(N > 0, "The number of elements in a static queue must be greater than 0");
101 
102 public:
103  explicit StaticQueue();
104  ~StaticQueue();
105 
106  StaticQueue(const StaticQueue& other) = delete;
107 
108  StaticQueue& operator=(const StaticQueue& other) = delete;
109  StaticQueue(StaticQueue&& other) = delete;
110 
111  StaticQueue& operator=(StaticQueue&& other) = delete;
112 
113  bool Send(const T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
114  bool SendFromIsr(const T& data);
115 
116  bool Receive(T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
117  template <class Rep, class Period>
118  bool Receive(T& data, const std::chrono::duration<Rep, Period>& timeout);
119  bool ReceiveFromIsr(T& data);
120  bool Reset();
121  bool IsEmpty() const;
122  bool IsEmptyFromIsr() const;
123  bool IsFull() const;
124  bool IsFullFromIsr() const;
125  size_t SlotsUsed() const;
126  size_t SlotsUsedFromIsr() const;
127  size_t SlotsAvailable() const;
128 
129 private:
130  etcpal_queue_t queue_{};
131  uint8_t buffer[N * sizeof(T)];
132 };
133 
134 template <class T, unsigned N>
136 {
137  bool true_if_success = false;
138  true_if_success = etcpal_queue_create_static(&queue_, N, sizeof(T), buffer);
139  assert(true_if_success);
140 }
141 
143 template <class T, unsigned N>
145 {
146  etcpal_queue_destroy(&queue_);
147 }
148 
158 template <class T, unsigned N>
159 inline bool StaticQueue<T, N>::Send(const T& data, int timeout_ms)
160 {
161  return etcpal_queue_timed_send(&queue_, &data, timeout_ms);
162 }
163 
167 template <class T, unsigned N>
168 inline bool StaticQueue<T, N>::SendFromIsr(const T& data)
169 {
170  return etcpal_queue_send_from_isr(&queue_, &data);
171 }
172 
178 template <class T, unsigned N>
179 inline bool StaticQueue<T, N>::Receive(T& data, int timeout_ms)
180 {
181  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms);
182 }
183 
191 template <class T, unsigned N>
192 template <class Rep, class Period>
193 inline bool StaticQueue<T, N>::Receive(T& data, const std::chrono::duration<Rep, Period>& timeout)
194 {
195  int timeout_ms_clamped =
196  static_cast<int>(std::min(std::chrono::milliseconds(timeout).count(),
197  static_cast<std::chrono::milliseconds::rep>(std::numeric_limits<int>::max())));
198  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms_clamped);
199 }
200 
205 template <class T, unsigned N>
207 {
208  return etcpal_queue_receive_from_isr(&queue_, &data);
209 }
210 
214 template <class T, unsigned N>
216 {
217  return etcpal_queue_reset(&queue_);
218 }
219 
223 template <class T, unsigned N>
224 inline bool StaticQueue<T, N>::IsEmpty() const
225 {
226  return etcpal_queue_is_empty(&queue_);
227 };
228 
232 template <class T, unsigned N>
234 {
235  return etcpal_queue_is_empty_from_isr(&queue_);
236 };
237 
241 template <class T, unsigned N>
242 inline bool StaticQueue<T, N>::IsFull() const
243 {
244  return etcpal_queue_is_full(&queue_);
245 };
246 
250 template <class T, unsigned N>
252 {
253  return etcpal_queue_is_full_from_isr(&queue_);
254 };
255 
259 template <class T, unsigned N>
260 inline size_t StaticQueue<T, N>::SlotsUsed() const
261 {
262  return etcpal_queue_slots_used(&queue_);
263 };
264 
269 template <class T, unsigned N>
271 {
272  return etcpal_queue_slots_used_from_isr(&queue_);
273 };
274 
278 template <class T, unsigned N>
280 {
281  return etcpal_queue_slots_available(&queue_);
282 };
283 
284 }; // namespace etcpal
285 
286 #endif // ETCPAL_CPP_STATIC_QUEUE_H
An RTOS queue class.
Definition: static_queue.h:98
bool SendFromIsr(const T &data)
Add to a queue from an interrupt service routine.
Definition: static_queue.h:168
~StaticQueue()
Destroy a queue.
Definition: static_queue.h:144
bool Reset()
Resets queue to empty state.
Definition: static_queue.h:215
bool ReceiveFromIsr(T &data)
Get an item from the queue from an interrupt context.
Definition: static_queue.h:206
bool IsEmpty() const
Check if a queue is empty.
Definition: static_queue.h:224
bool IsEmptyFromIsr() const
Check if a queue is empty from an interrupt service routine.
Definition: static_queue.h:233
bool IsFullFromIsr() const
Check if a queue is full from an interrupt service routine.
Definition: static_queue.h:251
bool Send(const T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Add some data to the queue.
Definition: static_queue.h:159
bool Receive(T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Get an item from the queue.
Definition: static_queue.h:179
size_t SlotsUsedFromIsr() const
Get number of slots being stored in the queue from an interrupt service routine.
Definition: static_queue.h:270
size_t SlotsUsed() const
Get number of slots being stored in the queue.
Definition: static_queue.h:260
size_t SlotsAvailable() const
Get number of remaining slots in the queue.
Definition: static_queue.h:279
bool IsFull() const
Check if a queue is full.
Definition: static_queue.h:242
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_create_static(etcpal_queue_t *id, size_t size, size_t item_size, uint8_t *buffer)
Create a new queue with a buffer provided by the user.
Definition: queue.c:184
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
#define ETCPAL_WAIT_FOREVER
For etcpal_ functions that take a millisecond timeout, this means to wait indefinitely.
Definition: common.h:118