| eRPC API Reference Rev. 1.8.1 NXP Semiconductors

- Main Page
- API Reference
- Classes
- Files

- File List
- File Members

erpc_static_queue.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3 * Copyright 2016 NXP
4 * All rights reserved.
5 *
6 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 */
9
10 #ifndef __embedded_rpc__static_queue__
11 #define __embedded_rpc__static_queue__
12
13 #include <cstring>
14
21 // Classes
24
25 namespace erpc {
31 template <class T, uint32_t elementCount>
32 class StaticQueue
33 {
34 public:
40 StaticQueue(void)
41 {
42 m_capacity = elementCount;
43 m_head = 0;
44 m_tail = 0;
45 }
46
52 ~StaticQueue(void) {}
61 bool add(T element)
62 {
63 if ((m_head + 1) % m_capacity != m_tail)
64 {
65 std::memcpy(m_storage[m_head], &element, sizeof(T));
66 m_head = (m_head + 1) % m_capacity;
67 return true;
68 }
69 return false;
70 }
71
80 bool get(T *element)
81 {
82 if (m_tail != m_head)
83 {
84 std::memcpy(element, m_storage[m_tail], sizeof(T));
85 m_tail = (m_tail + 1) % m_capacity;
86 return true;
87 }
88 return false;
89 }
90
96 int size(void)
97 {
98 if (m_head >= m_tail)
99 {
100 return m_head - m_tail;
101 }
102 return (m_capacity - m_tail + m_head);
103 }
104
105 protected:
106 uint64_t m_storage[elementCount]
107 [(sizeof(T) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
109 uint32_t m_capacity;
110 uint32_t volatile m_head;
111 uint32_t volatile m_tail;
112 };
113
114 } // namespace erpc
115
118 #endif // defined(__embedded_rpc__static_queue__)
erpc::StaticQueue::m_head
uint32_t volatile m_head
Definition: erpc_static_queue.h:110
erpc::StaticQueue::StaticQueue
StaticQueue(void)
Constructor of StaticQueue class.
Definition: erpc_static_queue.h:40
erpc::StaticQueue::size
int size(void)
This function returns number of elements in queue.
Definition: erpc_static_queue.h:96
erpc
Definition: erpc_arbitrated_client_manager.h:25
erpc::StaticQueue::add
bool add(T element)
This function adds element to queue.
Definition: erpc_static_queue.h:61
erpc::StaticQueue::m_tail
uint32_t volatile m_tail
Definition: erpc_static_queue.h:111
erpc::StaticQueue::~StaticQueue
~StaticQueue(void)
Destructor of StaticQueue class.
Definition: erpc_static_queue.h:52
erpc::StaticQueue::m_storage
uint64_t m_storage[elementCount][(sizeof(T)+sizeof(uint64_t)-1)/sizeof(uint64_t)]
Definition: erpc_static_queue.h:107
erpc::StaticQueue
Template class which implements static queue as ring buffer.
Definition: erpc_static_queue.h:32
erpc::StaticQueue::m_capacity
uint32_t m_capacity
Definition: erpc_static_queue.h:109