Lambda Functions with Elementary - C++11

With this tutorial we'll give you a better view of how the lambda function can and will be constantly use in the C++ bindings.

For a more broad aproach you should do a little web research.

The syntax adopted for these examples:

[capture] (parameters) {body}

capture: Determinate how and if the capture occurs. Possible indicators, two or more should be intercalated by commas:

parameters: List of parameters necessary for each specific lambda function.

body: Function body

Let's start with a more simple lambda and later a more complex one, all extracted from elementary examples:

First Example - Button - Hello, Button! :

button_cxx_example_00.png
::elm::button btn(efl::eo::parent = win);
auto on_click = std::bind([] () { elm_exit(); });
btn.callback_clicked_add( on_click );

In this example we use a lambda function for elm::button btn that will be called when that button is clicked in callback_clicked_add( on_click ). This lambda will then ask to exit Elementary's main loop with elm_exit(). If this call is issued, it will flag the main loop to cease processing and return back to its parent function, usually your elm_main() function.

Now let's analize the sintax used for this lambda:

With [] we are signaling that we don't want to capture any variables and with () we are indicating that this lambda doesn't need parameters to work as it should. Now the important part of this function it's the body represented by {} where we are applying elm_exit() everytime this lambda is called.

In this case we are using std::bind to bind the parameters of our lambda function to return as std::function object to on_click which was declare as auto.

For this example with std::bind we simplified our work simply because we didn't have to search in the code or documentation of Elementary to look for the parameters and/or values that the callback_clicked_add requires of the function we are adding.

Second Example - Hoversel example with C++ Binding :

hoversel_cxx_example_01.png
auto add_item = std::bind([] (::elm::hoversel obj)
{
static int num = 0;
char *str = new char[sizeof(char) * 10];
snprintf(str, 10, "item %d", ++num);
elm::widget_item hoversel_item = obj.item_add(str, nullptr, ELM_ICON_NONE, NULL, str);
elm_object_item_del_cb_set(hoversel_item._eo_ptr(), &_free);
}, std::placeholders::_1);
hoversel.callback_clicked_add(add_item);

In this example we use a lambda function for hoversel that will be called when that hoversel is clicked in callback_clicked_add( add_item ). This lambda will then add an item to heversel, note that since we allocate memory for the item we need to know when the item dies so we can free that memory.

Now let's analize the sintax used for this lambda:

When the function object returned by bind is called, an argument with placeholder _1 is replaced by the first argument in the call, _2 is replaced by the second argument in the call, and so on.

In this case we are using std::bind to bind the parameters of our lambda function to return as std::function object to add_item which was declare as auto.

See also
Consult all examples from elementary with C++ Bindings here