EtcPal  0.3.0
ETC Platform Abstraction Layer (EtcPal)
View other versions:
queue.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 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  bool Send(const T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
100  bool SendFromIsr(const T& data);
101 
102  bool Receive(T& data, int timeout_ms = ETCPAL_WAIT_FOREVER);
103  template <class Rep, class Period>
104  bool Receive(T& data, const std::chrono::duration<Rep, Period>& timeout);
105  bool IsEmpty() const;
106  bool IsEmptyFromIsr() const;
107 
108 private:
109  etcpal_queue_t queue_{};
110 };
111 
115 template <class T>
116 inline Queue<T>::Queue(size_t size)
117 {
118  etcpal_queue_create(&queue_, size, sizeof(T));
119 }
120 
122 template <class T>
124 {
125  etcpal_queue_destroy(&queue_);
126 }
127 
136 template <class T>
137 inline bool Queue<T>::Send(const T& data, int timeout_ms)
138 {
139  return etcpal_queue_timed_send(&queue_, &data, timeout_ms);
140 }
141 
145 template <class T>
146 inline bool Queue<T>::SendFromIsr(const T& data)
147 {
148  return etcpal_queue_send_from_isr(&queue_, &data);
149 }
150 
155 template <class T>
156 inline bool Queue<T>::Receive(T& data, int timeout_ms)
157 {
158  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms);
159 }
160 
167 template <class T>
168 template <class Rep, class Period>
169 inline bool Queue<T>::Receive(T& data, const std::chrono::duration<Rep, Period>& timeout)
170 {
171  int timeout_ms_clamped =
172  static_cast<int>(std::min(std::chrono::milliseconds(timeout).count(),
173  static_cast<std::chrono::milliseconds::rep>(std::numeric_limits<int>::max())));
174  return etcpal_queue_timed_receive(&queue_, &data, timeout_ms_clamped);
175 }
176 
180 template <class T>
181 inline bool Queue<T>::IsEmpty() const
182 {
183  return etcpal_queue_is_empty(&queue_);
184 };
185 
189 template <class T>
190 inline bool Queue<T>::IsEmptyFromIsr() const
191 {
192  return etcpal_queue_is_empty_from_isr(&queue_);
193 };
194 
195 }; // namespace etcpal
196 
197 #endif // ETCPAL_CPP_RTOS_ERROR_H
An RTOS queue class.
Definition: queue.h:94
~Queue()
Destroy a queue.
Definition: queue.h:123
bool SendFromIsr(const T &data)
Add to a queue from an interrupt service routine.
Definition: queue.h:146
bool IsEmpty() const
Check if a queue is empty.
Definition: queue.h:181
bool IsEmptyFromIsr() const
Check if a queue is empty from an interrupt service routine.
Definition: queue.h:190
bool Send(const T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Add some data to the queue.
Definition: queue.h:137
bool Receive(T &data, int timeout_ms=ETCPAL_WAIT_FOREVER)
Get an item from the queue.
Definition: queue.h:156
Queue(size_t size)
Create a new queue.
Definition: queue.h:116
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.
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.
bool etcpal_queue_send_from_isr(etcpal_queue_t *id, const void *data)
Add an item to a queue from an interrupt context.
bool etcpal_queue_is_empty(const etcpal_queue_t *id)
Determine whether a queue is currently empty.
void etcpal_queue_destroy(etcpal_queue_t *id)
Destroy a queue.
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.
bool etcpal_queue_create(etcpal_queue_t *id, size_t size, size_t item_size)
Create a new queue.
#define ETCPAL_WAIT_FOREVER
For etcpal_ functions that take a millisecond timeout, this means to wait indefinitely.
Definition: common.h:111