Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
future_value.hpp
Go to the documentation of this file.
1#pragma once
4#include "async.hpp"
5#include "buffers.hpp"
6
7namespace uni20::async
8{
9
12template <typename T> class FutureValue {
13 public:
14 using value_type = T;
15
17 FutureValue() : async_{}, write_buf_{async_.write()} {}
18
20 FutureValue(FutureValue&&) noexcept = default;
22 FutureValue& operator=(FutureValue&&) noexcept = default;
23
25 FutureValue(FutureValue const&) = delete;
26
29 Async<T> const& async() const noexcept { return async_; }
30
33 ReadBuffer<T> read() const { return async_.read(); }
34
37 [[nodiscard]] WriteBuffer<T> write() noexcept { return std::move(write_buf_); }
38
43 template <typename U>
45 requires std::constructible_from<T, U&&>
46 {
47 write_buf_.emplace_assert(std::forward<U>(v));
48 write_buf_.release();
49 return *this;
50 }
51
56 template <typename U> FutureValue& operator=(Async<U> const& v)
57 {
58 async_assign(std::move(write_buf_), v.read());
59 return *this;
60 }
61
66 template <typename U> FutureValue& operator=(Async<U>&& v)
67 {
68 async_move(std::move(write_buf_), std::move(v));
69 return *this;
70 }
71
74 Async<T>& value() { return async_; }
77 Async<T> const& value() const { return async_; }
78
79 private:
80 Async<T> async_;
81 WriteBuffer<T> write_buf_;
82};
83
86template <typename T> class Defer {
87 public:
88 using value_type = T;
89
91 Defer(Defer&&) = default;
92
94 Defer() = delete;
96 Defer(Defer const&) = delete;
98 Defer& operator=(Defer&&) = delete;
100 Defer& operator=(Defer const&) = delete;
101
104 explicit Defer(Async<T>& w) : writer_(w.write()) {}
105
108 explicit Defer(WriteBuffer<T>&& w) : writer_(std::move(w)) {}
109
111 template <typename U>
112 requires std::constructible_from<T, U&&>
113 void write_assert(U&& val)
114 {
115 writer_.emplace_assert(std::forward<U>(val));
116 }
117
121 template <typename U>
122 requires std::assignable_from<T&, async_value_t<U>>
123 void operator=(U&& val)
124 {
125 async_assign(std::move(writer_), std::forward<U>(val));
126 }
127
129 [[nodiscard]] WriteProxy<T> write() { return writer_.write(); }
130
132 void release() { writer_.release(); }
133
134 private:
135 WriteBuffer<T> writer_;
136};
137
139template <typename T> Defer(Async<T>&) -> Defer<T>;
140
145template <typename T> Defer<T> defer_write(Async<T>& a) { return Defer<T>(a); }
146
147} // namespace uni20::async
Awaitable capabilities for shared reads and exclusive mutable access to Async<T>.
Async<T> is a container for coroutine-safe asynchronous read/write.
Definition async/async.hpp:93
ReadBuffer< T > read() const
Begin an asynchronous read of the value.
Definition async/async.hpp:339
Move-only deferred writer helper for a single WriteBuffer<T>.
Definition future_value.hpp:86
T value_type
Definition future_value.hpp:88
void release()
Release the retained write buffer early.
Definition future_value.hpp:132
void write_assert(U &&val)
Write immediately without suspending — asserts write readiness.
Definition future_value.hpp:113
Defer(Async< T > &w)
Construct from an Async writer endpoint.
Definition future_value.hpp:104
Defer()=delete
No default construction.
WriteProxy< T > write()
Get the WriteBuffer for coroutine-based use.
Definition future_value.hpp:129
Defer & operator=(Defer &&)=delete
Non-move-assignable.
Defer(Defer &&)=default
Move constructor.
Defer & operator=(Defer const &)=delete
Non-copyable.
Defer(Defer const &)=delete
Non-copyable.
void operator=(U &&val)
Schedule assignment through the deferred writer.
Definition future_value.hpp:123
Defer(WriteBuffer< T > &&w)
Construct from an existing write buffer.
Definition future_value.hpp:108
Write-once async value wrapper that retains a pending write buffer.
Definition future_value.hpp:12
FutureValue & operator=(U &&v)
Assign a value immediately into the retained write buffer.
Definition future_value.hpp:44
FutureValue()
Construct a new, uninitialized FutureValue.
Definition future_value.hpp:17
ReadBuffer< T > read() const
Get a read buffer from the wrapped Async value.
Definition future_value.hpp:33
Async< T > & value()
Mutable access to the wrapped Async value.
Definition future_value.hpp:74
WriteBuffer< T > write() noexcept
Get the retained write buffer.
Definition future_value.hpp:37
FutureValue & operator=(Async< U > &&v)
Move-assign from another async source by scheduling an async move.
Definition future_value.hpp:66
Async< T > const & value() const
Const access to the wrapped Async value.
Definition future_value.hpp:77
FutureValue & operator=(Async< U > const &v)
Assign from another async source by scheduling an async copy.
Definition future_value.hpp:56
FutureValue(FutureValue &&) noexcept=default
Move constructor.
T value_type
Definition future_value.hpp:14
RAII handle for reading the value from an Async container at a given epoch.
Definition buffers.hpp:53
Assignment-only proxy returned by WriteBuffer::write().
Definition buffers.hpp:1720
Exclusive mutable-access buffer for one Async<T> epoch.
Definition buffers.hpp:859
void async_move(WriteBuffer< T > lhs, U &&rhs)
Definition async_ops.hpp:369
void async_assign(WriteBuffer< T > destination, U &&source)
Assign an awaitable value into an existing independent-value timeline.
Definition async_ops.hpp:310
Definition qnum.hpp:403
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
Defer< T > defer_write(Async< T > &a)
Create a deferred writer helper for an Async value.
Definition future_value.hpp:145
Async aliases for lazy tensor-level views.