Skip to content

ui::Computed

Header: winui/core/Property.hpp.

template <typename T> class Computed

A read-only value derived from one or more sources: fn(a, b, ...), computed now and again whenever any source changes. The sources are written out where it is declared, in the same shape as the several-source bind(); nothing is tracked behind the scenes.

It goes through the equality gate like a Property, so its observers and the fields bound to it hear only about results that changed. It binds and observes like a const Property<T> (always one-way), and can be a source of another Computed, of bind() and of ui::observe().

It owns its edges to the sources, and either end may be destroyed first: a source destroyed before it keeps contributing its last value. Not copyable or movable, since the edges point at it. UI-thread only, as Property is.

Example:

ui::Property<std::string> first;
ui::Property<std::string> last;
ui::Property<bool>        agreed{false};

ui::Computed can_submit{[](const std::string& f, const std::string& l, bool ok) {
                            return !f.empty() && !l.empty() && ok;
                        }, first, last, agreed};

window.set_content(ui::Button{.text = "Submit", .enabled = can_submit});
if (can_submit.get()) { save(); }

value_type

using ui::Computed< T >::value_type = T

The stored type.

Computed

template <typename Fn, typename... Ss>
Computed(Fn fn, const Ss& ... sources)
    requires (sizeof...(Ss) > 0) && (detail::is_source<Ss> && ...) && std::invocable<Fn&, const detail::source_value_t<Ss>&...> && std::convertible_to<std::invoke_result_t<Fn&, const detail::source_value_t<Ss>&...>, T> && (std::copy_constructible<detail::source_value_t<Ss>> && ...)

Computes fn(sources.get()...) now, then again whenever a source changes. fn's result must convert to T.

Computed

Computed(const Computed&) = delete

Deleted: the edges point at this object.

operator=

Computed& operator=(const Computed&) = delete

Deleted: the edges point at this object.

get

const T& get() const noexcept

The current value. The reference is invalidated by the next change.

operator*

const T& operator*() const noexcept

Same as get().

operator->

const T* operator->() const noexcept

Member access to the current value.

observe

Subscription observe(Callback<void(const T&)> fn) const

Observe changes of the result, as Property::observe(): not called for the current value, and the Subscription must be kept for fn to keep firing.

property

const Property<T>& property() const noexcept

The read-only Property holding the result, for code that takes a const Property<T>&.