Qaz
Qaz

Reputation: 61900

Generic Callback

I'm trying to store a callback in a class. Currently, I do something like this:

struct Callback {
    Callback (std::function<void ()> func) : func_ (func){}
    void call() const { func(); }

private:
    std::function<void ()> func_;
};

As you can see, only a specific type of function (currently no return and no parameters) can be used.

Is there any way I could use such a class like this, where I pass it what to call it with?

void increment (int &n) {
    ++n;
}

int main() {
    int someNum = 5;
    Callback callback (increment, someNum); //will call `increment (someNum);`
}

I was thinking to use a parameter pack to store the arguments, and a typename to store the return type, and then making an std::function<ReturnType (Args)> callback_ sort of thing, and calling it with something like callback_ (givenArgs...);. I'm not really knowledgeable enough on templates to do this, however, or even figure out if it's possible.

The real use I'd be getting out of this (at least right now) is for a timer, but perhaps making a small generic_function<> class that wraps an std::function<> would help more. For this example, though, a timer that pauses and unpauses every 2 seconds:

void togglePause (Countdown &c) {
    c.togglePause();
}

int main() {
    Countdown main (10000); //create countdown of 10s
    Countdown delay (2000, togglePause, main); //this one calls func when expired

    for (; !main.expired();) { //go while unpaused time < 10s
        delay.wait().reset(); //wait 2s, call callback, reset back to 2s
    }
}

Of course this can be applied to other concepts as well, but I'm not sure how to go about attaining this syntax in the first place. I can build two different forms in case the return type is void just fine from an unrelated previous question, but storing a function with any number and types of arguments confuses me. If it's possible, how can I use syntax like this? If not, how close would the syntax be?

Upvotes: 1

Views: 894

Answers (1)

David Norman
David Norman

Reputation: 19879

I think you just want to use std::bind to convert your function with an argument into a function taking no arguments:

int main() {
    int someNum = 5;
    std::function<void (void)> boundFunc = std::bind(increment, std::ref(someNum));
    Callback callback (boundFunc); //will call `increment (someNum);`
}

Note that you need the std::ref to ensure that someNum is passed by reference and that you need to make sure that someNum stays in scope longer than the callback.

Upvotes: 5

Related Questions