Lomse library. API documentation
0.30.0
|
It is very important to understand how a callback works and how to use a class method as callback.
A callback function is just a function that is called through a function pointer, as in this example:
The problem with callbacks when using C++ is that a callback is just a C function, but not a C++ class method. As you know, to invoke an object's method you need a reference or pointer to the object, as in this example:
The only exception to this is when the method is static. In this case you will directly invoke the method as in:
Therefore, to use a method as callback it is necessary to pass two parameters, a pointer to the object instance, and a pointer to the method. But as the Lomse callback is a C callback, only one parameter is passed: the pointer to the method. As a consequence, only static methods can be invoked. But this is a very strong limitation as static members can only access static variables.
A simple solution used in Lomse, that works for both C and C++ programs, is to add another parameter to the function that sets up the callback. This parameter is a pointer to the object instance (for C++ programs) or NULL (for C programs). The callback is still an static method but it will receive the object instance pointer as parameter and, thus, non-static members can be invoked from inside the static method.
Example:
And inform lomse about it. We do it at lomse initialization: