YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
run.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <yaclib/log.hpp>
10
11namespace yaclib {
12namespace detail {
13
14template <typename V = Unit, typename E = StopError, typename Func>
16 auto* core = [&] {
17 if constexpr (std::is_same_v<V, Unit>) {
18 static constexpr auto CoreT = CoreType::Run | CoreType::Call | CoreType::ToUnique;
19 return MakeCore<CoreT, void, E>(std::forward<Func>(f));
20 } else {
21 return MakeUnique<PromiseCore<V, E, Func&&, false>>(std::forward<Func>(f)).Release();
22 }
23 }();
24 e.IncRef();
25 core->_executor.Reset(NoRefTag{}, &e);
26 e.Submit(*core);
27 using ResultCoreT = typename std::remove_reference_t<decltype(*core)>::Base;
29}
30
31template <typename V = Unit, typename E = StopError, typename Func>
33 auto* core = [&] {
34 if constexpr (std::is_same_v<V, Unit>) {
35 static constexpr auto CoreT = CoreType::Run | CoreType::Call | CoreType::ToShared;
36 return MakeCore<CoreT, void, E>(std::forward<Func>(f));
37 } else {
39 }
40 }();
41 e.IncRef();
42 core->_executor.Reset(NoRefTag{}, &e);
43 e.Submit(*core);
44 using ResultCoreT = typename std::remove_reference_t<decltype(*core)>::Base;
46}
47
48} // namespace detail
49
50/**
51 * Execute Callable func on Inline executor
52 *
53 * \param f func to execute
54 * \return \ref Future corresponding f return value
55 */
56template <typename E = StopError, typename Func>
57/*Future*/ auto Run(Func&& f) {
58 return detail::Run<Unit, E>(MakeInline(), std::forward<Func>(f)).On(nullptr);
59}
60
61template <typename E = StopError, typename Func>
62/*SharedFuture*/ auto RunShared(Func&& f) {
63 return detail::RunShared<Unit, E>(MakeInline(), std::forward<Func>(f));
64}
65
66/**
67 * Execute Callable func on executor
68 *
69 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
70 * \param f func to execute
71 * \return \ref FutureOn corresponding f return value
72 */
73template <typename E = StopError, typename Func>
74/*FutureOn*/ auto Run(IExecutor& e, Func&& f) {
76 "better way is call func explicit and use MakeFuture to create Future with func result"
77 " or at least use Run(func)");
78 return detail::Run<Unit, E>(e, std::forward<Func>(f));
79}
80
81template <typename E = StopError, typename Func>
82/*SharedFuture*/ auto RunShared(IExecutor& e, Func&& f) {
83 return detail::RunShared<Unit, E>(e, std::forward<Func>(f));
84}
85
86/**
87 * Execute Callable func on Inline executor
88 *
89 * \param f func to execute
90 * \return \ref Future corresponding f return value
91 */
92template <typename V = void, typename E = StopError, typename Func>
93/*Future*/ auto AsyncContract(Func&& f) {
94 return detail::Run<V, E>(MakeInline(), std::forward<Func>(f)).On(nullptr);
95}
96
97template <typename V = void, typename E = StopError, typename Func>
98/*SharedFuture*/ auto AsyncSharedContract(Func&& f) {
99 return detail::RunShared<V, E>(MakeInline(), std::forward<Func>(f));
100}
101
102/**
103 * Execute Callable func on executor
104 *
105 * \param e executor to be used to execute f and saved as callback executor for return \ref Future
106 * \param f func to execute
107 * \return \ref FutureOn corresponding f return value
108 */
109template <typename V = void, typename E = StopError, typename Func>
110/*FutureOn*/ auto AsyncContract(IExecutor& e, Func&& f) {
112 "better way is call func explicit and use MakeFuture to create Future with func result"
113 " or at least use AsyncContract(func)");
114 return detail::Run<V, E>(e, std::forward<Func>(f));
115}
116
117template <typename V = void, typename E = StopError, typename Func>
118/*SharedFuture*/ auto AsyncSharedContract(IExecutor& e, Func&& f) {
119 return detail::RunShared<V, E>(e, std::forward<Func>(f));
120}
121
122} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:244
A intrusive pointer to objects with an embedded reference count.
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
constexpr size_t kSharedRefWithFuture
std::conditional_t< IsDetach(Type), NoResultCore, std::conditional_t< IsToShared(Type), SharedCore< V, E >, UniqueCore< V, E > > > ResultCoreT
Definition core.hpp:92
YACLIB_INLINE auto RunShared(IExecutor &e, Func &&f)
Definition run.hpp:32
auto AsyncSharedContract(Func &&f)
Definition run.hpp:98
auto Run(Func &&f)
Execute Callable func on Inline executor.
Definition run.hpp:57
auto RunShared(Func &&f)
Definition run.hpp:62
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:93