YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <yaclib/fwd.hpp>
11
12namespace yaclib {
13namespace detail {
14
15void Start(BaseCore* head, IExecutor& e) noexcept;
16void Start(BaseCore* head) noexcept;
17
18} // namespace detail
19
20/**
21 * Provides a mechanism to schedule the some async operations
22 * TODO(MBkkt) add description
23 */
24template <typename V, typename E>
25class Task final {
26 public:
27 static_assert(Check<V>(), "V should be valid");
28 static_assert(Check<E>(), "E should be valid");
29 static_assert(!std::is_same_v<V, E>, "Task cannot be instantiated with same V and E, because it's ambiguous");
30
31 Task(const Task&) = delete;
32 Task& operator=(const Task&) = delete;
33
34 Task(Task&& other) noexcept = default;
35 Task& operator=(Task&& other) noexcept = default;
36
39 if (Valid()) {
40 std::move(*this).Cancel();
41 }
42 }
43
45 return _core != nullptr;
46 }
47
48 /**
49 * Check that \ref Result that corresponds to this \ref Task is computed
50 *
51 * \return false if the \ref Result of this \ref Task is not computed yet, otherwise true
52 */
55 return !_core->Empty();
56 }
57
58 /**
59 * Do nothing, just for compatibility with FutureOn
60 * TODO(MBkkt) think about force On/Detach/ToFuture:
61 * It's able to set passed executor to previous nullptr/all/head/etc or replace
62 */
63 Task<V, E> On(std::nullptr_t) && noexcept {
64 return {std::move(this->_core)};
65 }
66
67 template <typename Func>
68 /*Task*/ auto Then(IExecutor& e, Func&& f) && {
69 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyOn>(_core, &e, std::forward<Func>(f));
70 }
71 template <typename Func>
72 /*Task*/ auto ThenInline(Func&& f) && {
73 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyInline>(_core, nullptr,
74 std::forward<Func>(f));
75 }
76 template <typename Func>
77 /*Task*/ auto Then(Func&& f) && {
78 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyOn>(_core, nullptr,
79 std::forward<Func>(f));
80 }
81
82 void Cancel() && {
83 std::move(*this).Detach(MakeInline(StopTag{}));
84 }
85
86 void Detach() && noexcept {
88 auto* core = _core.Release();
89 core->StoreCallback(detail::MakeDrop());
91 }
92 void Detach(IExecutor& e) && noexcept {
94 auto* core = _core.Release();
95 core->StoreCallback(detail::MakeDrop());
97 }
98
99 Future<V, E> ToFuture() && noexcept {
100 detail::Start(_core.Get());
101 return {std::move(_core)};
102 }
104 detail::Start(_core.Get(), e);
105 return {std::move(_core)};
106 }
107
108 Result<V, E> Get() && noexcept {
109 // TODO(MBkkt) make it better: we can remove concurrent atomic changes from here
110 return std::move(*this).ToFuture().Get();
111 }
112
113 void Touch() & = delete;
114 void Touch() const&& = delete;
115
118 return _core->Get();
119 }
120 Result<V, E> Touch() && noexcept {
122 auto core = std::exchange(_core, nullptr);
123 return std::move(core->Get());
124 }
125
126 /**
127 * Method that get internal Core state
128 *
129 * \return internal Core state ptr
130 */
132 return _core;
133 }
135 }
136
137 private:
138 detail::ResultCorePtr<V, E> _core;
139};
140
141extern template class Task<>;
142
143} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:232
Provides a mechanism to access the result of async operations.
Definition future.hpp:199
A intrusive pointer to objects with an embedded reference count.
Encapsulated return value from caller.
Definition result.hpp:90
Provides a mechanism to schedule the some async operations TODO(MBkkt) add description.
Definition task.hpp:25
Task & operator=(const Task &)=delete
detail::ResultCorePtr< V, E > & GetCore() noexcept
Method that get internal Core state.
Definition task.hpp:131
Result< V, E > Touch() &&noexcept
Definition task.hpp:120
auto ThenInline(Func &&f) &&
Definition task.hpp:72
bool Ready() const &noexcept
Check that Result that corresponds to this Task is computed.
Definition task.hpp:53
void Touch() const &&=delete
auto Then(Func &&f) &&
Definition task.hpp:77
Task< V, E > On(std::nullptr_t) &&noexcept
Do nothing, just for compatibility with FutureOn TODO(MBkkt) think about force On/Detach/ToFuture: It...
Definition task.hpp:63
Task() noexcept=default
void Detach() &&noexcept
Definition task.hpp:86
void Cancel() &&
Definition task.hpp:82
Task(Task &&other) noexcept=default
void Detach(IExecutor &e) &&noexcept
Definition task.hpp:92
bool Valid() const &noexcept
Definition task.hpp:44
Future< V, E > ToFuture() &&noexcept
Definition task.hpp:99
Result< V, E > Get() &&noexcept
Definition task.hpp:108
auto Then(IExecutor &e, Func &&f) &&
Definition task.hpp:68
Task(const Task &)=delete
FutureOn< V, E > ToFuture(IExecutor &e) &&noexcept
Definition task.hpp:103
Task & operator=(Task &&other) noexcept=default
Task(detail::ResultCorePtr< V, E > core) noexcept
Definition task.hpp:134
void Touch() &=delete
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
InlineCore & MakeDrop() noexcept
void Start(BaseCore *head, IExecutor &e) noexcept
Definition task_impl.cpp:6
IExecutor & MakeInline() noexcept
Get Inline executor singleton object.
Definition inline.cpp:34
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25