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