promise.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2022 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_PROMISE_HPP
  8. #define BOOST_COBALT_PROMISE_HPP
  9. #include <boost/cobalt/detail/promise.hpp>
  10. namespace boost::cobalt
  11. {
  12. // tag::outline[]
  13. template<typename Return>
  14. struct [[nodiscard]] promise
  15. {
  16. promise(promise &&lhs) noexcept;
  17. promise& operator=(promise && lhs) noexcept;
  18. // enable `co_await`. <1>
  19. auto operator co_await ();
  20. // Ignore the return value, i.e. detach it. <2>
  21. void operator +() &&;
  22. // Cancel the promise.
  23. void cancel(asio::cancellation_type ct = asio::cancellation_type::all);
  24. // Check if the result is ready
  25. bool ready() const;
  26. // Check if the promise can be awaited.
  27. explicit operator bool () const; // <3>
  28. // Detach or attach
  29. bool attached() const;
  30. void detach();
  31. void attach();
  32. // end::outline[]
  33. /* tag::outline[]
  34. // Get the return value. If !ready() this function has undefined behaviour.
  35. Return get();
  36. end::outline[] */
  37. Return get(const boost::source_location & loc = BOOST_CURRENT_LOCATION)
  38. {
  39. BOOST_ASSERT(ready());
  40. return receiver_.get_result().value(loc);
  41. }
  42. using promise_type = detail::cobalt_promise<Return>;
  43. promise(const promise &) = delete;
  44. promise& operator=(const promise &) = delete;
  45. ~promise()
  46. {
  47. if (attached_)
  48. cancel();
  49. }
  50. private:
  51. template<typename>
  52. friend struct detail::cobalt_promise;
  53. promise(detail::cobalt_promise<Return> * promise) : receiver_(promise->receiver, promise->signal), attached_{true}
  54. {
  55. }
  56. detail::promise_receiver<Return> receiver_;
  57. bool attached_;
  58. friend struct detached;
  59. //tag::outline[]
  60. };
  61. // end::outline[]
  62. template<typename T>
  63. inline
  64. promise<T>::promise(promise &&lhs) noexcept
  65. : receiver_(std::move(lhs.receiver_)), attached_(std::exchange(lhs.attached_, false))
  66. {
  67. }
  68. template<typename T>
  69. inline
  70. promise<T>& promise<T>::operator=(promise && lhs) noexcept
  71. {
  72. if (attached_)
  73. cancel();
  74. receiver_ = std::move(lhs.receiver_);
  75. attached_ = std::exchange(lhs.attached_, false);
  76. }
  77. template<typename T>
  78. inline
  79. auto promise<T>::operator co_await () {return receiver_.get_awaitable();}
  80. // Ignore the returns value
  81. template<typename T>
  82. inline
  83. void promise<T>::operator +() && {detach();}
  84. template<typename T>
  85. inline
  86. void promise<T>::cancel(asio::cancellation_type ct)
  87. {
  88. if (!receiver_.done && receiver_.reference == &receiver_)
  89. receiver_.cancel_signal.emit(ct);
  90. }
  91. template<typename T>
  92. inline
  93. bool promise<T>::ready() const { return receiver_.done; }
  94. template<typename T>
  95. inline
  96. promise<T>::operator bool () const
  97. {
  98. return !receiver_.done || !receiver_.result_taken;
  99. }
  100. template<typename T>
  101. inline
  102. bool promise<T>::attached() const {return attached_;}
  103. template<typename T>
  104. inline
  105. void promise<T>::detach() {attached_ = false;}
  106. template<typename T>
  107. inline
  108. void promise<T>::attach() {attached_ = true;}
  109. }
  110. #endif //BOOST_COBALT_PROMISE_HPP