BAN: Add basic lambda support for BAN::Function

This commit is contained in:
Bananymous 2023-01-18 14:39:31 +02:00
parent ef2e8f1a2f
commit dd607ce86f
1 changed files with 22 additions and 0 deletions

View File

@ -30,6 +30,12 @@ namespace BAN
static_assert(sizeof(CallableMemberConst<Own>) <= m_size); static_assert(sizeof(CallableMemberConst<Own>) <= m_size);
new (m_storage) CallableMemberConst<Own>(function, owner); new (m_storage) CallableMemberConst<Own>(function, owner);
} }
template<typename Lambda>
Function(Lambda lambda)
{
static_assert(sizeof(CallableLambda<Lambda>) <= m_size);
new (m_storage) CallableLambda<Lambda>(lambda);
}
Ret operator()(Args... args) Ret operator()(Args... args)
{ {
@ -103,6 +109,22 @@ namespace BAN
Ret(Own::*m_function)(Args...) const = nullptr; Ret(Own::*m_function)(Args...) const = nullptr;
}; };
template<typename Lambda>
struct CallableLambda : public CallableBase
{
CallableLambda(Lambda lambda)
: m_lambda(lambda)
{ }
virtual Ret call(Args... args) override
{
return m_lambda(Forward<Args>(args)...);
}
private:
Lambda m_lambda;
};
private: private:
static constexpr size_t m_size = sizeof(void*) * 4; static constexpr size_t m_size = sizeof(void*) * 4;
alignas(max_align_t) uint8_t m_storage[m_size] { 0 }; alignas(max_align_t) uint8_t m_storage[m_size] { 0 };