From 8d758ced1552c45c09f7e54234dcb4c9413759da Mon Sep 17 00:00:00 2001 From: intolerantape Date: Thu, 2 Dec 2021 07:13:11 -0800 Subject: [PATCH] Added a constructor for C++ Function class to support initialization directly from reference-captured lambda. Previously, Function had to initialize reference-captured lambdas with the Bind() method. --- Source/Engine/Core/Delegate.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Source/Engine/Core/Delegate.h b/Source/Engine/Core/Delegate.h index bba43eb16..9269fec9d 100644 --- a/Source/Engine/Core/Delegate.h +++ b/Source/Engine/Core/Delegate.h @@ -89,6 +89,20 @@ public: _lambda = nullptr; } + /// + /// Initializes a new instance of the class. + /// + template + Function(const T& lambda) + { + _lambda = (Lambda*)Allocator::Allocate(sizeof(Lambda) + sizeof(T)); + _lambda->Refs = 1; + _lambda->Dtor = [](void* callee) -> void { static_cast(callee)->~T(); }; + _function = [](void* callee, Params ... params) -> ReturnType { return (*static_cast(callee))(Forward(params)...); }; + _callee = (byte*)_lambda + sizeof(Lambda); + new(_callee) T(lambda); + } + Function(const Function& other) : _callee(other._callee) , _function(other._function)