Nektar++
Static Public Member Functions | List of all members
OneDArrayToPython< T > Struct Template Reference

Static Public Member Functions

static PyObject * convert (Array< OneD, T > const &arr)
 

Detailed Description

template<typename T>
struct OneDArrayToPython< T >

Definition at line 57 of file SharedArray.cpp.

Member Function Documentation

◆ convert()

template<typename T >
static PyObject * OneDArrayToPython< T >::convert ( Array< OneD, T > const &  arr)
inlinestatic

Definition at line 59 of file SharedArray.cpp.

60 {
61 // Create a Python capsule to hold a pointer that contains a lightweight
62 // copy of arr. That way we guarantee Python will still have access to
63 // the memory allocated inside arr even if arr is deallocated in C++.
64#if PY_MAJOR_VERSION == 2
65 py::object capsule(py::handle<>(PyCObject_FromVoidPtr(
66 new Array<OneD, T>(arr), CapsuleDestructor<T>)));
67#else
68 py::object capsule(py::handle<>(
69 PyCapsule_New(new Array<OneD, T>(arr), nullptr,
70 (PyCapsule_Destructor)&CapsuleDestructor<T>)));
71#endif
72 PyObject *tmp =
73 py::incref(np::from_data(arr.data(), np::dtype::get_builtin<T>(),
74 py::make_tuple(arr.size()),
75 py::make_tuple(sizeof(T)), capsule)
76 .ptr());
77
78 return tmp;
79 }