tags:

views:

57

answers:

1

My problem is this:

I am trying to implement a C++ library which can call functions in the base EXE. I am trying to have a minimum amount of code on the EXE side to get the functionality to work.

At the moment I have DLL-side entities which are created by DLL function calls. These entities hold a container of "actions" which is just a map of strings (action names) and pointers to the EXE-side functions. Each of these functions return void and take as an argument a container of boost::any. The EXE then just has to call an update function in the DLL every tick and the DLL does the thinking and calls whichever "action" is appropriate.

This is working fine for normal functions. How can I get this to work for member functions also? (Where the DLL has no idea what kind of object the function resides in.)

Please let me know if my question is too vague and I will try to clarify.

Thanks!

+1  A: 

What you want is boost::bind and boost::function.

The dll side map would have boost::functions instead of function pointers. The client would pass a bound object using boost::bind which could be anything you want.

DLL Side:

typedef boost::function<void ()> CallbackFunctionType;

CallbackFunctionType gCallback;

void SetCallback(CallbackFunctionType& callback)
{
    gCallback = callback;
}

void Update
{
    if (condition == true)
    {
        gCallback();
    }
}

And on the Client side:

class CallbackClass
{
    void CallbackMember();
};

CallbackClass x;

dllInterface->SetCallback(bind(&CallbackClass::CallbackMember, &x));

dllInterface->Update();

Hope this is what you were looking for.

bind member

DominicMcDonnell
Seems straightforward enough but this doesn't seem to cover passing arguments? Is this possible?Also is it possible to do the binding on the DLL side to avoid the EXE having to include boost libraries or is this opening a can of worms?
Sebastian Edwards