Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass C++ callbacks between unrelated classes?

In a non-boost project, I have a class which uses a timer based on a certain user action (button pressed/released). I want this class generic, so it takes callbacks for user defined actions.

// TimerClass.h
typedef void (*timerCallback)(void);
...
Class TimerClass : public CButton {
public:
    void setCallbackShortTime(timerCallback clickFn) { shortCallback = clickFn;} ;
    void setCallbackLongTime(timerCallback clickFn) { longCallback = clickFn;} ;
private:
    timerCallback shortCallback, longCallback;
}


// CMyDlg.h
class CMyDlg : public CDialog
{
public:
    void openLiveViewWindow();
    void openAdminPanelWindow();
    TimerClass _buttonSettings;
}

// CMyDlg.cpp
...
_buttonSettings.setCallbackShortTime(&openLiveViewWindow);
...

Now, from another class (DialogClass) I can use the TimerClass but I cannot pass function pointers to the callback functions. These functions are not static. The compiler ends up complaining:

error C2276: '&' : illegal operation on bound member function expression

Some research on this pointed out to std::function() and std::bind() but I'm not familiar with these and would appreciate some pointers on how to resolve this.

RESOLUTION: For anyone interested, here are the bricks of the final solution

// TimedButton.h
#include <functional>
// define timer callback prototype
typedef std::function<void()> timerCallback;
...
class TimedButton : public CButton
{
public:
    TimedButton();
    ...
    void setCallbackShortTime(timerCallback clickFn) { _shortCallback = clickFn;} ;
    void setCallbackLongTime(timerCallback clickFn) { _longCallback = clickFn;} ;
private:
    timerCallback _shortCallback;
    timerCallback _longCallback;
}

// TimedButton.cpp
...
(_longCallback)();  // call long duration fn
...
(_shortCallback)();     // call short duration fn

// in MyDlg.cpp
#include <functional>
...
_buttonSettings.setCallbackShortTime(std::bind(&CMyDlg::openLiveViewWindow, this));
_buttonSettings.setCallbackLongTime(std::bind(&CMyDlg::openAdminPanelWindow, this));
like image 564
fduff Avatar asked Aug 13 '12 18:08

fduff


People also ask

How are callbacks implemented in C?

Callbacks in C are usually implemented using function pointers and an associated data pointer. You pass your function on_event() and data pointers to a framework function watch_events() (for example). When an event happens, your function is called with your data and some event-specific data.

Should callbacks be static?

Callbacks need to be static so that they don't have an implicit this parameter as the first argument in their function signature. Show activity on this post. Non-static methods require a 'this' instance, and can only be called upon an object instance.

What is callback function in C with example?

We can define it in other words like this: If the reference of a function is passed to another function argument for calling, then it is called the callback function. In C we have to use the function pointer to call the callback function. The following code is showing how the callback function is doing its task.

What is a callback class?

In the simplest terms a callback is a code that you pass into another method. E.g. you have a class A which calls a method on class B but you need some code running from Class A when it's finished. You put your code in its own new method on class A and you pass the method name in when you call the method on class B.


2 Answers

The old-fashioned way to do this is to make your callback function accept an additional void* parameter, for which you pass a pointer to the object you wish to call the function on. Then you use a static member function for the callback and let it cast the pointer to the proper object and call your true callback.

typedef void (*timerCallback)(void*);
...
void setCallbackShortTime(timerCallback clickFn, void* object) { shortCallback = clickFn; shortCallbackObject = object;} ;
void setCallbackLongTime(timerCallback clickFn, void* object) { longCallback = clickFn; longCallbackObject = object;} ;
...

static void openLiveViewWindowCallback(void* object) { ((CMyDlg*)object)->openLiveViewWindow(); }
void openLiveViewWindow();
like image 77
Mark Ransom Avatar answered Sep 26 '22 03:09

Mark Ransom


std::function is a polymorphic function object, which can wrap up any type of callable object with a particular signature. In your case, you want it to take no arguments and return no value, so you can define:

typedef std::function<void()> timerCallback;

std::bind allows you to adapt a callable object to one of a different signature, by binding arguments to parameters. In your case, you want to adapt a member function by binding it to a particular object to invoke it on:

_buttonSettings.setCallbackShortTime(std::bind(&CMyDlg::openLiveViewWindow, this));

Note that these were introduced in 2011, so older compilers won't support them. In that case, you could either use the very similar Boost or TR1 libraries, or make your own callable class containing a pointer-to-member-function and a pointer/reference to the object you want to invoke it on.

like image 25
Mike Seymour Avatar answered Sep 25 '22 03:09

Mike Seymour