-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathpybind11.h
More file actions
207 lines (195 loc) · 7.93 KB
/
Copy pathpybind11.h
File metadata and controls
207 lines (195 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* LSST Data Management System
* Copyright 2008-2016 AURA/LSST.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#ifndef NDARRAY_pybind11_h_INCLUDED
#define NDARRAY_pybind11_h_INCLUDED
/**
* @file ndarray/pybind11.h
* @brief Public header file for pybind11-based Python support.
*
* \warning Both the Numpy C-API headers "arrayobject.h" and
* "ufuncobject.h" must be included before ndarray/python.hpp
* or any of the files in ndarray/python.
*
* \note This file is not included by the main "ndarray.h" header file.
*/
/** \defgroup ndarrayPythonGroup Python Support
*
* The ndarray Python support module provides conversion
* functions between ndarray objects, notably Array and
* Vector, and Python Numpy objects.
*/
#include "pybind11.h"
#include "Python.h"
#include "ndarray.h"
#include "ndarray/converter/numpy.h"
#include "ndarray/converter/ufunctors.h"
#include "ndarray/converter/Vector.h"
#include "ndarray/converter/eigen.h"
NAMESPACE_BEGIN(pybind11)
NAMESPACE_BEGIN(detail)
/* @brief A pybind11 type_caster for ndarray::Array
*/
template <typename T, int N, int C>
class type_caster< ndarray::Array<T,N,C> > {
public:
bool load(handle src, bool) {
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< ndarray::Array<T,N,C> >::fromPythonStage1(_src)) {
PyErr_Clear();
return false;
}
return true;
}
static handle cast(const ndarray::Array<T,N,C> &src, return_value_policy /* policy */, handle /* parent */) {
return ndarray::PyConverter< ndarray::Array<T,N,C> >::toPython(src);
}
void set_value() {
if (!ndarray::PyConverter< ndarray::Array<T,N,C> >::fromPythonStage2(_src, _value)) {
throw error_already_set();
}
}
/* This part is normally created by the PYBIND11_TYPE_CASTER macro, which
* can't be used here due to the partial specialization
*/
protected:
ndarray::PyPtr _src;
ndarray::Array<T,N,C> _value;
public:
static PYBIND11_DESCR name() { return type_descr(_<ndarray::Array<T,N,C>>()); }
static handle cast(const ndarray::Array<T,N,C> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator ndarray::Array<T,N,C> * () { set_value(); return &_value; }
operator ndarray::Array<T,N,C> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
/* @brief A pybind11 type_caster for ndarray::EigenView
*/
template <typename T, int N, int C, typename Kind, int Rows, int Cols>
class type_caster< ndarray::EigenView<T,N,C,Kind,Rows,Cols> > {
public:
bool load(handle src, bool) {
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< ndarray::EigenView<T,N,C,Kind,Rows,Cols> >::fromPythonStage1(_src)) {
PyErr_Clear();
return false;
}
return true;
}
static handle cast(const ndarray::EigenView<T,N,C,Kind,Rows,Cols> &src, return_value_policy /* policy */, handle /* parent */) {
return ndarray::PyConverter< ndarray::EigenView<T,N,C,Kind,Rows,Cols> >::toPython(src);
}
void set_value() {
if (!ndarray::PyConverter< ndarray::EigenView<T,N,C,Kind,Rows,Cols> >::fromPythonStage2(_src, _value)) {
throw error_already_set();
}
}
/* This part is normally created by the PYBIND11_TYPE_CASTER macro, which
* can't be used here due to the partial specialization
*/
protected:
ndarray::PyPtr _src;
ndarray::EigenView<T,N,C,Kind,Rows,Cols> _value;
public:
static PYBIND11_DESCR name() { return type_descr(_<ndarray::EigenView<T,N,C,Kind,Rows,Cols>>()); }
static handle cast(const ndarray::EigenView<T,N,C,Kind,Rows,Cols> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator ndarray::EigenView<T,N,C,Kind,Rows,Cols> * () { set_value(); return &_value; }
operator ndarray::EigenView<T,N,C,Kind,Rows,Cols> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
/* @brief A pybind11 type_caster for Eigen::Array
*/
template<typename T, int R, int C, int O, int MR, int MC>
class type_caster< Eigen::Array<T,R,C,O,MR,MC> > {
public:
bool load(handle src, bool) {
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< Eigen::Array<T,R,C,O,MR,MC> >::fromPythonStage1(_src)) {
PyErr_Clear();
return false;
}
return true;
}
static handle cast(const Eigen::Array<T,R,C,O,MR,MC> &src, return_value_policy /* policy */, handle /* parent */) {
return ndarray::PyConverter< Eigen::Array<T,R,C,O,MR,MC> >::toPython(src);
}
void set_value() {
if (!ndarray::PyConverter< Eigen::Array<T,R,C,O,MR,MC> >::fromPythonStage2(_src, _value)) {
throw error_already_set();
}
}
/* This part is normally created by the PYBIND11_TYPE_CASTER macro, which
* can't be used here due to the partial specialization
*/
protected:
ndarray::PyPtr _src;
Eigen::Array<T,R,C,O,MR,MC> _value;
public:
static PYBIND11_DESCR name() { return type_descr(_<Eigen::Array<T,R,C,O,MR,MC>>()); }
static handle cast(const Eigen::Array<T,R,C,O,MR,MC> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator Eigen::Array<T,R,C,O,MR,MC> * () { set_value(); return &_value; }
operator Eigen::Array<T,R,C,O,MR,MC> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
/* @brief A pybind11 type_caster for Eigen::Matrix
*/
template<typename T, int R, int C, int O, int MR, int MC>
class type_caster< Eigen::Matrix<T,R,C,O,MR,MC> > {
public:
bool load(handle src, bool) {
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< Eigen::Matrix<T,R,C,O,MR,MC> >::fromPythonStage1(_src)) {
PyErr_Clear();
return false;
}
return true;
}
static handle cast(const Eigen::Matrix<T,R,C,O,MR,MC> &src, return_value_policy /* policy */, handle /* parent */) {
return ndarray::PyConverter< Eigen::Matrix<T,R,C,O,MR,MC> >::toPython(src);
}
void set_value() {
if (!ndarray::PyConverter< Eigen::Matrix<T,R,C,O,MR,MC> >::fromPythonStage2(_src, _value)) {
throw error_already_set();
}
}
/* This part is normally created by the PYBIND11_TYPE_CASTER macro, which
* can't be used here due to the partial specialization
*/
protected:
ndarray::PyPtr _src;
Eigen::Matrix<T,R,C,O,MR,MC> _value;
public:
static PYBIND11_DESCR name() { return type_descr(_<Eigen::Matrix<T,R,C,O,MR,MC>>()); }
static handle cast(const Eigen::Matrix<T,R,C,O,MR,MC> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator Eigen::Matrix<T,R,C,O,MR,MC> * () { set_value(); return &_value; }
operator Eigen::Matrix<T,R,C,O,MR,MC> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
NAMESPACE_END(detail)
NAMESPACE_END(pybind11)
#endif // !NDARRAY_pybind11_h_INCLUDED