From d3353488e6243f8cdfae745946d8eeb0fd1ced43 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 16 Sep 2024 19:55:12 +0200 Subject: [PATCH] feat(function_ref): add as_c_callback() member function --- function_ref.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/function_ref.hpp b/function_ref.hpp index 4942ee7..fd7cebb 100644 --- a/function_ref.hpp +++ b/function_ref.hpp @@ -135,6 +135,19 @@ class function_ref assert(*this && "*this does not store a callable function target"); return _erased_func(_invocable, std::forward(args)...); } + + // Helper that exposes the guts of this class to make it easily usable in C-style callbacks. + // This avoids extra indirection & conversion overhead. + // Bit hackis right now, should probably perform some proper conversions to actually rely on defined behavior... + std::pair as_c_callback() const noexcept { + return { + _invocable.optr, + reinterpret_cast( + // casting via intermediate most basic function pointer to suppress a warning about incompatible function pointer type. + reinterpret_cast(_erased_func) + ), + }; + } }; template