YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
schedule.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace yaclib {
6namespace detail {
7
8template <typename V, typename E, typename Func>
10 auto* core = [&] {
11 if constexpr (std::is_same_v<V, Unit>) {
12 return MakeCore<CoreType::Run, true, void, E>(std::forward<Func>(f));
13 } else {
14 return MakeUnique<PromiseCore<V, E, Func&&>>(std::forward<Func>(f)).Release();
15 }
16 }();
17 e.IncRef();
18 core->_executor.Reset(NoRefTag{}, &e);
19 using ResultCoreT = typename std::remove_reference_t<decltype(*core)>::Base;
21}
22
23} // namespace detail
24
25/**
26 * Execute Callable func on Inline executor
27 *
28 * \param f func to execute
29 * \return \ref Future corresponding f return value
30 */
31template <typename E = StopError, typename Func>
32/*Task*/ auto Schedule(Func&& f) {
33 return detail::Schedule<Unit, E>(MakeInline(), std::forward<Func>(f));
34}
35
36/**
37 * Execute Callable func on executor
38 *
39 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
40 * \param f func to execute
41 * \return \ref FutureOn corresponding f return value
42 */
43template <typename E = StopError, typename Func>
44/*Task*/ auto Schedule(IExecutor& e, Func&& f) {
46 "better way is call func explicit and use MakeTask to create Task with func result"
47 " or at least use Schedule(func)");
48 return detail::Schedule<Unit, E>(e, std::forward<Func>(f));
49}
50
51/**
52 * Execute Callable func on Inline executor
53 *
54 * \param f func to execute
55 * \return \ref Future corresponding f return value
56 */
57template <typename V = void, typename E = StopError, typename Func>
58/*Task*/ auto LazyContract(Func&& f) {
59 return detail::Schedule<V, E>(MakeInline(), std::forward<Func>(f)).On(nullptr);
60}
61
62/**
63 * Execute Callable func on executor
64 *
65 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
66 * \param f func to execute
67 * \return \ref Task corresponding f return value
68 */
69template <typename V = void, typename E = StopError, typename Func>
70/*Task*/ auto LazyContract(IExecutor& e, Func&& f) {
71 YACLIB_WARN(e.Tag() == IExecutor::Type::Inline, "better way is use LazyContract(func)");
72 return detail::Schedule<V, E>(e, std::forward<Func>(f));
73}
74
75} // namespace yaclib
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
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
YACLIB_INLINE auto Schedule(IExecutor &e, Func &&f)
Definition schedule.hpp:9
auto LazyContract(Func &&f)
Execute Callable func on Inline executor.
Definition schedule.hpp:58
auto Schedule(Func &&f)
Execute Callable func on Inline executor.
Definition schedule.hpp:32
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