aui: ADataBinding bad design

At the moment, ADataBinding envolves a lot of boilerplate instead of reducing it.

Example usecases

Increase integer field by 1

Expected:

 mViewModel.integerField += 1;

Actual:

 mViewModel.setValue(&ViewModel::integerField, mViewModel->integerField + 1);

Direct and indirect field binding:

Direct binding is almost acceptable:

_new<ALabel>() && mViewModel(&ViewModel::stringField),

But what about label and integer binding?

_new<ALabel>() let {
  mViewModel.addObserver(&ViewModel::integerField, [=](int v) {
    it->setText("The value is: {}"_format(v));
  });
},

Boss music: lists

I.e. I want to make a basic todo app:

struct ViewModel {
  struct Item {
    AString title;
    bool done = false;
  };
  AVector<Item> items;
};

I have no idea how to do this.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 29 (6 by maintainers)

Most upvoted comments

Very nice approaches here, I’m already seeing in which use cases they will be helpful to me! The third design seems to be closer of what we have in C#, but the trailing parenthesis make it somehow unconventional. I could have tried something too if I was familiar to this level of metaprogramming. But at this stage I think it’s on a very good path, theses can even be wrapped in macros, like AUI_ACCESSOR_PROPERTY(name, &CustomSetter::setName, &CustomSetter::getName) for custom get/set and AUI_MEMBER_PROEPRTY(AString, name) for default get/set.

Anyway, I’m eager to see where those things are going!

The third design

I’m trying to address here {this} issue of the second design. As the second design, this one is intended for referencing external setter/getter/signal. I’m injecting pointer to this by method:

template<typename M, typename SetterArg, typename Ignored1, typename GetterReturnT>
struct APropertyDef {
    using Field = std::decay_t<SetterArg>;
    using Model = M;
    const Model* model;
    Ignored1(Model::*setter)(SetterArg);
    GetterReturnT(Model::*getter)() const;

    template<aui::convertible_to<Field> U>
    APropertyDef& operator=(U&& u) {
        std::invoke(setter, const_cast<Model*>(model), std::forward<U>(u));
        return *this;
    }

    operator GetterReturnT() const noexcept {
        return std::invoke(getter, model);
    }
};

TEST(PropertyTest, CustomSetter) {
    class CustomSetter: public AObject {
    public:
        MOCK_METHOD(void, setName, (const AString& msg));
        AString getName() const {
            return "can't change";
        }

        auto name() const {
            return APropertyDef {
                .model = this,
                .setter = &CustomSetter::setName,
                .getter = &CustomSetter::getName,
            };
        }
    } u;

    EXPECT_CALL(u, setName(AString("World")));
    u.name() = "World";
    EXPECT_EQ(AString(u.name()), "can't change");
}
  • ✅ custom getter/setter without runtime overhead to call
  • ✅ okayish read syntax: AString(u->name())
  • ✅ okayish signal connection syntax: connect(u->name().changed, …) if value is changed, connect(u->name(), …) if value is changed + initial prefire (requires additional overload to AObject::connect but not a big deal)
  • ✅ invokeSignal does not lose emitter
  • ❌ confusing assignment syntax: u->name() = “Some Name”;``u->name() += " Surname" (still better than ADataBinding)
  • ❌ ~field definition: 2+ lines~ we can use this for customisable getter/setter properties only
  • ❌ no designated initialisers (for a model)