开发者

Calling member functions from DLL (AI library for games)

My problem is this:

I am trying to implement a C++ library which开发者_StackOverflow社区 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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