From dd607ce86f43a57a590717c778a0f00c216dfd1c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 18 Jan 2023 14:39:31 +0200 Subject: [PATCH] BAN: Add basic lambda support for BAN::Function --- BAN/include/BAN/Function.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/BAN/include/BAN/Function.h b/BAN/include/BAN/Function.h index bc210aaa3f..94c30f9399 100644 --- a/BAN/include/BAN/Function.h +++ b/BAN/include/BAN/Function.h @@ -30,6 +30,12 @@ namespace BAN static_assert(sizeof(CallableMemberConst) <= m_size); new (m_storage) CallableMemberConst(function, owner); } + template + Function(Lambda lambda) + { + static_assert(sizeof(CallableLambda) <= m_size); + new (m_storage) CallableLambda(lambda); + } Ret operator()(Args... args) { @@ -103,6 +109,22 @@ namespace BAN Ret(Own::*m_function)(Args...) const = nullptr; }; + template + struct CallableLambda : public CallableBase + { + CallableLambda(Lambda lambda) + : m_lambda(lambda) + { } + + virtual Ret call(Args... args) override + { + return m_lambda(Forward(args)...); + } + + private: + Lambda m_lambda; + }; + private: static constexpr size_t m_size = sizeof(void*) * 4; alignas(max_align_t) uint8_t m_storage[m_size] { 0 };