unique_handle.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // Copyright (c) 2023 Klemens Morgenstern ([email protected])
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_HANDLE_HPP
  8. #define BOOST_COBALT_HANDLE_HPP
  9. #include <boost/asio/associator.hpp>
  10. #include <coroutine>
  11. #include <memory>
  12. namespace boost::cobalt
  13. {
  14. template<typename T>
  15. struct unique_handle
  16. {
  17. unique_handle() noexcept = default;
  18. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  19. explicit unique_handle(T * promise,
  20. const boost::source_location & loc = BOOST_CURRENT_LOCATION) noexcept : handle_(promise), loc_(loc) {}
  21. #else
  22. explicit unique_handle(T * promise) noexcept : handle_(promise) {}
  23. #endif
  24. unique_handle(std::nullptr_t) noexcept {}
  25. std::coroutine_handle<T> release()
  26. {
  27. return std::coroutine_handle<T>::from_promise(*handle_.release());
  28. }
  29. void* address() const noexcept { return get_handle_().address(); }
  30. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  31. static unique_handle from_address(
  32. void* a,
  33. const boost::source_location & loc = BOOST_CURRENT_LOCATION) noexcept
  34. {
  35. unique_handle res;
  36. res.loc_ = loc;
  37. res.handle_.reset(&std::coroutine_handle<T>::from_address(a).promise());
  38. return res;
  39. }
  40. #else
  41. static unique_handle from_address(void* a) noexcept
  42. {
  43. unique_handle res;
  44. res.handle_.reset(&std::coroutine_handle<T>::from_address(a).promise());
  45. return res;
  46. }
  47. #endif
  48. bool done() const noexcept { return get_handle_().done(); }
  49. explicit operator bool() const { return static_cast<bool>(handle_); }
  50. void destroy() { handle_.reset(); }
  51. void operator()() const &
  52. {
  53. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  54. BOOST_ASIO_HANDLER_LOCATION((loc_.file_name(), loc_.line(), loc_.function_name()));
  55. #endif
  56. resume();
  57. }
  58. void resume() const & { get_handle_().resume(); }
  59. void operator()() &&
  60. {
  61. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  62. BOOST_ASIO_HANDLER_LOCATION((loc_.file_name(), loc_.line(), loc_.function_name()));
  63. #endif
  64. release().resume();
  65. }
  66. void resume() && { release().resume(); }
  67. T & promise() {return *handle_;}
  68. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  69. constexpr static unique_handle from_promise(
  70. T &p,
  71. const boost::source_location & loc = BOOST_CURRENT_LOCATION) noexcept
  72. {
  73. unique_handle res;
  74. res.loc_ = loc;
  75. res.handle_.reset(&p);
  76. return res;
  77. }
  78. #else
  79. constexpr static unique_handle from_promise(T &p) noexcept
  80. {
  81. unique_handle res;
  82. res.handle_.reset(&p);
  83. return res;
  84. }
  85. #endif
  86. T & operator*() {return *handle_;}
  87. const T & operator*() const {return *handle_;}
  88. T * operator->() {return handle_.get();}
  89. const T * operator->() const {return handle_.get();}
  90. T * get() {return handle_.get();}
  91. const T * get() const {return handle_.get();}
  92. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  93. void reset(T * handle = nullptr,
  94. const boost::source_location & loc = BOOST_CURRENT_LOCATION)
  95. {
  96. loc_ = loc; handle_.reset(handle);
  97. }
  98. #else
  99. void reset(T * handle = nullptr) {handle_.reset(handle);}
  100. #endif
  101. friend
  102. auto operator==(const unique_handle & h, std::nullptr_t) {return h.handle_ == nullptr;}
  103. friend
  104. auto operator!=(const unique_handle & h, std::nullptr_t) {return h.handle_ != nullptr;}
  105. private:
  106. struct deleter_
  107. {
  108. void operator()(T * p)
  109. {
  110. std::coroutine_handle<T>::from_promise(*p).destroy();
  111. }
  112. };
  113. std::coroutine_handle<T> get_handle_() const
  114. {
  115. return std::coroutine_handle<T>::from_promise(*handle_);
  116. }
  117. std::unique_ptr<T, deleter_> handle_;
  118. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  119. boost::source_location loc_;
  120. #endif
  121. };
  122. template<>
  123. struct unique_handle<void>
  124. {
  125. unique_handle() noexcept = default;
  126. unique_handle(std::nullptr_t) noexcept {}
  127. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  128. explicit unique_handle(void * handle,
  129. const boost::source_location & loc = BOOST_CURRENT_LOCATION) noexcept : handle_(handle), loc_(loc) {}
  130. #else
  131. explicit unique_handle(void * handle) noexcept : handle_(handle) {}
  132. #endif
  133. std::coroutine_handle<void> release()
  134. {
  135. return std::coroutine_handle<void>::from_address(handle_.release());
  136. }
  137. void* address() const noexcept { return get_handle_().address(); }
  138. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  139. static unique_handle<void> from_address(void* a,
  140. const boost::source_location & loc = BOOST_CURRENT_LOCATION) noexcept
  141. {
  142. unique_handle res;
  143. res.loc_ = loc;
  144. res.handle_.reset(std::coroutine_handle<void>::from_address(a).address());
  145. return res;
  146. }
  147. #else
  148. static unique_handle<void> from_address(void* a) noexcept
  149. {
  150. unique_handle res;
  151. res.handle_.reset(std::coroutine_handle<void>::from_address(a).address());
  152. return res;
  153. }
  154. #endif
  155. explicit operator bool() const { return static_cast<bool>(handle_); }
  156. bool done() const noexcept { return get_handle_().done(); }
  157. void operator()() const &
  158. {
  159. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  160. BOOST_ASIO_HANDLER_LOCATION((loc_.file_name(), loc_.line(), loc_.function_name()));
  161. #endif
  162. resume();
  163. }
  164. void resume() const & { get_handle_().resume(); }
  165. void operator()() &&
  166. {
  167. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  168. BOOST_ASIO_HANDLER_LOCATION((loc_.file_name(), loc_.line(), loc_.function_name()));
  169. #endif
  170. release().resume();
  171. }
  172. void resume() && { release().resume(); }
  173. void destroy() { handle_.reset(); }
  174. void * get() { return handle_.get(); }
  175. const void * get() const { return handle_.get(); }
  176. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  177. void reset(void * handle = nullptr,
  178. const boost::source_location & loc = BOOST_CURRENT_LOCATION)
  179. {
  180. loc_ = loc;
  181. handle_.reset(handle);
  182. }
  183. #else
  184. void reset(void * handle = nullptr) {handle_.reset(handle);}
  185. #endif
  186. friend
  187. auto operator==(const unique_handle & h, std::nullptr_t) {return h.handle_ == nullptr;}
  188. friend
  189. auto operator!=(const unique_handle & h, std::nullptr_t) {return h.handle_ != nullptr;}
  190. private:
  191. struct deleter_
  192. {
  193. void operator()(void * p)
  194. {
  195. std::coroutine_handle<void>::from_address(p).destroy();
  196. }
  197. };
  198. std::coroutine_handle<void> get_handle_() const
  199. {
  200. return std::coroutine_handle<void>::from_address(handle_.get());
  201. }
  202. std::unique_ptr<void, deleter_> handle_;
  203. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  204. boost::source_location loc_;
  205. #endif
  206. };
  207. template<>
  208. struct unique_handle<std::noop_coroutine_promise>
  209. {
  210. unique_handle() noexcept = default;
  211. unique_handle(std::nullptr_t) noexcept {}
  212. std::coroutine_handle<void> release()
  213. {
  214. return std::noop_coroutine();
  215. }
  216. void* address() const noexcept { return std::noop_coroutine().address(); }
  217. bool done() const noexcept { return true;}
  218. void operator()() const {}
  219. void resume() const {}
  220. void destroy() {}
  221. explicit operator bool() const { return true; }
  222. struct executor_type
  223. {
  224. template<typename Fn>
  225. void execute(Fn &&) const {}
  226. };
  227. executor_type get_executor() const {return {}; }
  228. friend
  229. auto operator==(const unique_handle &, std::nullptr_t) {return false;}
  230. friend
  231. auto operator!=(const unique_handle &, std::nullptr_t) {return true;}
  232. };
  233. }
  234. namespace boost::asio
  235. {
  236. template <template <typename, typename> class Associator,
  237. typename Promise, typename DefaultCandidate>
  238. requires (!std::is_void_v<Promise>)
  239. struct associator<Associator,
  240. boost::cobalt::unique_handle<Promise>, DefaultCandidate>
  241. : Associator<Promise, DefaultCandidate>
  242. {
  243. static typename Associator<Promise, DefaultCandidate>::type
  244. get(const boost::cobalt::unique_handle<Promise>& h) BOOST_ASIO_NOEXCEPT
  245. {
  246. return Associator<Promise, DefaultCandidate>::get(*h);
  247. }
  248. static BOOST_ASIO_AUTO_RETURN_TYPE_PREFIX2(
  249. typename Associator<Handler, DefaultCandidate>::type)
  250. get(const boost::cobalt::unique_handle<Promise>& h,
  251. const DefaultCandidate& c) BOOST_ASIO_NOEXCEPT
  252. BOOST_ASIO_AUTO_RETURN_TYPE_SUFFIX((
  253. Associator<Promise, DefaultCandidate>::get(*h, c)))
  254. {
  255. return Associator<Promise, DefaultCandidate>::get(*h, c);
  256. }
  257. };
  258. }
  259. #endif //BOOST_COBALT_HANDLE_HPP