diff --git a/libsrc/core/register_archive.hpp b/libsrc/core/register_archive.hpp index f5a3dde5..f5f673ed 100644 --- a/libsrc/core/register_archive.hpp +++ b/libsrc/core/register_archive.hpp @@ -2,6 +2,7 @@ #define NETGEN_REGISTER_ARCHIVE_HPP #ifdef NETGEN_PYTHON +#include #include #include #endif // NETGEN_PYTHON @@ -32,6 +33,15 @@ namespace ngcore { *this & val; return *this; } + + template + struct has_shared_from_this + { + template static std::true_type check( decltype( sizeof(&C::shared_from_this() )) ) { return std::true_type(); } + template static std::false_type check(...) { return std::false_type(); } + typedef decltype( check(sizeof(char)) ) type; + static constexpr type value = type(); + }; #endif // NETGEN_PYTHON @@ -65,8 +75,13 @@ namespace ngcore { }; #ifdef NETGEN_PYTHON info.anyToPyCaster = [](const std::any &a) { - const T* val = std::any_cast(&a); - return pybind11::cast(val); + if constexpr(has_shared_from_this::value) { + std::shared_ptr val = std::any_cast>(&a); + return pybind11::cast(val); + } else { + const T* val = std::any_cast(&a); + return pybind11::cast(val); + } }; #endif // NETGEN_PYTHON Archive::SetArchiveRegister(std::string(Demangle(typeid(T).name())),info);