#ifndef NETGEN_REGISTER_ARCHIVE_HPP #define NETGEN_REGISTER_ARCHIVE_HPP #ifdef NETGEN_PYTHON #include #include #include #endif // NETGEN_PYTHON #include #include "archive.hpp" namespace ngcore { // *************** Archiving functionality ************** #ifdef NETGEN_PYTHON template Archive& Archive :: Shallow(T& val) { static_assert(detail::is_any_pointer, "ShallowArchive must be given pointer type!"); if(shallow_to_python) { if(is_output) ShallowOutPython(pybind11::cast(val)); else { pybind11::object obj; ShallowInPython(obj); val = pybind11::cast(obj); } } else *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 template> class RegisterClassForArchive { public: RegisterClassForArchive() { static_assert(std::is_base_of::value || detail::is_base_of_tuple, "Second argument must be base class or tuple of base classes of T"); detail::ClassArchiveInfo info {}; info.creator = [](const std::type_info& ti, Archive& ar) -> void* { detail::TCargs args; ar &args; auto nT = detail::constructIfPossible(args); return typeid(T) == ti ? nT : Archive::Caster::tryUpcast(ti, nT); }; info.upcaster = [](const std::type_info& ti, void* p) -> void* { return typeid(T) == ti ? p : Archive::Caster::tryUpcast(ti, static_cast(p)); }; info.downcaster = [](const std::type_info& ti, void* p) -> void* { return typeid(T) == ti ? p : Archive::Caster::tryDowncast(ti, p); }; info.cargs_archiver = [](Archive &ar, void* p) { if constexpr(detail::has_GetCArgs_v) ar << static_cast(p)->GetCArgs(); }; #ifdef NETGEN_PYTHON info.anyToPyCaster = [](const std::any &a) { if constexpr(has_shared_from_this2::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); } }; } // namespace ngcore #endif // NETGEN_REGISTER_ARCHIVE_HPP