Skip to content

Add support for lambda functions. #112

@reifiedsteve

Description

@reifiedsteve

In particular, being able to use this useful little class with callbacks that are lambdas-with-context would make this much more useful for use with object member functions. For example:

class MyClass
{
public:

    MyClass()  : _button(7) {
        _button.attachClick([this]() { this->_myFunc(); });
    }

private:

    void _myFunc() {
        // ... do stuff when single clicked.
    }

    OneButton _button;
};

Something along the lines of what is already there, but just extrapolating it...

#include <functional>
...

class OneButton
{
public:
    ...
    typedef std::function<void()> callbackLambda;   // <== ADDED.
    ...
    callbackLambda _clickLambda;   // <== ADDED.
    ...
    void attachClick(callbackLambda newLambda) {    // <== ADDED new method.
        _clickLambda = newLambda;       
    }    

    ...
    void OneButton::Tick(bool activeLevel) 
    {
        ...
        case OneButton::OCS_COUNT:
            ...
            if (_nClicks == 1) {
                // this was 1 click only.
                if (_clickFunc) _clickFunc();
                if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
                if (_clickLambda) _clickLambda();         // <== ADDED.

etc..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions