mirror of
https://github.com/NGSolve/netgen.git
synced 2024-12-26 05:50:32 +05:00
Fix AnyToPython for types with shared_ptr holder type
This commit is contained in:
parent
01ba8dd4d6
commit
18535405d7
@ -2,6 +2,7 @@
|
|||||||
#define NETGEN_REGISTER_ARCHIVE_HPP
|
#define NETGEN_REGISTER_ARCHIVE_HPP
|
||||||
|
|
||||||
#ifdef NETGEN_PYTHON
|
#ifdef NETGEN_PYTHON
|
||||||
|
#include <memory>
|
||||||
#include <pybind11/pybind11.h>
|
#include <pybind11/pybind11.h>
|
||||||
#include <pybind11/cast.h>
|
#include <pybind11/cast.h>
|
||||||
#endif // NETGEN_PYTHON
|
#endif // NETGEN_PYTHON
|
||||||
@ -32,6 +33,15 @@ namespace ngcore {
|
|||||||
*this & val;
|
*this & val;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct has_shared_from_this
|
||||||
|
{
|
||||||
|
template <typename C> static std::true_type check( decltype( sizeof(&C::shared_from_this() )) ) { return std::true_type(); }
|
||||||
|
template <typename> static std::false_type check(...) { return std::false_type(); }
|
||||||
|
typedef decltype( check<T>(sizeof(char)) ) type;
|
||||||
|
static constexpr type value = type();
|
||||||
|
};
|
||||||
#endif // NETGEN_PYTHON
|
#endif // NETGEN_PYTHON
|
||||||
|
|
||||||
|
|
||||||
@ -65,8 +75,13 @@ namespace ngcore {
|
|||||||
};
|
};
|
||||||
#ifdef NETGEN_PYTHON
|
#ifdef NETGEN_PYTHON
|
||||||
info.anyToPyCaster = [](const std::any &a) {
|
info.anyToPyCaster = [](const std::any &a) {
|
||||||
const T* val = std::any_cast<T>(&a);
|
if constexpr(has_shared_from_this<T>::value) {
|
||||||
return pybind11::cast(val);
|
std::shared_ptr<T> val = std::any_cast<std::shared_ptr<T>>(&a);
|
||||||
|
return pybind11::cast(val);
|
||||||
|
} else {
|
||||||
|
const T* val = std::any_cast<T>(&a);
|
||||||
|
return pybind11::cast(val);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif // NETGEN_PYTHON
|
#endif // NETGEN_PYTHON
|
||||||
Archive::SetArchiveRegister(std::string(Demangle(typeid(T).name())),info);
|
Archive::SetArchiveRegister(std::string(Demangle(typeid(T).name())),info);
|
||||||
|
Loading…
Reference in New Issue
Block a user