74template<
class Reactive>
75 requires ::aria::ReactiveNode<Reactive>
76void dep(Reactive& r) {
78 t->record_read(
static_cast<Node&
>(r));
85template<PropertyValue T>
91 static_assert(std::copyable<T>,
92 "Computed<T> requires T to be copyable: every observer is handed "
93 "a copy of the latest computed value.");
95 "Computed<T> requires T to be equality-comparable: equal-to-cached "
96 "recomputes are skipped, which is what keeps the graph glitch-free.");
102 template<std::invocable<> Fn>
103 requires std::convertible_to<std::invoke_result_t<Fn>, T>
106 compute_(
std::move(fn)) {
135 [[nodiscard]] T
get()
const {
142 const_cast<Computed*
>(
this)->pull_self_();
145 if (
auto* t = g.current_tracker()) {
146 t->record_read(
const_cast<Computed&
>(*
this));
156 const_cast<Computed*
>(
this)->pull_self_();
158 if (
auto* t = g.current_tracker()) {
159 t->record_read(
const_cast<Computed&
>(*
this));
166 [[nodiscard]] T
peek() const noexcept(
std::is_nothrow_copy_constructible_v<T>) {
171 [[nodiscard]]
const T&
peek_ref() const noexcept {
return *cached_; }
173 operator T()
const {
return get(); }
223 const auto& reads = ctx.
reads();
226 while (edge_pool_.size() < reads.size()) edge_pool_.emplace_back();
227 const bool changed = !cached_ || !(*cached_ == new_val);
228 if (changed) cached_ = std::move(new_val);
235 for (
const auto& read : reads) {
249 std::size_t count = 0;
262 std::function<T()> compute_;
263 std::optional<T> cached_;
270 std::deque<Edge> edge_pool_;
271 std::size_t active_edges_ = 0;
Computed(Fn fn)
Construct and perform the initial compute eagerly, so get() immediately returns the correct value wit...
Definition computed.hpp:104
RAII handle to a single subscription.
Definition subscription.hpp:44
std::size_t dependency_count() const noexcept
Number of upstreams currently in use.
Definition computed.hpp:248
Computed(const Computed &)=delete
const T & peek_ref() const noexcept
Non-tracking snapshot read by const reference.
Definition computed.hpp:171
T peek() const noexcept(std::is_nothrow_copy_constructible_v< T >)
Non-tracking snapshot read.
Definition computed.hpp:166
Computed(Fn fn)
Construct and perform the initial compute eagerly, so get() immediately returns the correct value wit...
Definition computed.hpp:104
::aria::Subscription on_changed(std::function< void(const T &)> fn)
Definition effect.hpp:160
T get() const
Return the cached value, ensuring it is up to date.
Definition computed.hpp:135
Computed & operator=(Computed &&)=delete
~Computed() noexcept override
Explicit destructor: detach every upstream edge before the edge pool releases the storage backing the...
Definition computed.hpp:124
::aria::Subscription observe(std::function< void(const T &, const T &)> fn)
Definition effect.hpp:190
T value_type
Definition computed.hpp:88
bool recompute() override
Called by the Graph when an upstream has changed.
Definition computed.hpp:195
::aria::Subscription bind(std::function< void(const T &)> fn)
Definition effect.hpp:179
Computed & operator=(const Computed &)=delete
const T & get_ref() const
Read by const reference.
Definition computed.hpp:153
Computed(Computed &&)=delete
bool pull(Node &n)
Force-evaluate a single node if it is Dirty / MaybeDirty.
Definition graph.inl:342
Common base for every node participating in the reactive graph.
Definition node.hpp:136
void bump_version_() noexcept
Definition node.hpp:258
static Graph & graph() noexcept
Returns the process-wide singleton Graph this node belongs to.
Definition graph.inl:30
void set_depth(std::uint32_t d) noexcept
Definition node.hpp:209
Node(NodeKind kind) noexcept
Definition node.hpp:144
void for_each_source(F &&f) const
Iterate the source list (used by a Derivation in pull() to compare each upstream's current version ag...
Definition node.hpp:243
void retire_() noexcept
Retire before derived members (including user captures) are destroyed.
Definition graph.inl:42
void attach_as_observer_of(Node &source, Edge &edge) noexcept
Definition graph.inl:64
NodeState state() const noexcept
Definition node.hpp:154
void clear_sources() noexcept
Drop every upstream edge.
Definition graph.inl:107
RAII: push a tracker on construction, pop on destruction.
Definition graph.hpp:305
Per-recompute tracking context for a single Derivation evaluation.
Definition graph.hpp:103
std::vector< detail::NodeHandle > Buffer
Definition graph.hpp:105
const std::vector< detail::NodeHandle > & reads() const noexcept
Definition graph.hpp:130
Type that supports == and != (required for change detection).
Definition concepts.hpp:30
Definition computed.hpp:60
@ Clean
Definition node.hpp:103
NodeKind
Definition node.hpp:87
@ Derivation
Definition node.hpp:89
requires ::aria::ReactiveNode< Reactive > void dep(Reactive &r)
Definition computed.hpp:76
TrackingContext * current_tracker() noexcept
Returns the current tracker of the global graph.
Definition graph.hpp:300
Definition validation_key.hpp:110
A single dependency edge: (upstream source) -> (downstream observer).
Definition node.hpp:113