emlabcpp
modern opinionated embedded C++ library
owning_coroutine_handle.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include <coroutine>
27 #include <utility>
28 
29 namespace emlabcpp::coro
30 {
31 
32 template < typename PromiseType >
34 {
35 public:
36  using promise_type = PromiseType;
37 
39 
40  explicit owning_coroutine_handle( std::coroutine_handle< promise_type > h )
41  : h_( h )
42  {
43  }
44 
46 
48  {
49  *this = std::move( other );
50  }
51 
53 
55  {
56  std::swap( h_, other.h_ );
57  return *this;
58  }
59 
60  void operator()() const
61  {
62  h_();
63  }
64 
65  [[nodiscard]] constexpr explicit operator bool() const
66  {
67  return static_cast< bool >( h_ );
68  }
69 
70  [[nodiscard]] constexpr bool done() const
71  {
72  return h_.done();
73  }
74 
75  [[nodiscard]] constexpr void* address() const
76  {
77  return h_.address();
78  }
79 
80  constexpr promise_type& promise()
81  {
82  return h_.promise();
83  }
84 
85  [[nodiscard]] constexpr promise_type const& promise() const
86  {
87  return h_.promise();
88  }
89 
91  {
92  if ( h_ )
93  h_.destroy();
94  }
95 
96 private:
97  std::coroutine_handle< promise_type > h_;
98 };
99 
100 } // namespace emlabcpp::coro
Definition: owning_coroutine_handle.h:34
owning_coroutine_handle & operator=(owning_coroutine_handle const &)=delete
void operator()() const
Definition: owning_coroutine_handle.h:60
constexpr bool done() const
Definition: owning_coroutine_handle.h:70
~owning_coroutine_handle()
Definition: owning_coroutine_handle.h:90
owning_coroutine_handle(std::coroutine_handle< promise_type > h)
Definition: owning_coroutine_handle.h:40
owning_coroutine_handle(owning_coroutine_handle const &)=delete
constexpr void * address() const
Definition: owning_coroutine_handle.h:75
owning_coroutine_handle & operator=(owning_coroutine_handle &&other) noexcept
Definition: owning_coroutine_handle.h:54
PromiseType promise_type
Definition: owning_coroutine_handle.h:36
constexpr promise_type & promise()
Definition: owning_coroutine_handle.h:80
constexpr promise_type const & promise() const
Definition: owning_coroutine_handle.h:85
owning_coroutine_handle(owning_coroutine_handle &&other) noexcept
Definition: owning_coroutine_handle.h:47
MIT License.
Definition: data_promise.h:27
void swap(static_vector< T, N > const &lh, static_vector< T, N > const &rh) noexcept
Definition: static_vector.h:294