35#ifndef NEKTAR_LIBUTILITIES_PYTHON_BASICUTILS_SHAREDARRAY_HPP
36#define NEKTAR_LIBUTILITIES_PYTHON_BASICUTILS_SHAREDARRAY_HPP
75#if PY_MAJOR_VERSION == 2
100 template <
typename U = T,
101 std::enable_if_t<std::is_arithmetic<U>::value> * =
nullptr>
107#if PY_MAJOR_VERSION == 2
108 py::object capsule(py::handle<>(PyCObject_FromVoidPtr(
111 py::object capsule(py::handle<>(
113 (PyCapsule_Destructor)&CapsuleDestructor<U>)));
116 py::incref(np::from_data(arr.data(), np::dtype::get_builtin<U>(),
117 py::make_tuple(arr.size()),
118 py::make_tuple(
sizeof(U)), capsule)
129 template <
typename U = T,
130 std::enable_if_t<is_shared_ptr<U>::value ||
135 for (std::size_t i = 0; i < arr.size(); ++i)
141 return py::incref(tmp.ptr());
168 py::object obj((py::handle<>(py::borrowed(objPtr))));
171 catch (boost::python::error_already_set &)
173 py::handle_exception();
179 template <
typename U = T,
180 std::enable_if_t<std::is_arithmetic<U>::value> * =
nullptr>
183 np::ndarray array = py::extract<np::ndarray>(obj);
187 np::dtype::get_builtin<typename std::remove_const<U>::type>();
188 if (dtype != array.get_dtype())
194 if (array.get_nd() != 1)
202 template <
typename U = T,
203 std::enable_if_t<is_shared_ptr<U>::value ||
207 py::extract<py::list> list_conv(obj);
209 if (!list_conv.check())
214 py::list l = list_conv();
215 const std::size_t nItems = py::len(l);
219 for (std::size_t i = 0; i < nItems; ++i)
221 py::extract<T> item_conv(l[i]);
223 if (!py::extract<T>(l[i]).check())
234 if (!Py_IsInitialized())
243 py::decref((PyObject *)objPtr);
247 py::converter::rvalue_from_python_stage1_data *data)
252 template <
typename U = T,
253 std::enable_if_t<std::is_arithmetic<U>::value> * =
nullptr>
255 PyObject *objPtr, py::converter::rvalue_from_python_stage1_data *data)
259 py::object obj(py::handle<>(py::borrowed(objPtr)));
260 np::ndarray array = py::extract<np::ndarray>(obj);
265 py::object base = array.get_base();
268#if PY_MAJOR_VERSION == 2
269 if (PyCObject_Check(base.ptr()))
272 PyCObject_AsVoidPtr(base.ptr()));
275 if (PyCapsule_CheckExact(base.ptr()))
278 PyCapsule_GetPointer(base.ptr(),
nullptr));
283 ((py::converter::rvalue_from_python_storage<Array<OneD, T>> *)data)
285 data->convertible = storage;
287 using nonconst_t =
typename std::remove_const<T>::type;
294 template <
typename U = T,
295 std::enable_if_t<is_shared_ptr<U>::value ||
298 PyObject *objPtr, py::converter::rvalue_from_python_stage1_data *data)
300 using nonconst_t =
typename std::remove_const<T>::type;
302 py::object obj(py::handle<>(py::borrowed(objPtr)));
303 py::list l = py::extract<py::list>(obj);
305 const std::size_t nItems = py::len(l);
309 ((py::converter::rvalue_from_python_storage<Array<OneD, nonconst_t>>
314 data->convertible = storage;
317 for (std::size_t i = 0; i < nItems; ++i)
319 (*tmp)[i] = py::extract<nonconst_t>(l[i]);
void CapsuleDestructor(PyObject *ptr)
void export_SharedArray()
Convenience function to export C++-to-Python and Python-to-C++ converters for the requested type.
Convert for Array<OneD, T> to Python list of objects for numeric types T.
static PyObject * convert_impl(Array< OneD, U > const &arr)
static PyObject * convert(Array< OneD, T > const &arr)
Converter for Python to Nektar::Array<OneD, T>.
PythonToOneDArray()
Default constructor.
static void construct_impl(PyObject *objPtr, py::converter::rvalue_from_python_stage1_data *data)
static bool try_convertible(py::object &obj)
static void construct(PyObject *objPtr, py::converter::rvalue_from_python_stage1_data *data)
static void decrement(void *objPtr)
static void * convertible(PyObject *objPtr)
Determine whether the given objPtr is convertible to convertible to an Array type or not.
Template utility to determine whether.
Template type to determine whether.