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) && {
70 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyOn>(_core, &e, std::forward<Func>(f));
71 }
72 template <typename Func>
73 /*Task*/ auto ThenInline(Func&& f) && {
75 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyInline>(_core, nullptr,
76 std::forward<Func>(f));
77 }
78 template <typename Func>
79 /*Task*/ auto Then(Func&& f) && {
81 return detail::SetCallback<detail::CoreType::Then, detail::CallbackType::LazyOn>(_core, nullptr,
82 std::forward<Func>(f));
83 }
84
85 void Cancel() && noexcept {
86 std::move(*this).Detach(MakeInline(StopTag{}));
87 }
88
89 void Detach() && noexcept {
91 auto* core = _core.Release();
92 core->StoreCallback(detail::MakeDrop());
94 }
95 void Detach(IExecutor& e) && noexcept {
97 auto* core = _core.Release();
98 core->StoreCallback(detail::MakeDrop());
100 }
101
102 Future<V, E> ToFuture() && noexcept {
104 detail::Start(_core.Get());
105 return {std::move(_core)};
106 }
109 detail::Start(_core.Get(), e);
110 return {std::move(_core)};
111 }
112
113 Result<V, E> Get() && noexcept {
114 // TODO(MBkkt) make it better: we can remove concurrent atomic changes from here
115 return std::move(*this).ToFuture().Get();
116 }
117
118 void Touch() & = delete;
119 void Touch() const&& = delete;
120
123 return _core->Get();
124 }
125 Result<V, E> Touch() && noexcept {
127 auto core = std::exchange(_core, nullptr);
128 return std::move(core->Get());
129 }
130
131 /**
132 * Method that get internal Core state
133 *
134 * \return internal Core state ptr
135 */
137 return _core;
138 }
140 }
141
142 private:
143 detail::ResultCorePtr<V, E> _core;
144};
145
146extern template class Task<>;
147
148} // 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:136
Result< V, E > Touch() &&noexcept
Definition task.hpp:125
auto ThenInline(Func &&f) &&
Definition task.hpp:73
bool Ready() const &noexcept
Check that Result that corresponds to this Task is computed.
Definition task.hpp:53
void Touch() const &&=delete
void Cancel() &&noexcept
Definition task.hpp:85
auto Then(Func &&f) &&
Definition task.hpp:79
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:89
Task(Task &&other) noexcept=default
void Detach(IExecutor &e) &&noexcept
Definition task.hpp:95
bool Valid() const &noexcept
Definition task.hpp:44
Future< V, E > ToFuture() &&noexcept
Definition task.hpp:102
Result< V, E > Get() &&noexcept
Definition task.hpp:113
auto Then(IExecutor &e, Func &&f) &&
Definition task.hpp:68
Task(const Task &)=delete
FutureOn< V, E > ToFuture(IExecutor &e) &&noexcept
Definition task.hpp:107
Task & operator=(Task &&other) noexcept=default
Task(detail::ResultCorePtr< V, E > core) noexcept
Definition task.hpp:139
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