Skip to content

Commit

Permalink
CallbackLambda: allow usage of lambda with no parameters on callbacks…
Browse files Browse the repository at this point in the history
… that have at least one
  • Loading branch information
JonathSpirit committed Sep 11, 2024
1 parent a754f33 commit afb98aa
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions includes/FastEngine/C_callback.inl
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,38 @@ bool CallbackObjectFunctor<TObject, Types...>::check(void* ptr)

//CallbackLambda

namespace
{

template<typename T>
struct lambda_traits : public lambda_traits<decltype(&T::operator())>
{};

template<typename ClassType, typename ReturnType, typename... Args>
struct lambda_traits<ReturnType (ClassType::*)(Args...) const>
{
enum : std::size_t
{
ArgCount = sizeof...(Args)
};
};

} // namespace

template<class... Types>
template<typename TLambda>
CallbackLambda<Types...>::CallbackLambda(TLambda const& lambda) :
g_lambda(new TLambda(lambda))
{
this->g_executeLambda = [](void* lambdaPtr, Types... arguments) {
return (*reinterpret_cast<TLambda*>(lambdaPtr))(arguments...);
this->g_executeLambda = [](void* lambdaPtr, [[maybe_unused]] Types... arguments) {
if constexpr (lambda_traits<TLambda>::ArgCount != 0)
{
return (*reinterpret_cast<TLambda*>(lambdaPtr))(arguments...);
}
else
{
return (*reinterpret_cast<TLambda*>(lambdaPtr))();
}
};
this->g_deleteLambda = [](void* lambdaPtr) { delete reinterpret_cast<TLambda*>(lambdaPtr); };
}
Expand Down

0 comments on commit afb98aa

Please sign in to comment.