YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
promise_core.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <yaclib/log.hpp>
6
7#include <type_traits>
8#include <utility>
9
10namespace yaclib::detail {
11
12template <typename V, typename E, typename Func>
13class PromiseCore : public ResultCore<V, E>, public FuncCore<Func> {
14 using F = FuncCore<Func>;
15 using Invoke = typename F::Invoke;
16 using Storage = typename F::Storage;
17
18 public:
20
21 explicit PromiseCore(Func&& f) : F{std::forward<Func>(f)} {
22 }
23
24 private:
25 void Call() noexcept final {
27 try {
28 // We need to move func with capture on stack, because promise can be Set before func return
29 static_assert(std::is_nothrow_move_constructible_v<Storage>);
30 auto func = std::move(this->_func.storage);
31 this->_func.storage.~Storage();
32 std::forward<Invoke>(func)(std::move(promise));
33 } catch (...) {
34 if (promise.Valid()) {
35 std::move(promise).Set(std::current_exception());
36 } else {
37 // ignore it, because promise already used
38 YACLIB_WARN(true, "Your exception will be ignored, you probably move promise too early");
39 }
40 }
41 }
42
43 void Drop() noexcept final {
44 this->_func.storage.~Storage();
45 this->Store(StopTag{});
46 Loop(this, this->template SetResult<false>());
47 }
48};
49
50} // namespace yaclib::detail
A intrusive pointer to objects with an embedded reference count.
std::decay_t< Func > Storage
Definition func_core.hpp:14
YACLIB_NO_UNIQUE_ADDRESS State _func
Definition func_core.hpp:32
std::conditional_t< std::is_function_v< std::remove_reference_t< Func > >, Storage, Func > Invoke
Definition func_core.hpp:15
void Store(Args &&... args) noexcept(std::is_nothrow_constructible_v< Result< V, E >, Args &&... >)
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
YACLIB_INLINE void Loop(InlineCore *prev, InlineCore *curr) noexcept
Definition base_core.hpp:69
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_NO_UNIQUE_ADDRESS Storage storage
Definition func_core.hpp:24