2021-09-29 01:34:11 +05:00
|
|
|
#ifndef NETGEN_REGISTER_ARCHIVE_HPP
|
|
|
|
#define NETGEN_REGISTER_ARCHIVE_HPP
|
|
|
|
|
|
|
|
#ifdef NETGEN_PYTHON
|
2023-08-28 18:21:02 +05:00
|
|
|
#include <memory>
|
2021-09-29 01:34:11 +05:00
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
#include <pybind11/cast.h>
|
|
|
|
#endif // NETGEN_PYTHON
|
2023-08-28 13:02:22 +05:00
|
|
|
#include <tuple>
|
2021-09-29 01:34:11 +05:00
|
|
|
|
|
|
|
#include "archive.hpp"
|
|
|
|
|
|
|
|
namespace ngcore {
|
2023-08-28 13:02:22 +05:00
|
|
|
// *************** Archiving functionality **************
|
2021-09-29 01:34:11 +05:00
|
|
|
|
2023-08-28 13:02:22 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
|
|
|
template<typename T>
|
|
|
|
Archive& Archive :: Shallow(T& val)
|
|
|
|
{
|
|
|
|
static_assert(detail::is_any_pointer<T>, "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<T>(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*this & val;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-08-28 18:21:02 +05:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct has_shared_from_this
|
|
|
|
{
|
2023-08-29 21:27:39 +05:00
|
|
|
template <typename C> static std::true_type check( decltype( sizeof(&C::shared_from_this )) ) { return std::true_type(); }
|
2023-08-28 18:21:02 +05:00
|
|
|
template <typename> static std::false_type check(...) { return std::false_type(); }
|
|
|
|
typedef decltype( check<T>(sizeof(char)) ) type;
|
|
|
|
static constexpr type value = type();
|
|
|
|
};
|
2023-08-28 13:02:22 +05:00
|
|
|
#endif // NETGEN_PYTHON
|
|
|
|
|
|
|
|
|
2023-08-29 21:27:39 +05:00
|
|
|
template<typename T, typename Bases=std::tuple<>>
|
2021-09-29 01:34:11 +05:00
|
|
|
class RegisterClassForArchive
|
|
|
|
{
|
|
|
|
public:
|
2023-08-29 21:27:39 +05:00
|
|
|
RegisterClassForArchive()
|
2021-09-29 01:34:11 +05:00
|
|
|
{
|
2023-08-28 13:02:22 +05:00
|
|
|
static_assert(std::is_base_of<Bases, T>::value ||
|
|
|
|
detail::is_base_of_tuple<T, Bases>,
|
|
|
|
"Second argument must be base class or tuple of base classes of T");
|
2021-09-29 01:34:11 +05:00
|
|
|
detail::ClassArchiveInfo info {};
|
2023-08-28 13:02:22 +05:00
|
|
|
info.creator = [](const std::type_info& ti, Archive& ar) -> void*
|
|
|
|
{
|
2023-08-29 21:27:39 +05:00
|
|
|
detail::TCargs<T> args;
|
2023-08-28 13:02:22 +05:00
|
|
|
ar &args;
|
|
|
|
auto nT = detail::constructIfPossible<T>(args);
|
|
|
|
return typeid(T) == ti ? nT
|
|
|
|
: Archive::Caster<T, Bases>::tryUpcast(ti, nT);
|
|
|
|
};
|
2021-09-29 01:34:11 +05:00
|
|
|
info.upcaster = [/*this*/](const std::type_info& ti, void* p) -> void*
|
2023-08-28 13:02:22 +05:00
|
|
|
{ return typeid(T) == ti ? p : Archive::Caster<T, Bases>::tryUpcast(ti, static_cast<T*>(p)); };
|
2021-09-29 01:34:11 +05:00
|
|
|
info.downcaster = [/*this*/](const std::type_info& ti, void* p) -> void*
|
2023-08-28 13:02:22 +05:00
|
|
|
{ return typeid(T) == ti ? p : Archive::Caster<T, Bases>::tryDowncast(ti, p); };
|
|
|
|
info.cargs_archiver = [this](Archive &ar, void* p) {
|
2023-08-29 21:27:39 +05:00
|
|
|
if constexpr(detail::has_GetCArgs_v<T>)
|
|
|
|
ar << static_cast<T*>(p)->GetCArgs();
|
2023-08-28 13:02:22 +05:00
|
|
|
};
|
2021-09-29 01:34:11 +05:00
|
|
|
#ifdef NETGEN_PYTHON
|
2023-08-28 13:02:22 +05:00
|
|
|
info.anyToPyCaster = [](const std::any &a) {
|
2023-08-28 18:21:02 +05:00
|
|
|
if constexpr(has_shared_from_this<T>::value) {
|
|
|
|
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);
|
|
|
|
}
|
2023-08-28 13:02:22 +05:00
|
|
|
};
|
2021-09-29 01:34:11 +05:00
|
|
|
#endif // NETGEN_PYTHON
|
2023-08-28 13:02:22 +05:00
|
|
|
Archive::SetArchiveRegister(std::string(Demangle(typeid(T).name())),info);
|
|
|
|
}
|
|
|
|
};
|
2021-09-29 01:34:11 +05:00
|
|
|
} // namespace ngcore
|
|
|
|
#endif // NETGEN_REGISTER_ARCHIVE_HPP
|