From cdbfc27e813a8a1dfa434d17f108e3d4f7fac9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Milk?= <rene.milk@wwu.de> Date: Wed, 20 Jun 2018 15:06:31 +0200 Subject: [PATCH] [py] simplify walker bindings --- python/dune/xt/grid/walker.bindings.hh | 47 ++++++++++---------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/python/dune/xt/grid/walker.bindings.hh b/python/dune/xt/grid/walker.bindings.hh index 70e574aaa..9afa5754c 100644 --- a/python/dune/xt/grid/walker.bindings.hh +++ b/python/dune/xt/grid/walker.bindings.hh @@ -26,32 +26,19 @@ namespace Grid { namespace bindings { namespace internal { - -template <class GL> -class Walker +template <class WalkerOrDerivedType, class... Bases> +static void bind_walker_functions(pybind11::class_<WalkerOrDerivedType, Bases...>& c) { - static_assert(is_layer<GL>::value, ""); - -public: - typedef XT::Grid::Walker<GL> type; - typedef pybind11::class_<type> bound_type; - - static bound_type bind(pybind11::module& m, const std::string& grid_and_layer_name) - { - namespace py = pybind11; - using namespace pybind11::literals; - - bound_type c(m, Common::to_camel_case(std::string("walker_") + grid_and_layer_name).c_str()); - c.def("clear", &type::clear); - c.def("append", [](type& self, type& other) { self.append(other); }, "other"_a, py::keep_alive<1, 2>()); - c.def("prepare", &type::prepare); - c.def("finalize", &type::finalize); - c.def("walk", [](type& self, const bool use_tbb) { self.walk(use_tbb); }, "use_tbb"_a = false); - - return c; - } // ... bind(...) -}; // class Walker + namespace py = pybind11; + using namespace pybind11::literals; + using type = WalkerOrDerivedType; + c.def("clear", &type::clear); + c.def("append", [](type& self, type& other) { self.append(other); }, "other"_a, py::keep_alive<1, 2>()); + c.def("prepare", &type::prepare); + c.def("finalize", &type::finalize); + c.def("walk", [](type& self, const bool use_tbb) { self.walk(use_tbb); }, "use_tbb"_a = false); +} // ... bind(...) } // namespace internal @@ -61,11 +48,10 @@ class Walker { static_assert(is_grid<G>::value, ""); typedef typename Layer<G, layer, backend>::type GL; - using binder = internal::Walker<GL>; public: - using type = typename binder::type; - using bound_type = typename binder::bound_type; + using type = XT::Grid::Walker<GL>; + using bound_type = pybind11::class_<type>; private: template <bool is_dd = (layer == Layers::dd_subdomain || layer == Layers::dd_subdomain_boundary @@ -111,8 +97,11 @@ public: // we need to add the factory methods first, since adding the class below might fail (if someone added it before) factory_method<>::addbind(m); - return binder::bind( - m, grid_name<G>::value() + "_" + layer_name<layer>::value() + "_" + backend_name<backend>::value()); + const auto gl_name = + grid_name<G>::value() + "_" + layer_name<layer>::value() + "_" + backend_name<backend>::value(); + bound_type c(m, Common::to_camel_case(std::string("walker_") + gl_name).c_str()); + internal::bind_walker_functions(c); + return c; } }; // class Walker -- GitLab