Nektar++
Public Member Functions | Static Public Member Functions | List of all members
pybind11::detail::nekarray_caster< Type, T > Struct Template Reference

Convert for Array<OneD, T> to Python list of objects for numeric types, self-array types and shared_ptr types. More...

#include <SharedArray.hpp>

Public Member Functions

 PYBIND11_TYPE_CASTER (Type, const_name("Array<OneD, ")+handle_type_name< T >::name+const_name(">"))
 
bool load (handle src, bool enable)
 
template<typename U = T, std::enable_if_t< std::is_arithmetic_v< U >, bool > = true>
bool load_impl (handle src, bool)
 
template<typename U = T, std::enable_if_t< is_shared_ptr< typename std::remove_const< U >::type >::value||is_nekarray_oned< U >::value, bool > = true>
bool load_impl (handle src, bool)
 

Static Public Member Functions

static handle cast (Nektar::Array< Nektar::OneD, T > const &arr, return_value_policy policy, handle parent)
 
static void decrement (void *objPtr)
 
template<typename U = T, std::enable_if_t< std::is_arithmetic_v< U >, bool > = true>
static handle cast_impl (Nektar::Array< Nektar::OneD, T > const &arr, return_value_policy, handle)
 
template<typename U = T, std::enable_if_t< is_shared_ptr< typename std::remove_const< U >::type >::value||is_nekarray_oned< U >::value, bool > = true>
static handle cast_impl (Nektar::Array< Nektar::OneD, U > const &arr, return_value_policy, handle)
 

Detailed Description

template<typename Type, typename T>
struct pybind11::detail::nekarray_caster< Type, T >

Convert for Array<OneD, T> to Python list of objects for numeric types, self-array types and shared_ptr types.

Template Parameters
TypeThe overall type Array<OneD, T>
TThe data type T

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

Member Function Documentation

◆ cast()

template<typename Type , typename T >
static handle pybind11::detail::nekarray_caster< Type, T >::cast ( Nektar::Array< Nektar::OneD, T > const &  arr,
return_value_policy  policy,
handle  parent 
)
inlinestatic

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

82 {
83 return cast_impl(arr, policy, parent);
84 }
static handle cast_impl(Nektar::Array< Nektar::OneD, T > const &arr, return_value_policy, handle)

References pybind11::detail::nekarray_caster< Type, T >::cast_impl().

◆ cast_impl() [1/2]

template<typename Type , typename T >
template<typename U = T, std::enable_if_t< std::is_arithmetic_v< U >, bool > = true>
static handle pybind11::detail::nekarray_caster< Type, T >::cast_impl ( Nektar::Array< Nektar::OneD, T > const &  arr,
return_value_policy  ,
handle   
)
inlinestatic

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

138 {
139 // Create a Python capsule to hold a pointer that contains a lightweight
140 // copy of arr. Uhat way we guarantee Python will still have access to
141 // the memory allocated inside arr even if arr is deallocated in C++.
142 capsule c(new Nektar::Array<Nektar::OneD, U>(arr), [](void *ptr) {
145 delete tmp;
146 });
147
148 // Create the NumPy array, passing the capsule. When we go out of scope,
149 // c's reference count will have been reduced by 1, but array increases
150 // the reference count when it assigns the base to the array.
151 array_t<U> array({arr.size()}, {}, arr.data(), c);
152
153 // This causes the array to be released without decreasing its reference
154 // count, otherwise the array would be deallocated immediately when this
155 // function returns.
156 return array.release();
157 }

Referenced by pybind11::detail::nekarray_caster< Type, T >::cast().

◆ cast_impl() [2/2]

template<typename Type , typename T >
template<typename U = T, std::enable_if_t< is_shared_ptr< typename std::remove_const< U >::type >::value||is_nekarray_oned< U >::value, bool > = true>
static handle pybind11::detail::nekarray_caster< Type, T >::cast_impl ( Nektar::Array< Nektar::OneD, U > const &  arr,
return_value_policy  ,
handle   
)
inlinestatic

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

203 {
204 py::list tmp;
205
206 for (std::size_t i = 0; i < arr.size(); ++i)
207 {
208 tmp.append(arr[i]);
209 }
210
211 return tmp.release();
212 }

◆ decrement()

template<typename Type , typename T >
static void pybind11::detail::nekarray_caster< Type, T >::decrement ( void *  objPtr)
inlinestatic

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

120 {
121 if (!Py_IsInitialized())
122 {
123 // In deinitialisation phase, reference counters appear to not be
124 // terribly robust; decremementing counters here can lead to
125 // segfaults during program exit in some cases.
126 return;
127 }
128
129 // Otherwise decrement reference counter.
130 handle obj((PyObject *)objPtr);
131 obj.dec_ref();
132 }

Referenced by pybind11::detail::nekarray_caster< Type, T >::load_impl().

◆ load()

template<typename Type , typename T >
bool pybind11::detail::nekarray_caster< Type, T >::load ( handle  src,
bool  enable 
)
inline

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

76 {
77 return load_impl(src, enable);
78 }

References pybind11::detail::nekarray_caster< Type, T >::load_impl().

◆ load_impl() [1/2]

template<typename Type , typename T >
template<typename U = T, std::enable_if_t< std::is_arithmetic_v< U >, bool > = true>
bool pybind11::detail::nekarray_caster< Type, T >::load_impl ( handle  src,
bool   
)
inline

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

89 {
90 if (!array_t<U>::check_(src))
91 {
92 return false;
93 }
94
95 auto arr = array_t<U, array::c_style | array::forcecast>::ensure(src);
96 if (!arr)
97 {
98 return false;
99 }
100
101 if (arr.ndim() != 1)
102 {
103 return false;
104 }
105
106 using nonconst_t = typename std::remove_const<U>::type;
107 value = Nektar::Array<Nektar::OneD, T>(arr.shape(0),
108 (nonconst_t *)arr.data(),
109 (void *)src.ptr(), &decrement);
110
111 // We increase the refcount on src so that even if on the Python side we
112 // go out of scope, the data will still exist in at least one reference
113 // until it is no longer used on the C++ side.
114 src.inc_ref();
115
116 return true;
117 }

References pybind11::detail::nekarray_caster< Type, T >::decrement().

Referenced by pybind11::detail::nekarray_caster< Type, T >::load().

◆ load_impl() [2/2]

template<typename Type , typename T >
template<typename U = T, std::enable_if_t< is_shared_ptr< typename std::remove_const< U >::type >::value||is_nekarray_oned< U >::value, bool > = true>
bool pybind11::detail::nekarray_caster< Type, T >::load_impl ( handle  src,
bool   
)
inline

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

165 {
166 if (!py::isinstance<py::list>(src))
167 {
168 return false;
169 }
170
171 py::list l = py::cast<py::list>(src);
172 const std::size_t nItems = l.size();
173
174 using nonconst_t = typename std::remove_const<U>::type;
175 auto tmparr = Nektar::Array<Nektar::OneD, nonconst_t>(nItems);
176
177 // We'll need to construct a temporary vector to hold each item in the
178 // list.
179 try
180 {
181 for (std::size_t i = 0; i < nItems; ++i)
182 {
183 tmparr[i] = py::cast<nonconst_t>(l[i]);
184 }
185 }
186 catch (...)
187 {
188 return false;
189 }
190
191 value = tmparr;
192
193 return true;
194 }

◆ PYBIND11_TYPE_CASTER()

template<typename Type , typename T >
pybind11::detail::nekarray_caster< Type, T >::PYBIND11_TYPE_CASTER ( Type  ,
const_name("Array<OneD, ")+handle_type_name< T >::name+const_name(">")   
)