1 #ifndef NETGEN_REGISTER_ARCHIVE_HPP 2 #define NETGEN_REGISTER_ARCHIVE_HPP 3 4 #ifdef NETGEN_PYTHON 5 #include <pybind11/pybind11.h> 6 #include <pybind11/cast.h> 7 #endif // NETGEN_PYTHON 8 9 #include "archive.hpp" 10 11 namespace ngcore { 12 13 template<typename T, typename ... Bases> 14 class RegisterClassForArchive 15 { 16 public: RegisterClassForArchive()17 RegisterClassForArchive() 18 { 19 static_assert(detail::all_of_tmpl<std::is_base_of<Bases,T>::value...>, 20 "Variadic template arguments must be base classes of T"); 21 detail::ClassArchiveInfo info {}; 22 info.creator = [](const std::type_info& ti) -> void* 23 { return typeid(T) == ti ? detail::constructIfPossible<T>() 24 : Archive::Caster<T, Bases...>::tryUpcast(ti, detail::constructIfPossible<T>()); }; 25 info.upcaster = [/*this*/](const std::type_info& ti, void* p) -> void* 26 { return typeid(T) == ti ? p : Archive::Caster<T, Bases...>::tryUpcast(ti, static_cast<T*>(p)); }; 27 info.downcaster = [/*this*/](const std::type_info& ti, void* p) -> void* 28 { return typeid(T) == ti ? p : Archive::Caster<T, Bases...>::tryDowncast(ti, p); }; 29 #ifdef NETGEN_PYTHON 30 info.anyToPyCaster = [](const std::any& a) 31 { 32 const T* val = std::any_cast<T>(&a); 33 return pybind11::cast(val); }; 34 #endif // NETGEN_PYTHON 35 Archive::SetArchiveRegister(std::string(Demangle(typeid(T).name())),info); 36 } 37 }; 38 } // namespace ngcore 39 #endif // NETGEN_REGISTER_ARCHIVE_HPP 40