From debc2db34c701262db2f095924770fdfe95bbe3c Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 17 Jun 2024 19:33:44 -0400 Subject: [PATCH] xo-pyutil: pycaller: provide type-erased factory functions --- include/xo/pyutil/pycaller.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/include/xo/pyutil/pycaller.hpp b/include/xo/pyutil/pycaller.hpp index 8368c06..20ecc92 100644 --- a/include/xo/pyutil/pycaller.hpp +++ b/include/xo/pyutil/pycaller.hpp @@ -12,6 +12,9 @@ namespace xo { namespace pyutil { struct pycaller_base { + using void_function_type = void (*)(); + using factory_function_type = pycaller_base*(*)(void_function_type); + virtual ~pycaller_base() = default; static pybind11::module & declare_once(pybind11::module & m) { @@ -40,8 +43,11 @@ namespace xo { struct pycaller : public pycaller_base { using self_type = pycaller; using function_type = Retval (*)(); + using void_function_type = void (*)(); - pycaller(function_type addr) : fptr_{reinterpret_cast(addr)} {} + pycaller(void_function_type addr) : fptr_{reinterpret_cast(addr)} {} + + static pycaller_base * make(void_function_type addr) { return new pycaller(addr); } static pybind11::module & declare_once(pybind11::module & m) { static bool s_once = false; @@ -68,8 +74,11 @@ namespace xo { struct pycaller : public pycaller_base { using self_type = pycaller; using function_type = Retval (*)(Arg1); + using void_function_type = void (*)(); - pycaller(function_type addr) : fptr_{reinterpret_cast(addr)} {} + pycaller(void_function_type addr) : fptr_{reinterpret_cast(addr)} {} + + static pycaller_base * make(void_function_type addr) { return new pycaller(addr); } static pybind11::module & declare_once(pybind11::module & m) { static bool s_once = false; @@ -99,8 +108,11 @@ namespace xo { struct pycaller : public pycaller_base { using self_type = pycaller; using function_type = Retval (*)(Arg1, Arg2); + using void_function_type = void (*)(); - pycaller(function_type addr) : fptr_{reinterpret_cast(addr)} {} + pycaller(void_function_type addr) : fptr_{reinterpret_cast(addr)} {} + + static pycaller_base * make(void_function_type addr) { return new pycaller(addr); } static pybind11::module & declare_once(pybind11::module & m) { static bool s_once = false;