在 C++/WinRT 中制作 Windows 运行时委托的敏捷版本(第 1 部分)Making an agile version of a Windows Runtime delegate in C++/WinRT, part 1
本系列文章的第一部分介绍了如何在 C++/WinRT 中创建 Windows 运行时委托的敏捷版本。文章从最简单的情况入手,演示了基础场景下的实现方法,为后续更复杂的异步和线程模型处理奠定基础。
Raymond Chen
Suppose you have some C++/WinRT code that receives a delegate from an outside source, and you might invoke that delegate from a potentially different COM context. However, the original delegate may not be agile. How can you make an agile version of that delegate?
The easy way is to wrap the delegate in an agile_ref, and then resolve the agile_ref back to a delegate when you want to invoke it.
template<typename Delegate>
Delegate make_agile_delegate(Delegate const& d)
{
return [agile = winrt::agile_ref(d)](auto&&...args) {
return agile.get()(std::forward<decltype(args)>(args)...);
};
}But if it were that easy, why would we call this article “part 1”?
More in part 2.
Author
Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.
需要完整排版与评论请前往来源站点阅读。