9
9
namespace cpp2py {
10
10
11
11
template <typename T> static void delete_pycapsule (PyObject *capsule) {
12
- auto *ptr = static_cast <std::unique_ptr<T[] > *>(PyCapsule_GetPointer (capsule, " guard" ));
12
+ auto *ptr = static_cast <std::vector<T > *>(PyCapsule_GetPointer (capsule, " guard" ));
13
13
delete ptr;
14
14
}
15
15
16
16
// Convert vector to numpy_proxy, WARNING: Deep Copy
17
- template <typename T> numpy_proxy make_numpy_proxy_from_vector (std::vector<T> const & v) {
17
+ template <typename T> numpy_proxy make_numpy_proxy_from_vector (std::vector<T> v) {
18
18
19
- auto *data_ptr = new std::unique_ptr<T[]>{new T[v.size ()]};
20
- std::copy (begin (v), end (v), data_ptr->get ());
21
- auto capsule = PyCapsule_New (data_ptr, " guard" , &delete_pycapsule<T>);
19
+ auto *vec_heap = new std::vector<T>{std::move (v)};
20
+ auto capsule = PyCapsule_New (vec_heap, " guard" , &delete_pycapsule<T>);
22
21
23
22
return {1 , // rank
24
23
npy_type<std::remove_const_t <T>>,
25
- (void *)data_ptr-> get (),
24
+ (void *)vec_heap-> data (),
26
25
std::is_const_v<T>,
27
26
v_t {static_cast <long >(v.size ())}, // extents
28
27
v_t {sizeof (T)}, // strides
@@ -46,14 +45,14 @@ namespace cpp2py {
46
45
47
46
template <typename T> struct py_converter <std::vector<T>> {
48
47
49
- static PyObject *c2py (std::vector<T> const & v) {
48
+ static PyObject *c2py (std::vector<T> v) {
50
49
51
50
if constexpr (has_npy_type<T>) {
52
- return make_numpy_proxy_from_vector (v ).to_python ();
51
+ return make_numpy_proxy_from_vector (std::move (v) ).to_python ();
53
52
} else { // Convert to Python List
54
53
PyObject *list = PyList_New (0 );
55
54
for (auto const &x : v) {
56
- pyref y = py_converter<T>::c2py (x );
55
+ pyref y = py_converter<T>::c2py (std::move (x) );
57
56
if (y.is_null () or (PyList_Append (list, y) == -1 )) {
58
57
Py_DECREF (list);
59
58
return NULL ;
0 commit comments