将快速委托转换为标准函数

Converting Fast Delegate to std function

我正在尝试将 FastDelegate 转换为 std::function,但我无法理解语法。

这是委托库: http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

以及我要转换的代码:

typedef shared_ptr<IEventData> IEventDataPtr;  
typedef fastdelegate::FastDelegate1<IEventDataPtr> EventListenerDelegate;

//some code to find the event

EventListenerDelegate listener = (*it);  
listener(pEvent); // call the delegate

到目前为止这还行不通:

typedef std::function<std::shared_ptr<IEventData>> Functor;
Functor listener = (*it);
listener(pEvent); // call the delegate

这可能不是一个完整的答案,因为除了提供回调功能之外,我不完全确定 fastDelegate 库支持做什么。 那里的原始代码来自 Mike Shaffry 和 Dave Graham 的 Game Coding Complete。

所以我可能遗漏了书中所说的 fastDelegate 库可以附加成员变量的部分。因此,由于您有一个以 IEventData 作为参数的 void 函数,因此您的所有回调函数都将相同。

    typedef std::function<void(IEventData)> Functor;
    Functor listener = (*it);
    listener(pEvent);

然后在您的解决方案中的其他地方定义的实际 "Functor" 只是 std::function 的类型定义。如:

   void GameCode::DelegateFunction(IEventData){
   //do stuff with event
   }

这就是它的工作原理。我知道这个 post 已经过时了,但我想我还是会回答,因为我自己是在凌晨 4 点才弄明白的