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 60 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 62 of file SharedArray.cpp.

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