Here is what I did:
I created a register function within my library which took two arguments, one is a pointer to a function and one a pointer to void. These pointers are then stored inside the library.
The GUI is now require to call this register function with a pointer to itself and a pointer to a static function (cast to *void).
The lib will now use these two pointer when it needs to talk to the GUI. It calls the first pointer (which is a pointer to a function) where the first argument is the pointer-to-void (which is actually a pointer to the GUI instance running).
The static function (which has just been called by the lib) knows the actual type of the first argument (the class type of the GUI) and can therefore cast the pointer to this type and call the public functions of the GUI class.
So to sum it up, what I now require the GUIs to define if they are to use my library are a static function which looks like this:
Code:
void function(void *ptr)
{
MyGUIClass *gui = (MyGUIClass*)ptr;
ptr->refresh_function();
}
And second: the GUI has to register itself and the static function:
Code:
// inside the MyGUIClass class
Library *lib = new Library((void*)this, function);
Note how I decided to offer a constructor which automaticly registered the two pointers.
This works fine for me, but I would like to hear if there are some comments about it. Maybe it is not optimal, maybe there can be further improvements?