zpp
Zephyr C++20 Framework
fifo.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #ifndef ZPP_INCLUDE_ZPP_FIFO_HPP
8 #define ZPP_INCLUDE_ZPP_FIFO_HPP
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
12 
13 #include <chrono>
14 #include <limits>
15 #include <type_traits>
16 #include <cstddef>
17 
18 namespace zpp {
19 
26 template<template<typename> typename T_BaseFifoType, typename T_BaseItemType>
27 class fifo_base {
28 public:
29  using native_type = struct k_fifo;
32 
33  using item_type = T_BaseItemType;
35  using item_const_pointer = item_type const *;
36 protected:
40  fifo_base() noexcept
41  {
42  static_assert(std::is_standard_layout_v<item_type>);
43  static_assert(std::is_same_v<void*, decltype(item_type::fifo_reserved)>);
44  static_assert(offsetof(item_type, fifo_reserved) == 0);
45  }
46 public:
52  [[nodiscard]] constexpr auto native_handle() noexcept -> native_pointer
53  {
54  return static_cast<T_BaseFifoType<item_type>*>(this)->native_handle();
55  }
56 
62  [[nodiscard]] constexpr auto native_handle() const noexcept -> native_const_pointer
63  {
64  return static_cast<const T_BaseFifoType<item_type>*>(this)->native_handle();
65  }
66 
70  void cancel_wait() noexcept
71  {
72  k_fifo_cancel_wait(native_handle());
73  }
74 
80  void push_back(item_pointer item) noexcept
81  {
82  k_fifo_put(native_handle(), item);
83  }
84 
90  [[nodiscard]] item_pointer
91  pop_front() noexcept
92  {
93  return static_cast<item_pointer>(
94  k_fifo_get(native_handle(), K_FOREVER));
95  }
96 
102  [[nodiscard]] item_pointer
103  try_pop_front() noexcept
104  {
105  return static_cast<item_pointer>(
106  k_fifo_get(native_handle(), K_NO_WAIT));
107  }
108 
116  template <class T_Rep, class T_Period>
117  [[nodiscard]] item_pointer
118  try_pop_front_for(const std::chrono::duration<T_Rep, T_Period>& timeout) noexcept
119  {
120  using namespace std::chrono;
121 
122  return static_cast<item_pointer>(
123  k_fifo_get(native_handle(),
124  duration_cast<milliseconds>(timeout).count()));
125  }
126 
132  [[nodiscard]] item_pointer
133  front() noexcept
134  {
135  return static_cast<item_pointer>(
136  k_fifo_peek_head(native_handle()));
137  }
138 
144  [[nodiscard]] item_pointer back() noexcept
145  {
146  return static_cast<item_pointer>(
147  k_fifo_peek_tail(native_handle()));
148  }
149 
155  [[nodiscard]] bool empty() noexcept
156  {
157  auto res = k_fifo_is_empty(native_handle());
158  if (res == 0) {
159  return false;
160  } else {
161  return true;
162  }
163  }
164 public:
165  fifo_base(const fifo_base&) = delete;
166  fifo_base(fifo_base&&) = delete;
167  fifo_base& operator=(const fifo_base&) = delete;
169 };
170 
174 template<typename T_ItemType>
175 class fifo : public fifo_base<fifo, T_ItemType> {
176 public:
180 public:
184  fifo() noexcept
185  {
186  k_fifo_init(&m_fifo);
187  }
188 
194  [[nodiscard]] constexpr auto native_handle() noexcept -> native_pointer
195  {
196  return &m_fifo;
197  }
198 
204  [[nodiscard]] constexpr auto native_handle() const noexcept -> native_const_pointer
205  {
206  return &m_fifo;
207  }
208 private:
209  native_type m_fifo;
210 public:
211  fifo(const fifo&) = delete;
212  fifo(fifo&&) = delete;
213  fifo& operator=(const fifo&) = delete;
214  fifo& operator=(fifo&&) = delete;
215 };
216 
220 template<typename T_ItemType>
221 class fifo_ref : public fifo_base<fifo_ref, T_ItemType> {
222 public:
226 public:
234  constexpr explicit fifo_ref(native_pointer f) noexcept
235  : m_fifo_ptr(f)
236  {
237  __ASSERT_NO_MSG(m_fifo_ptr != nullptr);
238  }
239 
247  template<template<class> class T_Fifo>
248  constexpr explicit fifo_ref(T_Fifo<T_ItemType>& f) noexcept
249  : m_fifo_ptr(f.native_handle())
250  {
251  __ASSERT_NO_MSG(m_fifo_ptr != nullptr);
252  }
253 
263  constexpr fifo_ref& operator=(native_pointer f) noexcept
264  {
265  m_fifo_ptr = f;
266  __ASSERT_NO_MSG(m_fifo_ptr != nullptr);
267  return *this;
268  }
269 
279  template<template<class> class T_Fifo>
280  constexpr fifo_ref& operator=(const T_Fifo<T_ItemType>& f) noexcept
281  {
282  m_fifo_ptr = f.native_handle();
283  __ASSERT_NO_MSG(m_fifo_ptr != nullptr);
284  return *this;
285  }
286 
292  [[nodiscard]] constexpr auto native_handle() noexcept -> native_pointer
293  {
294  return m_fifo_ptr;
295  }
296 
302  [[nodiscard]] constexpr auto native_handle() const noexcept -> native_const_pointer
303  {
304  return m_fifo_ptr;
305  }
306 private:
307  native_pointer m_fifo_ptr{ nullptr };
308 public:
309  fifo_ref() = delete;
310 };
311 
312 } // namespace zpp
313 
314 #endif // ZPP_INCLUDE_ZPP_FIFO_HPP
Fifo CRTP base class.
Definition: fifo.hpp:27
item_type * item_pointer
Definition: fifo.hpp:34
fifo_base(fifo_base &&)=delete
constexpr auto native_handle() noexcept -> native_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:52
fifo_base() noexcept
default constructor, can only be called from derived types
Definition: fifo.hpp:40
constexpr auto native_handle() const noexcept -> native_const_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:62
item_pointer try_pop_front() noexcept
try to pop item from the fifo without waiting
Definition: fifo.hpp:103
fifo_base(const fifo_base &)=delete
fifo_base & operator=(const fifo_base &)=delete
item_type const * item_const_pointer
Definition: fifo.hpp:35
item_pointer pop_front() noexcept
pop item from fifo waiting for ever
Definition: fifo.hpp:91
native_type * native_pointer
Definition: fifo.hpp:30
item_pointer try_pop_front_for(const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
try to pop item from the fifo waiting a certain amount of time
Definition: fifo.hpp:118
struct k_fifo native_type
Definition: fifo.hpp:29
void cancel_wait() noexcept
force a waiting thread to return with a timeout error
Definition: fifo.hpp:70
item_pointer front() noexcept
get item at the front without removing it from the fifo
Definition: fifo.hpp:133
void push_back(item_pointer item) noexcept
push an item on the back of the fifo
Definition: fifo.hpp:80
item_pointer back() noexcept
get item at the back without removing it from the fifo
Definition: fifo.hpp:144
bool empty() noexcept
check if the fifo is empty
Definition: fifo.hpp:155
T_BaseItemType item_type
Definition: fifo.hpp:33
native_type const * native_const_pointer
Definition: fifo.hpp:31
fifo_base & operator=(fifo_base &&)=delete
fifo that references a k_fifo object
Definition: fifo.hpp:221
constexpr fifo_ref & operator=(const T_Fifo< T_ItemType > &f) noexcept
Reference another fifo object.
Definition: fifo.hpp:280
constexpr auto native_handle() const noexcept -> native_const_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:302
constexpr fifo_ref & operator=(native_pointer f) noexcept
Reference another fifo object.
Definition: fifo.hpp:263
fifo_ref()=delete
constexpr auto native_handle() noexcept -> native_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:292
constexpr fifo_ref(native_pointer f) noexcept
wrap k_fifo
Definition: fifo.hpp:234
constexpr fifo_ref(T_Fifo< T_ItemType > &f) noexcept
Reference another fifo object.
Definition: fifo.hpp:248
fifo that manages a k_fifo object
Definition: fifo.hpp:175
fifo & operator=(const fifo &)=delete
fifo(const fifo &)=delete
fifo & operator=(fifo &&)=delete
fifo() noexcept
create new fifo
Definition: fifo.hpp:184
fifo(fifo &&)=delete
constexpr auto native_handle() const noexcept -> native_const_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:204
constexpr auto native_handle() noexcept -> native_pointer
get the Zephyr native fifo handle
Definition: fifo.hpp:194