1#if defined(BBM_PYTHON) && !defined(_BBM_NATIVE_PYTHON_H_)
2#define _BBM_NATIVE_PYTHON_H_
6#include <pybind11/numpy.h>
7#include "backbone/array.h"
12 template<
typename ARR>
13 struct type_caster<ARR,
std::enable_if_t<backbone::is_array_v<ARR>>>
15 using array_type = std::decay_t<ARR>;
16 using value_type =
typename array_type::value_type;
19 bool load(handle src,
bool convert)
29 if(!isinstance<array_t<array_type>>(src) && !convert)
return false;
32 array arr = reinterpret_borrow<array>(src);
33 arr = array_t<value_type, array::c_style>::ensure(arr);
34 if(!arr)
return false;
37 if(arr.ndim() != 1 && convert)
return false;
38 if(arr.shape()[0] !=
value.size())
return false;
41 const value_type* buf =
static_cast<const value_type*
>(arr.data());
42 for(
size_t i=0; i <
value.size(); ++i)
48 static handle
cast(
const array_type* src, return_value_policy policy, handle parent)
50 if(!src)
return pybind11::none();
51 return cast(*src, policy, parent);
54 static handle
cast(
const array_type& src, return_value_policy , handle )
56 std::array<size_t, 1> shape{src.size()};
57 std::array<size_t, 1> stride{
sizeof(value_type)};
59 array arr(pybind11::dtype::of<value_type>(),
60 std::vector<ssize_t>(shape.begin(), shape.end()),
61 std::vector<ssize_t>(stride.begin(), stride.end()));
63 value_type* buf =
static_cast<value_type*
>(arr.mutable_data());
64 for(
size_t i=0; i < src.size(); ++i)
70 template<
typename T>
using cast_op_type = pybind11::detail::cast_op_type<T>;
72 static constexpr auto name = _(
"numpy.ndarray[dtype=") + npy_format_descriptor<value_type>::name + _(
"], shape=(") + _<backbone::array_size<array_type>>() + _(
")");
74 operator array_type*() {
if (is_none)
return nullptr;
else return &
value; }
75 operator array_type&() {
if (is_none)
throw pybind11::cast_error(
"Cannot cast None or nullptr to an backbone::array.");
return value; }
NEWTYPE cast(OLDTYPE &&val)
cast
Definition: control.h:27
decltype(auto) value(T &&t)
return the value of an attribute, or if not an attribute the object
Definition: attribute_value.h:20