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>) {
13 return MakeCore<CoreT, void, E>(std::forward<Func>(f));
14 } else {
15 return MakeUnique<PromiseCore<V, E, Func&&, false>>(std::forward<Func>(f)).Release();
16 }
17 }();
18 e.IncRef();
19 core->_executor.Reset(NoRefTag{}, &e);
20 using ResultCoreT = typename std::remove_reference_t<decltype(*core)>::Base;
22}
23
24} // namespace detail
25
26/**
27 * Execute Callable func on Inline executor
28 *
29 * \param f func to execute
30 * \return \ref Future corresponding f return value
31 */
32template <typename E = StopError, typename Func>
33/*Task*/ auto Schedule(Func&& f) {
34 return detail::Schedule<Unit, E>(MakeInline(), std::forward<Func>(f));
35}
36
37/**
38 * Execute Callable func on executor
39 *
40 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
41 * \param f func to execute
42 * \return \ref FutureOn corresponding f return value
43 */
44template <typename E = StopError, typename Func>
45/*Task*/ auto Schedule(IExecutor& e, Func&& f) {
47 "better way is call func explicit and use MakeTask to create Task with func result"
48 " or at least use Schedule(func)");
49 return detail::Schedule<Unit, E>(e, std::forward<Func>(f));
50}
51
52/**
53 * Execute Callable func on Inline executor
54 *
55 * \param f func to execute
56 * \return \ref Future corresponding f return value
57 */
58template <typename V = void, typename E = StopError, typename Func>
59/*Task*/ auto LazyContract(Func&& f) {
60 return detail::Schedule<V, E>(MakeInline(), std::forward<Func>(f)).On(nullptr);
61}
62
63/**
64 * Execute Callable func on executor
65 *
66 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
67 * \param f func to execute
68 * \return \ref Task corresponding f return value
69 */
70template <typename V = void, typename E = StopError, typename Func>
71/*Task*/ auto LazyContract(IExecutor& e, Func&& f) {
72 YACLIB_WARN(e.Tag() == IExecutor::Type::Inline, "better way is use LazyContract(func)");
73 return detail::Schedule<V, E>(e, std::forward<Func>(f));
74}
75
76} // 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
std::conditional_t< IsDetach(Type), NoResultCore, std::conditional_t< IsToShared(Type), SharedCore< V, E >, UniqueCore< V, E > > > ResultCoreT
Definition core.hpp:92
auto LazyContract(Func &&f)
Execute Callable func on Inline executor.
Definition schedule.hpp:59
auto Schedule(Func &&f)
Execute Callable func on Inline executor.
Definition schedule.hpp:33
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