AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ADataBinding< Model > Class Template Reference

Data binding implementation. More...

#include <AUI/Util/ADataBinding.h>

Public Member Functions#

 ADataBinding (const Model &m)
 
 ADataBinding (Model *m)
 
template<typename View, typename ModelField, typename SetterArg>
auto operator() (ModelField(Model::*field), void(View::*setterFunc)(SetterArg))
 Create a connection to setter only.
 
template<typename ModelField, typename SetterLambda>
auto operator() (ModelField(Model::*field), SetterLambda setterLambda)
 Create a connection to specified lambda setter only.
 
template<typename View, typename ModelField, typename GetterRV, aui::invocable< View *, const ModelField & > Setter>
auto operator() (ModelField(Model::*field), ASignal< GetterRV >(View::*getter), Setter setter=(void(View::*)(const ModelField &)) nullptr)
 Create a connection to specified pointer-to-member-field signal and pointer-to-member-function setter.
 
template<typename Data>
auto operator() (Data(Model::*field))
 Create a connection via ADataBindingDefault.
 
template<typename Data, aui::invocable< Data > Projection>
ADataBindingLinker2< Model, Data, Projection > operator() (Data(Model::*field), Projection projection)
 Create a connection via ADataBindingDefault and projection (setter only).
 
const Model & getModel () const noexcept
 
Model const * operator-> () const noexcept
 
Model & getEditableModel ()
 
void setModel (const Model &model)
 
void setModel (Model *model)
 
const void * getExclusion () const
 
void notifyUpdate (void *except=nullptr, unsigned field=-1)
 
template<typename ModelField>
void notifyUpdate (ModelField(Model::*field))
 
template<typename ModelField, aui::convertible_to< ModelField > U>
void setValue (ModelField(Model::*field), U &&value)
 
void addObserver (Observer applier)
 
template<aui::invocable T>
void addObserver (T &&applier)
 
template<typename ModelField, typename FieldObserver>
void addObserverNoInitialCall (ModelField(Model::*field), FieldObserver &&observer)
 
template<typename ModelField, typename FieldObserver>
void addObserver (ModelField(Model::*field), FieldObserver &&observer)
 
- Public Member Functions inherited from AObject
_< AObjectsharedPtr ()
 
_weak< AObjectweakPtr ()
 
template<typename Connectable, ACompatibleSlotFor< Connectable > Function>
decltype(auto) connect (const Connectable &connectable, Function &&function)
 Connects signal or property to slot of "this" object.
 
void setSignalsEnabled (bool enabled)
 
bool isSignalsEnabled () const noexcept
 
template<ASignalInvokable T>
void operator^ (T &&t) noexcept
 
_< AAbstractThreadgetThread () const
 
bool isSlotsCallsOnlyOnMyThread () const noexcept
 
void setSlotsCallsOnlyOnMyThread (bool slotsCallsOnlyOnMyThread)
 
- Public Member Functions inherited from AObjectBase
 AObjectBase (AObjectBase &&rhs) noexcept
 
 AObjectBase (const AObjectBase &rhs) noexcept
 
AObjectBaseoperator= (const AObjectBase &rhs) noexcept
 
AObjectBaseoperator= (AObjectBase &&rhs) noexcept
 
- Public Member Functions inherited from aui::noncopyable
 noncopyable (const noncopyable &)=delete
 
noncopyableoperator= (const noncopyable &)=delete
 

Signals and public fields#

emits modelChanged
 Data in the model has changed.
 

Additional Inherited Members#

- Static Public Member Functions inherited from AObject
static void disconnect ()
 
template<AAnySignal Signal, aui::derived_from< AObjectBase > Object, ACompatibleSlotFor< Signal > Function>
static decltype(auto) connect (const Signal &signal, Object *object, Function &&function)
 Connects signal to the slot of the specified object.
 
template<AAnyProperty Property, aui::derived_from< AObjectBase > Object, typename Function>
static decltype(auto) connect (const Property &property, Object *object, Function &&function)
 Connects property to the slot of the specified object.
 
template<APropertyReadable PropertySource, APropertyWritable PropertyDestination>
requires requires { { *propertySource } -> aui::convertible_to<std::decay_t<decltype(*propertyDestination)>>; }
static void connect (PropertySource &&propertySource, PropertyDestination &&propertyDestination)
 Connects source property to the destination property.
 
template<APropertyWritable PropertySource, APropertyWritable PropertyDestination>
requires requires { { *propertySource } -> aui::convertible_to<std::decay_t<decltype(*propertyDestination)>>; { *propertyDestination } -> aui::convertible_to<std::decay_t<decltype(*propertySource)>>; }
static void biConnect (PropertySource &&propertySource, PropertyDestination &&propertyDestination)
 Connects source property to the destination property and opposite (bidirectionally).
 
template<AAnySignalOrProperty Connectable, aui::derived_from< AObjectBase > Object, ACompatibleSlotFor< Connectable > Function>
static decltype(auto) connect (const Connectable &connectable, Object &object, Function &&function)
 Connects signal or property to the slot of the specified object.
 
template<AAnySignalOrProperty Connectable, aui::derived_from< AObjectBase > Object, ACompatibleSlotFor< Connectable > Function>
static decltype(auto) connect (const Connectable &connectable, _< Object > object, Function &&function)
 Connects signal or property to the slot of the specified object.
 
