From 18535405d70d91830de826a0401f70d6b23ccc1a Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Mon, 28 Aug 2023 15:21:02 +0200 Subject: [PATCH] Fix AnyToPython for types with shared_ptr holder type --- libsrc/core/register_archive.hpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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);