morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::ui::Prop< T > Class Template Reference

A node property: a constant, or a slot — a reactive read, with a stable id when it was given one. More...

#include <view.hpp>

Public Member Functions

 Prop ()
 The value-initialised constant.
template<typename U>
requires (!std::invocable<U&>) && detail::ConstantConvertible<U, T>
 Prop (U &&value)
 A constant.
template<typename F>
requires std::invocable<F&> && std::convertible_to<std::invoke_result_t<F&>, T>
 Prop (F binding)
 A slot without an id.
template<typename F>
requires std::invocable<F&> && std::convertible_to<std::invoke_result_t<F&>, T>
 Prop (SlotBinding< F > binding)
 A slot with an id, as ui::slot makes it.
bool isBound () const noexcept
 Whether this is a slot.
T const & constant () const
 The constant. Only valid when !isBound().
std::function< T()> const & binding () const
 The slot's read. Only valid when isBound().
std::optional< SlotId > slotId () const noexcept
 The slot's id.
T evaluate () const
 The current value: the constant, or the slot read once.

Detailed Description

template<typename T>
class morph::ui::Prop< T >

A node property: a constant, or a slot — a reactive read, with a stable id when it was given one.

A constant is set on the widget once and creates no reactive node. A slot becomes an equality-gated reactive::Computed plus an reactive::Effect calling one widget setter. An input whose value is a slot is controlled: after a user's edit, the widget shows what the slot says. A constant is not re-asserted.

A braced list cannot initialise a Prop, because the constant constructor deduces its argument's type: name the type, as in .options = std::vector<ui::SelectOption>{{...}, {...}}.

Template Parameters
TThe property's value type.

Definition at line 106 of file view.hpp.

Constructor & Destructor Documentation

◆ Prop() [1/4]

template<typename T>
morph::ui::Prop< T >::Prop ( )
inline

The value-initialised constant.

Definition at line 109 of file view.hpp.

◆ Prop() [2/4]

template<typename T>
template<typename U>
requires (!std::invocable<U&>) && detail::ConstantConvertible<U, T>
morph::ui::Prop< T >::Prop ( U && value)
inline

A constant.

A callable is never a constant, even when it converts to T: a captureless lambda converts to a function pointer and that to bool, so Prop<bool>{[] { return false; }} would otherwise be the constant true.

A pointer does not become a bool constant and a floating-point value does not become an integral one; see detail::ConstantConvertible.

Template Parameters
UA non-callable type that converts to T without changing the kind of value.
Parameters
valueThe constant.

Definition at line 124 of file view.hpp.

◆ Prop() [3/4]

template<typename T>
template<typename F>
requires std::invocable<F&> && std::convertible_to<std::invoke_result_t<F&>, T>
morph::ui::Prop< T >::Prop ( F binding)
inline

A slot without an id.

The read must be callable: an empty std::function or a null function pointer is refused here, where the mistake is made, rather than when a mount first calls it.

Template Parameters
FA callable taking no arguments and returning something convertible to T.
Parameters
bindingEvaluated by the mount inside a Computed; every signal it reads is a dependency.
Exceptions
std::invalid_argumentwhen binding is an empty std::function or a null function pointer.

Definition at line 138 of file view.hpp.

◆ Prop() [4/4]

template<typename T>
template<typename F>
requires std::invocable<F&> && std::convertible_to<std::invoke_result_t<F&>, T>
morph::ui::Prop< T >::Prop ( SlotBinding< F > binding)
inline

A slot with an id, as ui::slot makes it.

Template Parameters
FA callable taking no arguments and returning something convertible to T.
Parameters
bindingThe id and the read.
Exceptions
std::invalid_argumentwhen the read is an empty std::function or a null function pointer.

Definition at line 148 of file view.hpp.

Member Function Documentation

◆ isBound()

template<typename T>
bool morph::ui::Prop< T >::isBound ( ) const
inlinenodiscardnoexcept

Whether this is a slot.

Returns
True for a slot, false for a constant.

Definition at line 155 of file view.hpp.

◆ constant()

template<typename T>
T const & morph::ui::Prop< T >::constant ( ) const
inlinenodiscard

The constant. Only valid when !isBound().

Returns
The constant.
Exceptions
std::bad_variant_accesswhen this is a slot.

Definition at line 160 of file view.hpp.

◆ binding()

template<typename T>
std::function< T()> const & morph::ui::Prop< T >::binding ( ) const
inlinenodiscard

The slot's read. Only valid when isBound().

Returns
The read.
Exceptions
std::bad_variant_accesswhen this is a constant.

Definition at line 165 of file view.hpp.

◆ slotId()

template<typename T>
std::optional< SlotId > morph::ui::Prop< T >::slotId ( ) const
inlinenodiscardnoexcept

The slot's id.

Returns
The id given through ui::slot; nullopt for a constant and for a slot made from a bare callable.

Definition at line 169 of file view.hpp.

◆ evaluate()

template<typename T>
T morph::ui::Prop< T >::evaluate ( ) const
inlinenodiscard

The current value: the constant, or the slot read once.

Returns
The value.

Definition at line 176 of file view.hpp.


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