源码理解 UE4中的 FCookStatsManager::FAutoRegisterCallback RegisterCookStats
官方文档:https://dev.epicgames.com/documentation/zh-cn/unreal-engine/API/Runtime/Core/ProfilingDebugging/FCookStatsManager 文档中的注释: When a cook a complete that is configured to use stats (ENABLE_COOK_STATS), it will broadcast this delegate, which, when called, gives you a TFunction to call to report each of your stats. 代码: FCookStatsManager::FAutoRegisterCallback RegisterCookStats([](FCookStatsManager::AddStatFuncRef AddStat) { ……
首先,RegisterCookStats是一个全局(非静态)变量,它的类型是 FCookStatsManager::FAutoRegisterCallback ,它调用的初始化器是 ( []()... ) ,也就是说,传入了一个lambda函数,作为初始化器的参数。
那么看FCookStatsManager::FAutoRegisterCallback的声明如下,可以得知,所有声明一个 FCookStatsManager::FAutoRegisterCallback 结构体,本质上就是给 static CookStatsCallbacks 静态变量添加元素(也就是该lambda方法)。
/** Use this to register a callback to gather cook stats for a translation unit. When a cook is finished, this delegate will be fired. */ static CORE_API FGatherCookStatsDelegate CookStatsCallbacks; /** Called after the cook is finished to gather the stats. */ static CORE_API void LogCookStats(AddStatFuncRef AddStat); /** Helper struct to auto-register your STATIC FUNCTION with CookStatsCallbacks */ struct FAutoRegisterCallback { template<typename Func> explicit FAutoRegisterCallback(Func Callback) { CookStatsCallbacks.AddLambda(Callback); } };