Skip to content

ui::Subscription

Header: winui/core/Property.hpp.

One edge of the observer graph: keeps a handler registered on a Property or Signal for as long as it lives, owned by the observer.

Move-only. Destroying, reset()ting or move-assigning over it unsubscribes; if the observed Property dies first the subscription goes dead (alive() turns false), so either end may be destroyed first. Not a template, so an Element holds bindings of any value type in one std::vector.

Example:

ui::Property<int> count{0};
auto label = std::make_unique<ui::TextControl>("0");
ui::TextControl* observer = label.get();

// Owned by the label: unsubscribes when the label is destroyed.
label->add_subscription(count.observe([observer](const int& n) {
    observer->set_text(std::to_string(n));
}));

// Or held by hand for a scoped observation.
ui::Subscription sub = count.observe([](const int& n) { std::printf("%d\n", n); });
count = 1;   // both handlers fire
sub.reset(); // only the label's remains

Subscription

Subscription() = default

A dead subscription observing nothing; alive() is false.

~Subscription

~Subscription()

Unsubscribes (see reset()).

Subscription

Subscription(const Subscription&) = delete

Not copyable: one subscription is one edge.

operator=

Subscription& operator=(const Subscription&) = delete

Not copyable: one subscription is one edge.

Subscription

Subscription(Subscription&& other) noexcept

Takes over other's place in the observer list; other is left dead.

operator=

Subscription& operator=(Subscription&& other) noexcept

Unsubscribes this one, then takes over other's place; other is left dead.

reset

void reset() noexcept

Unsubscribe now. Idempotent, and safe after the observed Property has been destroyed.

alive

bool alive() const noexcept

False once this subscription has been reset(), moved from, or had its Property destroyed out from under it.