Skip to content

Commit

Permalink
introduce lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v committed Sep 14, 2023
1 parent c7a9438 commit 0b22328
Show file tree
Hide file tree
Showing 6 changed files with 622 additions and 128 deletions.
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ This section will explain in detail how the Library is to be used from the Ardui
<br><br>
Alternativly you may use the extended callback funtion which provides three parameters to the callback function `myCallback(Control *sender, int eventname, void * UserParameter)`. The `UserParameter` is provided as part of the `ESPUI.addControl` method set and allows the user to define contextual information that is to be presented to the callback function in an unmodified form.
<br><br>
The below example creates a button and defines a lambda function to implicitly create an `ExtendedCallback` which then invokes a more specialized button callback handler. The example uses the `UserParameter` to hold the `this` pointer to an object instance, providing a mechanism for sending the event to a specific object without the need for a switch / map / lookup translation of the Sender Id to an object reference.
It also possible to use a lambda function in the callback parameter. It also allows the user to define, in a more C++ way, contextual information in any form. This is shown by the [completeLambda](examples/completeLambda/completeLambda.ino) example.
<br><br>
The below example creates a button and defines a lambda function to invoke a more specialized button callback handler:
```
void YourClassName::setup()
{
Expand All @@ -163,26 +165,18 @@ void YourClassName::setup()
" Button Face Text ",
ControlColor::None,
ParentElementId,
[](Control *sender, int eventname, void* param)
[&](Control *sender, int eventname)
{
if(param)
{
reinterpret_cast<YourClassName*>(param)->myButtonCallback(sender, eventname);
}
},
this); // <-Third parameter for the extended callback
myButtonCallback(sender, eventname); // class method
});
// or
ButtonElementId = ESPUI.button(
" Button Face Text ",
[](Control *sender, int eventname, void* param)
[&](Control *sender, int eventname)
{
if(param)
{
reinterpret_cast<YourClassName*>(param)->myButtonCallback(sender, eventname);
}
},
this); // <-Third parameter for the extended callback
myButtonCallback(sender, eventname); // class method
});
}
```
```
Expand Down
Loading

0 comments on commit 0b22328

Please sign in to comment.