s00500 / ESPUI

A simple web user interface library for ESP32 and ESP8266
https://valencia.lbsfilm.at/midterm-presentation/
Other
932 stars 174 forks source link

Unable to use class lambda - no matching function to call #289

Closed vbartusevicius closed 4 months ago

vbartusevicius commented 10 months ago

Describe the bug Trying to use a lambda callback as in the README:

    ESPUI.addControl(
        ControlType::Number, 
        "Distance to sensor when \"empty\" (cm)", 
        "100", 
        color, 
        Control::noParent,
        [&](Control *sender, int eventname) {
            this->handleCallback(sender, eventname);
        }
    );

Getting an error:

Admin.cpp:31:5: error: no matching function for call to 'ESPUIClass::addControl(ControlType, const char [37], const char [4], ControlColor&, const uint16_t&, WebAdmin::begin()::<lambda(Control*, int)>)'

Using PlatformIO and s00500/ESPUI@^2.2.3

MartinMueller2003 commented 10 months ago

I am using Lambda all the time. The issue is that you cannot capture "this" easily. I pass the this pointer in as a user argument. Here is the syntax I am using:

ControlId = ESPUI.addControl (
    uiControltype,
    Title.c_str (),
    GetDataValueStr (),
    color,
    TabId,
    [] (Control * sender, int type, void * UserInfo)
    {
        if (UserInfo)
        {
            static_cast <cControlCommon *> (UserInfo)->Callback (sender, type);
        }
    },
    this);
vbartusevicius commented 10 months ago

Yes, that works. I just want to ask if there is an error in the documentation or the code. Because the docs say: image I think the type-definition could be adjusted to allow capturing this.

Personally, I use the UserInfo for different purposes. Wrapping additional structure to incorporate this seems not the best idea.

MartinMueller2003 commented 10 months ago

I happen to use this. You can point at any structure you want and interpret it any way you want. Ultimate flexibility.

thespielplatz commented 10 months ago

I am using Lambda all the time. The issue is that you cannot capture "this" easily. I pass the this pointer in as a user argument. Here is the syntax I am using:

ControlId = ESPUI.addControl (
    uiControltype,
    Title.c_str (),
    GetDataValueStr (),
    color,
    TabId,
    [] (Control * sender, int type, void * UserInfo)
    {
        if (UserInfo)
        {
            static_cast <cControlCommon *> (UserInfo)->Callback (sender, type);
        }
    },
    this);

Thanks a lot! I was missing this!!!