ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZenitUI::Bindable< T > Class Template Reference

Observable single-value container. More...

#include <Bindable.hpp>

Classes

class  Subscription
 RAII handle to an onChanged registration. More...

Public Member Functions

 Bindable (T initial)
 Construct with an initial value.
 Bindable (const Bindable &)=delete
Bindable & operator= (const Bindable &)=delete
 Bindable (Bindable &&)=delete
Bindable & operator= (Bindable &&)=delete
const T & get () const
 Read the current value.
void set (T v)
 Replace the value and notify subscribers if it changed.
Subscription onChanged (std::function< void(const T &)> cb, bool fireImmediately=false)
 Register a callback fired on value changes.
template<typename W, typename Arg>
Subscription bindTo (std::weak_ptr< W > widget, void(W::*method)(Arg), bool fireImmediately=false)
 Bind this value to a widget method.
template<typename W, typename Arg>
Subscription bindTo (std::shared_ptr< W > widget, void(W::*method)(Arg), bool fireImmediately=false)
 Overload for shared_ptr widgets; forwards to the weak-pointer version.

Detailed Description

template<typename T>
class ZenitUI::Bindable< T >

Observable single-value container.

Template Parameters
TValue type; must be copy-constructible and support ==.
Examples
00_demo/main.cpp, 05_data_binding/main.cpp, and 07_dynamic_lists/main.cpp.

Member Function Documentation

◆ bindTo()

template<typename T>
template<typename W, typename Arg>
Subscription ZenitUI::Bindable< T >::bindTo ( std::weak_ptr< W > widget,
void(W::* method )(Arg),
bool fireImmediately = false )
inline

Bind this value to a widget method.

Template Parameters
WWidget type.
ArgArgument type of the widget method; must be implicitly constructible from T.
Parameters
widgetWeak pointer to the target widget.
methodMember function to call on change.
fireImmediatelyWhen true, invoke method right away.
Returns
A Subscription that unregisters on destruction.

The weak pointer is locked on every notification, so the widget can be destroyed independently of the subscription.

Examples
00_demo/main.cpp.

◆ onChanged()

template<typename T>
Subscription ZenitUI::Bindable< T >::onChanged ( std::function< void(const T &)> cb,
bool fireImmediately = false )
inline

Register a callback fired on value changes.

Parameters
cbCallback invoked with the new value.
fireImmediatelyWhen true, invoke cb right away with the current value (before returning).
Returns
A Subscription that unregisters on destruction.
Examples
00_demo/main.cpp, and 05_data_binding/main.cpp.

◆ set()

template<typename T>
void ZenitUI::Bindable< T >::set ( T v)
inline

Replace the value and notify subscribers if it changed.

Equality is compared on T; setting the same value is a no-op. Subscribers are snapshotted before dispatch, so a subscriber that (un)subscribes from within its callback does not invalidate the loop.

Examples
00_demo/main.cpp.

The documentation for this class was generated from the following file: