47 #define SQ_GUARD_CREATE(name) wpp::WppGuard name
55 #define SQ_GUARD_LOCK(name) name.lock()
63 #define SQ_GUARD_UNLOCK(name) name.unlock()
72 #define SQ_GUARD_TRY_LOCK(name) name.try_lock()
75 #define SAFE_QUEUE_DEF_ELEM_CNT 0x01
98 template <
typename T,
size_t SIZE>
209 bool at(
size_t i, T* data);
267 SQ_GUARD_CREATE(_push_guard);
268 SQ_GUARD_CREATE(_pop_guard);
269 size_t _front_i = 0x00, _end_i = 0x00, _counter = 0x00;
276 template <
typename T,
size_t SIZE>
281 if (!elements_cnt || _check_add_ability(elements_cnt) ==
false) {
286 for (
size_t i = 0; i < elements_cnt; i++) {
287 memcpy((uint8_t *)(_buffer + _end_i), (uint8_t *)(data + i),
sizeof(T));
288 _end_i = _get_next_index(_end_i);
296 template <
typename T,
size_t SIZE>
301 if (_check_remove_ability(elements_cnt) ==
false) {
306 _front_i = _get_next_index(_front_i, elements_cnt);
307 _counter -= elements_cnt;
313 template <
typename T,
size_t SIZE>
318 if (!elements_cnt || elements_cnt > size()) {
320 return std::vector<T>();
323 size_t end_of_range_index = _get_next_index(_front_i, elements_cnt);
324 uint8_t is_out_of_range = _front_i >= end_of_range_index;
334 std::vector<T> converted (_buffer + _front_i, (is_out_of_range)? _buffer + SIZE : _buffer + end_of_range_index);
335 if (is_out_of_range) converted.insert(converted.end(), _buffer, _buffer + end_of_range_index);
341 template <
typename T,
size_t SIZE>
343 if (is_empty())
return NULL;
344 return _buffer + _front_i;
347 template <
typename T,
size_t SIZE>
349 if (is_empty())
return NULL;
350 return _buffer + _get_prev_index(_end_i);
353 template <
typename T,
size_t SIZE>
355 if (is_empty() || i >= size())
return NULL;
356 return _buffer + ((_front_i + i) % SIZE);
359 template <
typename T,
size_t SIZE>
363 if (!data || is_empty()) {
367 memcpy((uint8_t *)data, (uint8_t *)(_buffer + _front_i),
sizeof(T));
373 template <
typename T,
size_t SIZE>
377 if (!data || is_empty()) {
381 memcpy((uint8_t *)data, (uint8_t *)(_buffer + _get_prev_index(_end_i)),
sizeof(T));
387 template <
typename T,
size_t SIZE>
391 if (!data || is_empty() || i >= size()) {
395 memcpy((uint8_t *)data, (uint8_t *)(_buffer + ((_front_i + i) % SIZE)),
sizeof(T));
401 template <
typename T,
size_t SIZE>
406 template <
typename T,
size_t SIZE>
408 return _counter == 0x00;
411 template <
typename T,
size_t SIZE>
413 return _counter == SIZE;
416 template <
typename T,
size_t SIZE>
421 template <
typename T,
size_t SIZE>
423 return SIZE - _counter;
426 template <
typename T,
size_t SIZE>
433 template <
typename T,
size_t SIZE>
435 return (base_index + elements_cnt) % SIZE;
438 template <
typename T,
size_t SIZE>
440 return (base_index >= elements_cnt)? (base_index - elements_cnt) : (SIZE - (elements_cnt - base_index));
443 template <
typename T,
size_t SIZE>
445 return size() >= elements_cnt;
448 template <
typename T,
size_t SIZE>
450 return (SIZE - size()) >= elements_cnt;
#define SQ_GUARD_LOCK(name)
Macro for locking the specified guard object. The lock() method is used to acquire the lock on the gu...
#define SQ_GUARD_UNLOCK(name)
Macro for unlocking the specified guard object. The unlock() method is used to release the lock on th...
#define SQ_GUARD_TRY_LOCK(name)
Macro for trying to lock the specified guard object. The try_lock() method is used to attempt to acqu...
#define SAFE_QUEUE_DEF_ELEM_CNT
SafeQueue is an implementation of the thread/IRQ safe queue that does not use dynamic memory,...
bool front(T *data)
Returns the first element in the queue and copies it to the specified memory location....
void clear()
Clears the queue.
T * at(size_t i)
Returns a pointer to the element at the specified index in the queue. The at() method does not guaran...
T * front()
Returns a pointer to the first element in the queue. The front() method does not guarantee that the p...
size_t available_space()
Returns the number of additional elements the queue can hold.
uint32_t element_size()
Returns the size of each element in the queue.
bool push(const T *const data, size_t elements_cnt=SAFE_QUEUE_DEF_ELEM_CNT)
Adds an element or an array of elements to the queue. The push() method can be simultaneously called ...
bool is_empty()
Checks if the queue is empty.
size_t size()
Returns the current size of the queue.
bool back(T *data)
Returns the last element in the queue and copies it to the specified memory location....
bool at(size_t i, T *data)
Returns the element at the specified index in the queue and copies it to the specified memory locatio...
T * back()
Returns a pointer to the last element in the queue. The back() method does not guarantee that the poi...
std::vector< T > to_vector(size_t elements_cnt=SIZE)
Converts the contents of the queue to a vector. The to_vector() method creates a vector containing a ...
bool pop(size_t elements_cnt=SAFE_QUEUE_DEF_ELEM_CNT)
Removes a specified number of elements from the queue. The pop() method can be simultaneously called ...
bool is_full()
Checks if the queue is full.