YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
promise_type.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <exception>
10
11namespace yaclib::detail {
12
13template <typename V, typename E, bool Lazy>
14class PromiseType;
15
16struct Destroy final {
17 constexpr bool await_ready() const noexcept {
18 return false;
19 }
20
21 template <typename Promise>
22 YACLIB_INLINE auto await_suspend(yaclib_std::coroutine_handle<Promise> handle) const noexcept {
23 auto& promise = handle.promise();
24#if YACLIB_FINAL_SUSPEND_TRANSFER != 0
25 return promise.template SetResult<true>();
26#elif YACLIB_SYMMETRIC_TRANSFER != 0
27 return promise.template SetResult<true>().resume();
28#else
29 return Loop(&promise, promise.template SetResult<false>());
30#endif
31 }
32
33 constexpr void await_resume() const noexcept {
34 }
35};
36
37template <bool Lazy>
39 template <typename V, typename E>
40 static void Delete(ResultCore<V, E>& core) noexcept;
41};
42
43template <typename V, typename E, bool Lazy>
44class PromiseType final : public OneCounter<ResultCore<V, E>, PromiseTypeDeleter<Lazy>> {
46
47 public:
49 } // get_return_object is gonna be invoked right after ctor
50
52 if constexpr (Lazy) {
54 } else {
56 }
57 }
58
60 if constexpr (Lazy) {
61 return yaclib_std::suspend_always{};
62 } else {
63 return yaclib_std::suspend_never{};
64 }
65 }
66
68 return {};
69 }
70
72 this->Store(std::current_exception());
73 }
74
75 template <typename Value>
76 void return_value(Value&& value) noexcept(std::is_nothrow_constructible_v<Result<V, E>, Value&&>) {
77 this->Store(std::forward<Value>(value));
78 }
79
80 void return_value(Unit) noexcept {
81 this->Store(std::in_place);
82 }
83
85 auto handle = yaclib_std::coroutine_handle<PromiseType>::from_promise(*this);
87 return handle;
88 }
89
90 private:
91 void DecRef() noexcept final {
92 this->Sub(1);
93 }
94
95 void Call() noexcept final {
96 auto next = Curr();
97 next.resume();
98 }
99
100 void Drop() noexcept final {
101 this->Store(StopTag{});
102#if YACLIB_SYMMETRIC_TRANSFER != 0
103 this->template SetResult<true>().resume();
104#else
105 Loop(this, this->template SetResult<false>());
106#endif
107 }
108
109 YACLIB_INLINE void Impl(InlineCore& caller) noexcept {
110 this->_executor = std::move(DownCast<BaseCore>(caller)._executor);
111 YACLIB_ASSERT(this->_executor != nullptr);
112 }
113 [[nodiscard]] InlineCore* Here(InlineCore& caller) noexcept final {
114 Impl(caller);
115 Call();
116 return nullptr;
117 }
118#if YACLIB_SYMMETRIC_TRANSFER != 0
119 [[nodiscard]] yaclib_std::coroutine_handle<> Next(InlineCore& caller) noexcept final {
120 Impl(caller);
121 return Curr();
122 }
123#endif
124
125 [[nodiscard]] yaclib_std::coroutine_handle<> Curr() noexcept final {
126 auto handle = Handle();
127 YACLIB_ASSERT(!handle.done());
128 return handle;
129 }
130};
131
132template <bool Lazy>
133template <typename V, typename E>
136 auto handle = promise.Handle();
137 handle.destroy();
138}
139
140} // namespace yaclib::detail
Provides a mechanism to access the result of async operations.
Definition future.hpp:199
A intrusive pointer to objects with an embedded reference count.
Provides a mechanism to schedule the some async operations TODO(MBkkt) add description.
Definition task.hpp:25
void unhandled_exception() noexcept
void return_value(Unit) noexcept
auto get_return_object() noexcept
void return_value(Value &&value) noexcept(std::is_nothrow_constructible_v< Result< V, E >, Value && >)
Destroy final_suspend() noexcept
auto initial_suspend() noexcept
void Store(Args &&... args) noexcept(std::is_nothrow_constructible_v< Result< V, E >, Args &&... >)
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
YACLIB_INLINE void Loop(InlineCore *prev, InlineCore *curr) noexcept
Definition base_core.hpp:69
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_INLINE auto await_suspend(yaclib_std::coroutine_handle< Promise > handle) const noexcept
constexpr void await_resume() const noexcept
constexpr bool await_ready() const noexcept
static void Delete(ResultCore< V, E > &core) noexcept