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.
This commit is contained in:
intolerantape
2021-12-02 07:13:11 -08:00
parent 6ed655f8c3
commit 8d758ced15

View File

@@ -89,6 +89,20 @@ public:
_lambda = nullptr; _lambda = nullptr;
} }
/// <summary>
/// Initializes a new instance of the <see cref="Function"/> class.
/// </summary>
template<typename T>
Function(const T& lambda)
{
_lambda = (Lambda*)Allocator::Allocate(sizeof(Lambda) + sizeof(T));
_lambda->Refs = 1;
_lambda->Dtor = [](void* callee) -> void { static_cast<T*>(callee)->~T(); };
_function = [](void* callee, Params ... params) -> ReturnType { return (*static_cast<T*>(callee))(Forward<Params>(params)...); };
_callee = (byte*)_lambda + sizeof(Lambda);
new(_callee) T(lambda);
}
Function(const Function& other) Function(const Function& other)
: _callee(other._callee) : _callee(other._callee)
, _function(other._function) , _function(other._function)