Skip to content

Commit 4b79e27

Browse files
committed
Add TODO note on pyarray
1 parent 2562449 commit 4b79e27

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

include/xtensor-python/pyarray.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ namespace pybind11
7272
};
7373

7474
// Type caster for casting ndarray to xexpression<pyarray>
75-
template<typename T>
75+
template <typename T>
7676
struct type_caster<xt::xexpression<xt::pyarray<T>>> : pyobject_caster<xt::pyarray<T>>
7777
{
7878
using Type = xt::xexpression<xt::pyarray<T>>;
79-
79+
8080
operator Type&()
8181
{
82-
return this->value;
82+
return this->value;
8383
}
8484

8585
operator const Type&()
@@ -89,7 +89,7 @@ namespace pybind11
8989
};
9090

9191
// Type caster for casting xarray to ndarray
92-
template<class T>
92+
template <class T>
9393
struct type_caster<xt::xarray<T>> : xtensor_type_caster_base<xt::xarray<T>>
9494
{
9595
};

include/xtensor-python/pytensor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ namespace pybind11
7474
};
7575

7676
// Type caster for casting ndarray to xexpression<pytensor>
77-
template<class T, std::size_t N>
77+
template <class T, std::size_t N>
7878
struct type_caster<xt::xexpression<xt::pytensor<T, N>>> : pyobject_caster<xt::pytensor<T, N>>
7979
{
8080
using Type = xt::xexpression<xt::pytensor<T, N>>;
81-
81+
8282
operator Type&()
8383
{
8484
return this->value;
@@ -91,7 +91,7 @@ namespace pybind11
9191
};
9292

9393
// Type caster for casting xt::xtensor to ndarray
94-
template<class T, std::size_t N>
94+
template <class T, std::size_t N>
9595
struct type_caster<xt::xtensor<T, N>> : xtensor_type_caster_base<xt::xtensor<T, N>>
9696
{
9797
};

include/xtensor-python/xtensor_type_caster_base.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,26 @@
1616
#define XTENSOR_TYPE_CASTER_HPP
1717

1818
#include "xtensor/xtensor.hpp"
19-
#include <pybind11/pybind11.h>
19+
2020
#include <pybind11/numpy.h>
21+
#include <pybind11/pybind11.h>
2122

2223
namespace pybind11
2324
{
2425
namespace detail
2526
{
2627
// Casts an xtensor (or xarray) type to numpy array. If given a base, the numpy array references the src data,
2728
// otherwise it'll make a copy. writeable lets you turn off the writeable flag for the array.
28-
template<typename Type>
29-
handle xtensor_array_cast(Type const &src, handle base = handle(), bool writeable = true)
29+
template <typename Type>
30+
handle xtensor_array_cast(Type const& src, handle base = handle(), bool writeable = true)
3031
{
32+
// TODO: make use of xt::pyarray instead of array.
3133
std::vector<size_t> python_strides(src.strides().size());
3234
std::transform(src.strides().begin(), src.strides().end(), python_strides.begin(),
3335
[](auto v) { return sizeof(typename Type::value_type) * v; });
3436

35-
std::vector<size_t> python_shape(src.shape().size());
36-
std::copy(src.shape().begin(), src.shape().end(), python_shape.begin());
37+
std::vector<size_t> python_shape(src.shape().size());
38+
std::copy(src.shape().begin(), src.shape().end(), python_shape.begin());
3739

3840
array a(python_shape, python_strides, src.begin(), base);
3941

@@ -50,7 +52,7 @@ namespace pybind11
5052
// the base will be set to None, and lifetime management is up to the caller). The numpy array is
5153
// non-writeable if the given type is const.
5254
template <typename Type, typename CType>
53-
handle xtensor_ref_array(CType &src, handle parent = none())
55+
handle xtensor_ref_array(CType& src, handle parent = none())
5456
{
5557
return xtensor_array_cast<Type>(src, parent, !std::is_const<CType>::value);
5658
}
@@ -60,14 +62,14 @@ namespace pybind11
6062
// its destruction to that of any dependent python objects. Const-ness is determined by whether or
6163
// not the CType of the pointer given is const.
6264
template <typename Type, typename CType>
63-
handle xtensor_encapsulate(CType *src)
65+
handle xtensor_encapsulate(CType* src)
6466
{
65-
capsule base(src, [](void *o) { delete static_cast<CType *>(o); });
67+
capsule base(src, [](void* o) { delete static_cast<CType*>(o); });
6668
return xtensor_ref_array<Type>(*src, base);
6769
}
6870

6971
// Base class of type_caster for xtensor and xarray
70-
template<class Type>
72+
template <class Type>
7173
struct xtensor_type_caster_base
7274
{
7375
bool load(handle src, bool)
@@ -79,7 +81,7 @@ namespace pybind11
7981

8082
// Cast implementation
8183
template <typename CType>
82-
static handle cast_impl(CType *src, return_value_policy policy, handle parent)
84+
static handle cast_impl(CType* src, return_value_policy policy, handle parent)
8385
{
8486
switch (policy)
8587
{
@@ -103,19 +105,19 @@ namespace pybind11
103105
public:
104106

105107
// Normal returned non-reference, non-const value:
106-
static handle cast(Type &&src, return_value_policy /* policy */, handle parent)
108+
static handle cast(Type&& src, return_value_policy /* policy */, handle parent)
107109
{
108110
return cast_impl(&src, return_value_policy::move, parent);
109111
}
110112

111113
// If you return a non-reference const, we mark the numpy array readonly:
112-
static handle cast(const Type &&src, return_value_policy /* policy */, handle parent)
114+
static handle cast(const Type&& src, return_value_policy /* policy */, handle parent)
113115
{
114116
return cast_impl(&src, return_value_policy::move, parent);
115117
}
116118

117119
// lvalue reference return; default (automatic) becomes copy
118-
static handle cast(Type &src, return_value_policy policy, handle parent)
120+
static handle cast(Type& src, return_value_policy policy, handle parent)
119121
{
120122
if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
121123
{
@@ -126,7 +128,7 @@ namespace pybind11
126128
}
127129

128130
// const lvalue reference return; default (automatic) becomes copy
129-
static handle cast(const Type &src, return_value_policy policy, handle parent)
131+
static handle cast(const Type& src, return_value_policy policy, handle parent)
130132
{
131133
if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
132134
{
@@ -137,20 +139,20 @@ namespace pybind11
137139
}
138140

139141
// non-const pointer return
140-
static handle cast(Type *src, return_value_policy policy, handle parent)
142+
static handle cast(Type* src, return_value_policy policy, handle parent)
141143
{
142144
return cast_impl(src, policy, parent);
143145
}
144146

145147
// const pointer return
146-
static handle cast(const Type *src, return_value_policy policy, handle parent)
148+
static handle cast(const Type* src, return_value_policy policy, handle parent)
147149
{
148150
return cast_impl(src, policy, parent);
149151
}
150152

151153
static PYBIND11_DESCR name()
152154
{
153-
return _("xt::xtensor");
155+
return _("xt::xtensor");
154156
}
155157

156158
template <typename T>

0 commit comments

Comments
 (0)