template<AAnySignalOrProperty Connectable, aui::derived_from< AObjectBase > Object, typename Function>
static decltype(auto) connect (const Connectable &connectable, ASlotDef< Object *, Function > slotDef)
 Connects signal to the slot of the specified object. Slot is packed to single argument.
 
template<AAnyProperty Property, typename Object, ACompatibleSlotFor< Property > Function>
requires (!aui::derived_from<Object, AObject>)
static void connect (const Property &property, _< Object > object, Function &&function)
 Connects signal or property to the slot of the specified non-AObject type.
 
static void moveToThread (aui::no_escape< AObject > object, _< AAbstractThread > thread)
 
- Static Public Attributes inherited from AObject
static constexpr AObjectBaseGENERIC_OBSERVER = nullptr
 Indicates that a connection should not be explicitly linked to receiver's lifetime.
 
- Static Public Attributes inherited from AObjectBase
static ASpinlockMutex SIGNAL_SLOT_GLOBAL_SYNC
 
- Protected Member Functions inherited from AObject
void setThread (_< AAbstractThread > thread)
 Set thread of the object.
 
- Protected Member Functions inherited from AObjectBase
void clearAllIngoingConnections () noexcept
 
virtual void handleSlotException (std::exception_ptr exception)
 Called then an exception has thrown during slot processing of the signal emitted by this object.
 

Detailed Description#

template<typename Model>
class ADataBinding< Model >
Template Parameters
ModelYour model type.
Warning
This API is experimental. Experimental APIs are likely to contain bugs, might be changed or removed in the future.

If const reference of your model passed, ADataBinding will create and manage its own copy of your model.

If pointer of your model passed, ADataBinding will reference to your model and write directly to your model. When ADataBinding is destructed the pointer will not be deleted.

Example:

_new<ATextField>() && dataBinding(&User::username)

This code will bind ATextField with username field in the User model.

Another example:

class MyWindow: public AWindow {
public:
    MyWindow(): AWindow("Test") {
        struct Model {
            int value = 0;
        };
        auto data = _new<ADataBinding<Model>>(Model{});
        data->addObserver(&Model::value, [](int v) {
            ALogger::info("Debug") << "New value: " << v;
        });
        setContents(Centered {
            Vertical {
                Label { } let {
                    data->addObserver(&Model::value, [it](int v) {
                        it->setText("{}"_format(v));
                    });
                },
                Horizontal{
                    Button{"+"}.clicked(this, [data] {
                        data->getEditableModel().value += 1;
                        data->notifyUpdate();
                    }),
                    Button{"-"}.clicked(this, [data] {
                        data->getEditableModel().value -= 1;
                        data->notifyUpdate();
                    }),
                },
            }
        });
    }
};
void setContents(const _< AViewContainer > &container)
Moves (like via std::move) all children and layout of the specified container to this container.
Represents a window in the underlying windowing system.
Definition AWindow.h:45
#define let
Performs multiple operations on a single object without repeating its name (in place) This function c...
Definition kAUI.h:262
Button
Specifies button(s) to be displayed.
Definition AMessageBox.h:79

Here, we use getEditableModel() in order to change data in our model and notifyUpdate() to notify.

Also, we use let construction to define custom format for label.

Member Function Documentation#

◆ operator()() [1/5]#

template<typename Model>
template<typename Data>
auto ADataBinding< Model >::operator() ( DataModel::* field)
inline
Parameters
fieldpointer-to-member-field of model.

ADataBindingDefault must be defined for model type and your view.

◆ operator()() [2/5]#

template<typename Model>
template<typename Data, aui::invocable< Data > Projection>
ADataBindingLinker2< Model, Data, Projection > ADataBinding< Model >::operator() ( DataModel::* field,
Projection projection )
inline

ADataBindingDefault must be defined for model type and your view.

◆ operator()() [3/5]#

template<typename Model>
template<typename View, typename ModelField, typename GetterRV, aui::invocable< View *, const ModelField & > Setter>
auto ADataBinding< Model >::operator() ( ModelFieldModel::* field,
ASignal< GetterRV >View::* getter,
Setter setter = (void(View::*)(const ModelField&))nullptr )
inline
Parameters
fieldpointer-to-member-field of model.
getterpointer-to-member-field of view's signal
setterpointer-to-member-field of view's setter

◆ operator()() [4/5]#

template<typename Model>
template<typename ModelField, typename SetterLambda>
auto ADataBinding< Model >::operator() ( ModelFieldModel::* field,
SetterLambda setterLambda )
inline
Parameters
fieldpointer-to-member-field of model.
setterLambdalambda which accepts reference to your view type and const reference to data (see examples).

View type is deduces from the first argument of your lambda.

struct Model {
  AString text;
};
auto model = _new<ADataBinding<Model>>(Model{});
_new<ALabel>() && model(&Model::text, [](ALabel& view, const AString& data) {
   view.setText(data);
});
Represents a Unicode character string.
Definition AString.h:38

◆ operator()() [5/5]#

template<typename Model>
template<typename View, typename ModelField, typename SetterArg>
auto ADataBinding< Model >::operator() ( ModelFieldModel::* field,
void(View::* setterFunc )(SetterArg) )
inline
Parameters
fieldpointer-to-member-field of model.
setterFuncpointer-to-member-function setter.