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));
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.
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.
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With