ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Bindable.hpp File Reference

Single-value observable used for one-way data binding between models and UI components. More...

#include "Common.hpp"

Go to the source code of this file.

Classes

class  ZenitUI::Bindable< T >
 Observable single-value container. More...
class  ZenitUI::Bindable< T >::Subscription
 RAII handle to an onChanged registration. More...

Detailed Description

Single-value observable used for one-way data binding between models and UI components.

Bindable<T> is deliberately minimal:

  • It stores one value.
  • set() notifies subscribers if the value changed.
  • onChanged() returns a Subscription RAII handle that unsubscribes on destruction, so subscribers do not need to keep track of tokens.
  • bindTo() builds a Subscription that calls a method on a widget.

Lifetime model: Bindable is not copyable or movable. Its Subscription handles hold a shared_ptr<ControlBlock> whose alive_ flag is flipped in the destructor, so a Subscription outliving its Bindable is safe (it just becomes a no-op). Conversely, set() is re-entrancy-safe: the subscriber list is snapshotted before iteration, so a subscriber that (un)subscribes from inside its own callback does not invalidate the loop.

Non-goals: thread safety, computed/derived bindings, batching. Wrap in a mutex or compose externally if those are needed.