简体   繁体   English

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

[英]Converting Fast Delegate to std function

i'm trying to convert FastDelegate into a std::function, but am having trouble understanding the syntax. 我正在尝试将FastDelegate转换为std :: function,但是在理解语法时遇到了麻烦。

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

and the code i'm trying to convert: 和我要转换的代码:

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

//some code to find the event

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

So far this can't work: 到目前为止,这是行不通的:

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

This may not be a complete answer as I'm not entirely sure what the fastDelegate library is support to do other than offer the callback functionality. 这可能不是一个完整的答案,因为我不完全确定除了提供回调功能外,fastDelegate库还支持做什么。 The original code there comes from Game Coding Complete by Mike Shaffry & Dave Graham. 原始代码来自Mike Shaffry和Dave Graham编写的Game Coding Complete。

So I may be missing the part where the books says the fastDelegate Library can attach member variables. 因此,我可能缺少书籍中所说的fastDelegate库可以附加成员变量的部分。 So since you have a void function with IEventData as a parameter, all your callback functions will be the same. 因此,由于您具有将IEventData作为参数的void函数,因此所有回调函数都将相同。

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

Then defined somewhere else in your solution would be the actualy "Functor" which is just a typedef for std::function. 然后在解决方案中的其他地方定义实际的“ Functor”,它只是std :: function的typedef。 Such as: 如:

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

That's pretty much the gist of how it works. 这几乎是其工作原理的要点。 I know this post is old but I figured I'd answer anyways since I was just figuring it out at 4 am myself 我知道这篇文章很旧,但我想我还是会回答,因为我自己是在凌晨4点才知道的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM