From 782aa072bcc43f3dec568b220a0341db666a4420 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Fri, 21 Dec 2018 16:03:44 +0100 Subject: [PATCH] fix NGSPickle for abstract classes (like CoefficientFunction) --- libsrc/core/CMakeLists.txt | 4 ++++ libsrc/core/archive.hpp | 35 +++++++++++++++++++---------------- tests/catch/archive.cpp | 11 ----------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index e7fe7240..92f2e8b7 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -4,6 +4,10 @@ target_compile_definitions(ngcore PRIVATE -DNGCORE_EXPORTS) install(TARGETS ngcore DESTINATION ${NG_INSTALL_DIR} COMPONENT netgen) +if(USE_PYTHON) + target_include_directories(ngcore PUBLIC ${PYTHON_INCLUDE_DIRS}) +endif(USE_PYTHON) + install(FILES ngcore.hpp archive.hpp type_traits.hpp version.hpp ngcore_api.hpp DESTINATION ${NG_INSTALL_DIR_INCLUDE}/core COMPONENT netgen_devel) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 15ecb894..fd2ee275 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -806,23 +806,23 @@ namespace ngcore }; #ifdef NG_PYTHON - namespace py = pybind11; template class PyArchive : public ARCHIVE { private: - py::list lst; + pybind11::list lst; size_t index = 0; using ARCHIVE::stream; public: - PyArchive(const py::object& alst = py::none()) : + PyArchive(const pybind11::object& alst = pybind11::none()) : ARCHIVE(std::make_shared()), - lst(alst.is_none() ? py::list() : py::cast(alst)) + lst(alst.is_none() ? pybind11::list() : pybind11::cast(alst)) { ARCHIVE::shallow_to_python = true; if(Input()) - stream = std::make_shared(py::cast(lst[py::len(lst)-1])); + stream = std::make_shared + (pybind11::cast(lst[pybind11::len(lst)-1])); } using ARCHIVE::Output; @@ -831,32 +831,35 @@ namespace ngcore using ARCHIVE::operator&; using ARCHIVE::operator<<; using ARCHIVE::GetVersion; - void ShallowOutPython(py::object val) override { lst.append(val); } - py::object ShallowInPython() override { return lst[index++]; } + void ShallowOutPython(pybind11::object val) override { lst.append(val); } + pybind11::object ShallowInPython() override { return lst[index++]; } - py::list WriteOut() + pybind11::list WriteOut() { FlushBuffer(); - lst.append(py::bytes(std::static_pointer_cast(stream)->str())); + lst.append(pybind11::bytes(std::static_pointer_cast(stream)->str())); return lst; } }; template - auto NGSPickle() + auto NGSPickle(bool printoutput=false) { - return py::pickle([](T& self) + return pybind11::pickle([printoutput](T* self) { PyArchive ar; ar & self; - return py::make_tuple(ar.WriteOut()); + auto output = pybind11::make_tuple(ar.WriteOut()); + if(printoutput) + pybind11::print("pickle output of", Demangle(typeid(T).name()),"=", output); + return output; }, - [](py::tuple state) + [](pybind11::tuple state) { - auto val = std::make_unique(); + T* val = nullptr; PyArchive ar(state[0]); - ar & *val; - return std::move(val); + ar & val; + return val; }); } diff --git a/tests/catch/archive.cpp b/tests/catch/archive.cpp index ea5ac1dc..b3f27d49 100644 --- a/tests/catch/archive.cpp +++ b/tests/catch/archive.cpp @@ -236,13 +236,6 @@ void testMultipleInheritance(Archive& in, Archive& out) } } -void testLibraryVersion(Archive& in, Archive& out) -{ - SetLibraryVersion("netgen","v6.2.1812"); - CHECK(in.GetVersion("netgen") == "v6.2.1811"); - CHECK(out.GetVersion("netgen") == "v6.2.1812"); -} - void testArchive(Archive& in, Archive& out) { SECTION("Empty String") @@ -292,10 +285,6 @@ void testArchive(Archive& in, Archive& out) { testNullPtr(in, out); } - SECTION("Library Version") - { - testLibraryVersion(in,out); - } } TEST_CASE("BinaryArchive")