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
2223namespace 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