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/fwd.hpp>
6
7namespace yaclib {
8
9template <typename V, typename E>
11 static_assert(Check<V>(), "V should be valid");
12 static_assert(Check<E>(), "E should be valid");
13 static_assert(!std::is_same_v<V, E>, "Promise cannot be instantiated with same V and E, because it's ambiguous");
14
15 public:
16 Promise(const Promise& other) = delete;
17 Promise& operator=(const Promise& other) = delete;
18
19 Promise(Promise&& other) noexcept = default;
20 Promise& operator=(Promise&& other) noexcept = default;
21
22 /**
23 * The default constructor creates not a \ref Valid Promise
24 *
25 * Needed only for usability, e.g. instead of std::optional<Promise<T>> in containers.
26 */
28
29 /**
30 * If Promise is \ref Valid then set \ref StopTag
31 */
33 if (Valid()) {
34 std::move(*this).Set(StopTag{});
35 }
36 }
37
38 /**
39 * Check if this \ref Promise has \ref Future
40 *
41 * \return false if this \ref Promise is default-constructed or moved to, otherwise true
42 */
44 return _core != nullptr;
45 }
46
47 /**
48 * Set \ref Promise result
49 *
50 * \tparam Args \ref Result<T> should be constructable from this types
51 * \param args arguments
52 */
53 template <typename... Args>
54 void Set(Args&&... args) && {
56 if constexpr (sizeof...(Args) == 0) {
57 _core->Store(std::in_place);
58 } else {
59 _core->Store(std::forward<Args>(args)...);
60 }
61 auto* core = _core.Release();
63 }
64
65 /**
66 * Part of unsafe but internal API
67 */
68 explicit Promise(detail::ResultCorePtr<V, E> core) noexcept : _core{std::move(core)} {
69 }
70
72 return _core;
73 }
74
75 private:
77};
78
79extern template class Promise<>;
80
81} // namespace yaclib
A intrusive pointer to objects with an embedded reference count.
Promise() noexcept=default
The default constructor creates not a Valid Promise.
Promise(const Promise &other)=delete
Promise(detail::ResultCorePtr< V, E > core) noexcept
Part of unsafe but internal API.
Definition promise.hpp:68
Promise & operator=(Promise &&other) noexcept=default
Promise & operator=(const Promise &other)=delete
bool Valid() const &noexcept
Check if this Promise has Future.
Definition promise.hpp:43
Promise(Promise &&other) noexcept=default
void Set(Args &&... args) &&
Set Promise result.
Definition promise.hpp:54
detail::ResultCorePtr< V, E > & GetCore() noexcept
Definition promise.hpp:71
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
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