zpp
Zephyr C++20 Framework
poll_signal.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_POLL_SIGNAL_HPP
8 #define ZPP_INCLUDE_ZPP_POLL_SIGNAL_HPP
9 
10 #ifdef CONFIG_POLL
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 
15 #include <limits>
16 #include <optional>
17 
18 namespace zpp {
19 
25 template<typename T_PollSignal>
27 public:
28  using native_type = struct k_poll_signal;
31 protected:
35  poll_signal_base() noexcept
36  {
37  }
38 public:
44  std::optional<int> check() noexcept
45  {
46  unsigned int signaled{0};
47  int result{0};
48 
49  k_poll_signal_check(native_handle(), &signaled, &result);
50 
51  if (signaled != 0) {
52  return { result };
53  } else {
54  return {};
55  }
56  }
57 
65  bool raise(int result) noexcept
66  {
67  auto rc = k_poll_signal_raise(native_handle(), result);
68  if (rc == 0) {
69  return true;
70  } else {
71  return false;
72  }
73  }
74 
78  void reset() noexcept
79  {
80  k_poll_signal_reset(native_handle());
81  }
82 
88  auto native_handle() noexcept -> native_pointer
89  {
90  return static_cast<T_PollSignal*>(this)->native_handle();
91  }
92 
98  auto native_handle() const noexcept -> native_const_pointer
99  {
100  return static_cast<const T_PollSignal*>(this)->native_handle();
101  }
102 public:
107 };
108 
113  : public poll_signal_base<poll_signal>
114 {
115 public:
119  poll_signal() noexcept
120  {
121  k_poll_signal_init(&m_signal);
122  }
123 
129  auto native_handle() noexcept -> native_pointer
130  {
131  return &m_signal;
132  }
133 
139  auto native_handle() const noexcept -> native_const_pointer
140  {
141  return &m_signal;
142  }
143 private:
144  native_type m_signal;
145 public:
146  poll_signal(const poll_signal&) = delete;
148  poll_signal& operator=(const poll_signal&) = delete;
150 };
151 
156  : public poll_signal_base<poll_signal_ref>
157 {
158 public:
165  : m_signal_ptr(s)
166  {
167  __ASSERT_NO_MSG(m_signal_ptr != nullptr);
168  }
169 
175  template<typename T_PollSignal>
176  poll_signal_ref(T_PollSignal& s) noexcept
177  : m_signal_ptr(s.native_handle())
178  {
179  __ASSERT_NO_MSG(m_signal_ptr != nullptr);
180  }
181 
188  {
189  m_signal_ptr = s;
190  __ASSERT_NO_MSG(m_signal_ptr != nullptr);
191  return *this;
192  }
193 
199  template<typename T_PollSignal>
200  poll_signal_ref& operator=(T_PollSignal& s) noexcept
201  {
202  m_signal_ptr = s.native_handle();
203  __ASSERT_NO_MSG(m_signal_ptr != nullptr);
204  return *this;
205  }
206 
212  auto native_handle() noexcept -> native_pointer
213  {
214  return m_signal_ptr;
215  }
216 
222  auto native_handle() const noexcept -> native_const_pointer
223  {
224  return m_signal_ptr;
225  }
226 private:
227  native_pointer m_signal_ptr{ nullptr };
228 public:
229  poll_signal_ref() = delete;
230 };
231 
232 } // namespace zpp
233 
234 #endif // CONFIG_POLL
235 
236 #endif // ZPP_INCLUDE_ZPP_POLL_SIGNAL_HPP
CRTP base class for poll_signals.
Definition: poll_signal.hpp:26
std::optional< int > check() noexcept
check if the signal was signaled
Definition: poll_signal.hpp:44
auto native_handle() const noexcept -> native_const_pointer
get the native k_poll_signal handle
Definition: poll_signal.hpp:98
poll_signal_base & operator=(const poll_signal_base &)=delete
native_type * native_pointer
Definition: poll_signal.hpp:29
poll_signal_base(poll_signal_base &&)=delete
void reset() noexcept
reset the state to non-signalled
Definition: poll_signal.hpp:78
poll_signal_base & operator=(poll_signal_base &&)=delete
native_type const * native_const_pointer
Definition: poll_signal.hpp:30
poll_signal_base() noexcept
default contructor only to be used by derived classes
Definition: poll_signal.hpp:35
auto native_handle() noexcept -> native_pointer
get the native k_poll_signal handle
Definition: poll_signal.hpp:88
struct k_poll_signal native_type
Definition: poll_signal.hpp:28
poll_signal_base(const poll_signal_base &)=delete
class wrapping a k_poll_signal
poll_signal_ref & operator=(native_pointer s) noexcept
create a wrapper around a k_poll_signal
auto native_handle() const noexcept -> native_const_pointer
get the native k_poll_signal handle
poll_signal_ref(native_pointer s) noexcept
create a wrapper around a k_poll_signal
poll_signal_ref(T_PollSignal &s) noexcept
create a wrapper around a k_poll_signal
auto native_handle() noexcept -> native_pointer
get the native k_poll_signal handle
poll_signal_ref & operator=(T_PollSignal &s) noexcept
create a wrapper around a k_poll_signal
class owning a k_poll_signal
auto native_handle() noexcept -> native_pointer
get the native k_poll_signal handle
poll_signal() noexcept
default constructor initializing the signal
auto native_handle() const noexcept -> native_const_pointer
get the native k_poll_signal handle
poll_signal(poll_signal &&)=delete
poll_signal & operator=(const poll_signal &)=delete
poll_signal & operator=(poll_signal &&)=delete
poll_signal(const poll_signal &)=delete
result class
Definition: result.hpp:89