Skip to content

Commit

Permalink
Trampoline macros
Browse files Browse the repository at this point in the history
  • Loading branch information
austinschneider committed Mar 10, 2024
1 parent cc2c335 commit 82aa3f7
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions projects/utilities/public/SIREN/utilities/Pybind11Trampoline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef SIREN_Pybind11Trampoline_H
#define SIREN_Pybind11Trampoline_H

#include <pybind11/pybind11.h>

#define SELF_OVERRIDE_PURE(selfname, BaseType, returnType, cfuncname, pyfuncname, ...) \
const BaseType * ref; \
if(selfname) { \
ref = selfname.cast<BaseType *>(); \
} else { \
ref = this; \
} \
do { \
do { \
pybind11::gil_scoped_acquire gil; \
pybind11::function override \
= pybind11::get_override(static_cast<const BaseType *>(ref), pyfuncname); \
if (override) { \
auto o = override(__VA_ARGS__); \
if (pybind11::detail::cast_is_temporary_value_reference<returnType>::value) { \
static pybind11::detail::override_caster_t<returnType> caster; \
return pybind11::detail::cast_ref<returnType>(std::move(o), caster); \
} \
return pybind11::detail::cast_safe<returnType>(std::move(o)); \
} \
} while (false); \
pybind11::pybind11_fail( \
"Tried to call pure virtual function \"" PYBIND11_STRINGIFY(BaseType) "::" #cfuncname "\""); \
} while (false);

#define SELF_OVERRIDE(selfname, BaseType, returnType, cfuncname, pyfuncname, ...) \
const BaseType * ref; \
if(selfname) { \
ref = selfname.cast<BaseType *>(); \
} else { \
ref = this; \
} \
do { \
do { \
pybind11::gil_scoped_acquire gil; \
pybind11::function override \
= pybind11::get_override(static_cast<const BaseType *>(ref), pyfuncname); \
if (override) { \
auto o = override(__VA_ARGS__); \
if (pybind11::detail::cast_is_temporary_value_reference<returnType>::value) { \
static pybind11::detail::override_caster_t<returnType> caster; \
return pybind11::detail::cast_ref<returnType>(std::move(o), caster); \
} \
return pybind11::detail::cast_safe<returnType>(std::move(o)); \
} \
} while (false); \
return BaseType::cfuncname(__VA_ARGS__); \
} while (false);

#endif // SIREN_Pybind11Trampoline_H

0 comments on commit 82aa3f7

Please sign in to comment.