I needed to use a C library in OF and digging through the code of the events implementation I came up with a wrapper to get pointers to member methods. As I’m not really 100% sure about this approach I was hoping any experienced dev could comment on my approach.
For example I have a method which defines a button with a callback:
button(ICON_COLOR, "Some text", callback);
To use this in OF the callback needs to be a static member:
button(ICON_COLOR, "Some text", ofApp::callback);
which is trouble as I don’t have access to ‘this’ anymore.
Unless I’m missing something, I dont’ think you can use your object the way you are showing here. The object is an automatic variable within the declaration of button(), meaning it will be disposed as soon as the line
Yeah, indeed I’m not gaining then. I just found out my wrapper doesn’t work also. It will work once. All registered callbacks will end up being the last registered callback.
Apparently there are some methods around it by using lambdas:
However that’s really nasty (c++11)code as well which I need to acquaint myself with first.
Apparently the usual approach is to have some userdata pointer added to the C library in which you can store a pointer to this. So I’m now looking into modifying the C library to accept an extra pointer to store the this context.
Unfortunately I don’t have time to flesh this out fully, but it would seem that you could make a CCallback mixin class and the make your own subclass of the OF button.
Below is a brief example of what I mean – typed straight into the browser and totally untested, but conceptually it should be sound.
This way the class is attached to button, gets automatically destroyed when the button gets destroyed etc.
class CCallback
{
CCallback(
Func inCallback,
void* inUserData) :
mCallback(inCallback),
mUserData(inUserData)
{
}
Func mCallback;
void* mUserData;
void Perform()
{
(*mCallback)(mUserData);
}
};
CMyButtonClass : public COFButton, public CCallback
{
CMyButtonClass(
int inButtonParam1,
int inButtonParam2,
Func inFunction,
void* inUserData) :
COFButton(inButtonParam1, inButtonParam2),
CCallback(inCallback, inUserData)
{
}
}