From 8a286f9cfc4bab3ef890edf4a28897c312fb119f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sun, 3 May 2020 12:57:01 +0200 Subject: [PATCH] Change Functor_converter to use a function-type template argument. --- src/runner/pythonrunner.cpp | 48 ++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index a2e0e09..4696d82 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -675,9 +675,12 @@ int getArgCount(PyObject *object) { return result; } +template +struct Functor_converter; + template -struct Functor_converter +struct Functor_converter { struct FunctorWrapper @@ -875,10 +878,10 @@ BOOST_PYTHON_MODULE(mobase) // TODO: ISaveGameInfoWidget bindings - Functor_converter(); - Functor_converter(); - Functor_converter(); - Functor_converter(); + Functor_converter(); + Functor_converter(); + Functor_converter(); + Functor_converter(); bpy::class_("IOrganizer") .def("createNexusBridge", bpy::pure_virtual(&IOrganizer::createNexusBridge), bpy::return_value_policy()) @@ -927,6 +930,9 @@ BOOST_PYTHON_MODULE(mobase) bpy::register_ptr_to_python>(); bpy::register_ptr_to_python>(); + // For removeIf: + Functor_converter const&)>(); + // FileTreeEntry Scope: auto fileTreeEntryClass = bpy::class_, boost::noncopyable>("FileTreeEntry", bpy::no_init); { @@ -1017,9 +1023,7 @@ BOOST_PYTHON_MODULE(mobase) .def("clear", &IFileTree::clear) .def("removeAll", &IFileTree::removeAll) - .def("removeIf", +[](IFileTree* p, boost::python::object fn) { - return p->removeIf(fn); - }) + .def("removeIf", &IFileTree::removeIf) // Special methods: .def("__getitem__", static_cast(IFileTree::*)(std::size_t)>(&IFileTree::at), @@ -1125,6 +1129,11 @@ BOOST_PYTHON_MODULE(mobase) .value("USER", MOBase::GUESS_USER) ; + // For setFilter, temporarily since the reference does not allow + // python plugin to update the name: + register_implicit_variant(); + Functor_converter(QString const&)>(); + bpy::class_, boost::noncopyable>("GuessedString") .def(bpy::init<>()) .def(bpy::init()) @@ -1141,7 +1150,24 @@ BOOST_PYTHON_MODULE(mobase) .def("reset", +[](GuessedValue* gv, const GuessedValue& other) { *gv = other; }, bpy::return_self<>()) // Use an intermediate lambda to avoid having to register the std::function conversion: - .def("setFilter", +[](GuessedValue* gv, bpy::object fn) { gv->setFilter(fn); }) + .def("setFilter", +[](GuessedValue* gv, std::function(QString const&)> fn) { + gv->setFilter([fn](QString& s) { + auto ret = fn(s); + return boost::apply_visitor([&s](auto v) { + qDebug() << "In visitor:" << typeid(v).name(); + if constexpr (std::is_same_v) { + s = v; + return true; + } + else if constexpr (std::is_same_v) { + return v; + } + else { + static_assert("Incorrect visitor."); + } + }, ret); + }); + }) // Exposing the set does not work, but even if it worked, we would lose the order since it would be // converted to a python set() so we expose a cusotm iterator. This works because variants() returns @@ -1157,7 +1183,7 @@ BOOST_PYTHON_MODULE(mobase) bpy::to_python_converter>(); QFlags_from_python_obj(); - Functor_converter(); // converter for the onRefreshed-callback + Functor_converter(); // converter for the onRefreshed-callback bpy::enum_("PluginState") .value("missing", IPluginList::STATE_MISSING) @@ -1181,7 +1207,7 @@ BOOST_PYTHON_MODULE(mobase) bpy::to_python_converter>(); QFlags_from_python_obj(); - Functor_converter(); // converter for the onModStateChanged-callback + Functor_converter(); // converter for the onModStateChanged-callback bpy::enum_("ModState") .value("exists", IModList::STATE_EXISTS)