YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
promise.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <yaclib/config.hpp>
5#include <yaclib/fwd.hpp>
7
8namespace yaclib {
9
10template <typename V, typename T>
12 static_assert(Check<V>(), "V should be valid");
13 static_assert(!std::is_same_v<V, typename T::Error>,
14 "V cannot be the same as the trait Error type, because callback dispatch would be ambiguous");
15
16 public:
17 Promise(const Promise& other) = delete;
18 Promise& operator=(const Promise& other) = delete;
19
20 Promise(Promise&& other) noexcept = default;
21 Promise& operator=(Promise&& other) noexcept = default;
22
23 /**
24 * The default constructor creates not a \ref Valid Promise
25 *
26 * Needed only for usability, e.g. instead of std::optional<Promise<T>> in containers.
27 */
29
30 /**
31 * If Promise is \ref Valid then set \ref StopTag
32 */
34 if (Valid()) {
35 std::move(*this).Set(StopTag{});
36 }
37 }
38
39 /**
40 * Check if this \ref Promise has \ref Future
41 *
42 * \return false if this \ref Promise is default-constructed or moved to, otherwise true
43 */
45 return _core != nullptr;
46 }
47
48 /**
49 * Set \ref Promise result
50 *
51 * \tparam Args \ref T::MakeResult<V> should be invocable with this types
52 * \param args arguments
53 */
54 template <typename... Args>
55 void Set(Args&&... args) && {
56 YACLIB_ASSERT(Valid());
57 if constexpr (sizeof...(Args) == 0) {
58 _core->Store(Unit{});
59 } else {
60 _core->Store(std::forward<Args>(args)...);
61 }
62 auto* core = _core.Release();
63 detail::Loop(core, core->template SetResult<false>());
64 }
65
66 /**
67 * Part of unsafe but internal API
68 */
69 explicit Promise(detail::UniqueCorePtr<V, T> core) noexcept : _core{std::move(core)} {
70 }
71
73 return _core;
74 }
75
76 private:
78};
79
80extern template class Promise<>;
81
82} // namespace yaclib
A intrusive pointer to objects with an embedded reference count.
Promise(detail::UniqueCorePtr< V, T > core) noexcept
Part of unsafe but internal API.
Definition promise.hpp:69
Promise(const Promise &other)=delete
void Set(Args &&... args) &&
Set Promise result.
Definition promise.hpp:55
Promise & operator=(const Promise &other)=delete
detail::UniqueCorePtr< V, T > & GetCore() noexcept
Definition promise.hpp:72
Promise() noexcept=default
The default constructor creates not a Valid Promise.
Promise(Promise &&other) noexcept=default
Promise & operator=(Promise &&other) noexcept=default
bool Valid() const &noexcept
Check if this Promise has Future.
Definition promise.hpp:44
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
YACLIB_INLINE void Loop(InlineCore *prev, InlineCore *curr) noexcept
Contract< V, T > MakeContract()
Creates related future and promise.
Definition contract.hpp:25