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_, and then resolve the agile_ 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.
I hope explanation of what an “agile delegate” is is coming in part 2!
I think “agile” in COM means it’s not bound to a specific thread, so I’d guess “agile delegate” would be one which can be invoked from different threads.