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

Convert for Array<OneD, T> to Python list of objects for numeric types T. More...

#include <SharedArray.hpp>

Static Public Member Functions

static PyObject * convert (Array< OneD, T > const &arr)
 
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value > * = nullptr>
static PyObject * convert_impl (Array< OneD, U > const &arr)
 
template<typename U = T, std::enable_if_t< is_shared_ptr< U >::value||is_nekarray_oned< U >::value > * = nullptr>
static PyObject * convert_impl (Array< OneD, U > const &arr)
 Converter function. This copies entries into the Python list and relies on internal boost converter being available for the shared_ptr type. More...
 

Detailed Description

template<typename T>
struct OneDArrayToPython< T >

Convert for Array<OneD, T> to Python list of objects for numeric types T.

Definition at line 93 of file Python/BasicUtils/SharedArray.hpp.

Member Function Documentation

◆ convert()

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

Definition at line 95 of file Python/BasicUtils/SharedArray.hpp.

96 {
97 return convert_impl(arr);
98 }
static PyObject * convert_impl(Array< OneD, U > const &arr)

References OneDArrayToPython< T >::convert_impl().

◆ convert_impl() [1/2]

template<typename T >
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value > * = nullptr>
static PyObject * OneDArrayToPython< T >::convert_impl ( Array< OneD, U > const &  arr)
inlinestatic

Definition at line 102 of file Python/BasicUtils/SharedArray.hpp.

103 {
104 // Create a Python capsule to hold a pointer that contains a lightweight
105 // copy of arr. Uhat way we guarantee Python will still have access to
106 // the memory allocated inside arr even if arr is deallocated in C++.
107#if PY_MAJOR_VERSION == 2
108 py::object capsule(py::handle<>(PyCObject_FromVoidPtr(
109 new Array<OneD, U>(arr), CapsuleDestructor<U>)));
110#else
111 py::object capsule(py::handle<>(
112 PyCapsule_New(new Array<OneD, U>(arr), nullptr,
113 (PyCapsule_Destructor)&CapsuleDestructor<U>)));
114#endif
115 PyObject *tmp =
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)
119 .ptr());
120
121 return tmp;
122 }

Referenced by OneDArrayToPython< T >::convert().

◆ convert_impl() [2/2]

template<typename T >
template<typename U = T, std::enable_if_t< is_shared_ptr< U >::value||is_nekarray_oned< U >::value > * = nullptr>
static PyObject * OneDArrayToPython< T >::convert_impl ( Array< OneD, U > const &  arr)
inlinestatic

Converter function. This copies entries into the Python list and relies on internal boost converter being available for the shared_ptr type.

Definition at line 132 of file Python/BasicUtils/SharedArray.hpp.

133 {
134 py::list tmp;
135 for (std::size_t i = 0; i < arr.size(); ++i)
136 {
137 tmp.append(arr[i]);
138 }
139
140 // Increment counter to avoid de-allocation of `tmp`.
141 return py::incref(tmp.ptr());
142 }