diff --git a/python/cast.hh b/python/cast.hh index 77ed63a..beaa489 100644 --- a/python/cast.hh +++ b/python/cast.hh @@ -1,208 +1,210 @@ /** * @file * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any * later version. * * Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #ifndef __CAST_HH__ #define __CAST_HH__ /* -------------------------------------------------------------------------- */ #include "grid_base.hh" -#include "surface.hh" #include "numpy.hh" +#include "surface.hh" /* -------------------------------------------------------------------------- */ -#include #include +#include /* -------------------------------------------------------------------------- */ namespace pybind11 { // Format descriptor necessary for correct wrap of tamaas complex type template struct format_descriptor< tamaas::complex, detail::enable_if_t::value>> { static constexpr const char c = format_descriptor::c; static constexpr const char value[3] = {'Z', c, '\0'}; static std::string format() { return std::string(value); } }; #ifndef PYBIND11_CPP17 template constexpr const char format_descriptor< tamaas::complex, detail::enable_if_t::value>>::value[3]; #endif namespace detail { // declare tamaas complex as a complex type for pybind11 template struct is_complex> : std::true_type {}; template struct is_fmt_numeric, detail::enable_if_t::value>> : std::true_type { static constexpr int index = is_fmt_numeric::index + 3; }; +static handle policy_switch(return_value_policy policy, handle parent) { + switch (policy) { + case return_value_policy::copy: + case return_value_policy::move: + return handle(); + case return_value_policy::reference: + return none(); + case return_value_policy::reference_internal: + return parent; + default: + TAMAAS_EXCEPTION("Policy is not handled"); + } +} + +template +handle grid_to_python(const tamaas::Grid& grid, + return_value_policy policy, handle parent) { + parent = policy_switch(policy, parent); // reusing variable + std::vector sizes(dim); + std::copy(grid.sizes().begin(), grid.sizes().end(), sizes.begin()); + + if (grid.getNbComponents() != 1) + sizes.push_back(grid.getNbComponents()); + + return array(sizes, grid.getInternalData(), parent).release(); +} /** * Type caster for grid classes * inspired by https://tinyurl.com/y8m47qh3 from T. De Geus * and pybind11/eigen.h */ template