blob: 6c26f41baaaa15a641704bd3650b08b0b55359d8 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
2 pybind/cast.h: Partial template specializations to cast between
3 C++ and Python types
4
5 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
6
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020011#pragma once
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020013#include <pybind/pytypes.h>
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020014#include <pybind/typeid.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020015#include <map>
16#include <array>
17
18NAMESPACE_BEGIN(pybind)
19NAMESPACE_BEGIN(detail)
20
21/// Generic type caster for objects stored on the heap
22template <typename type> class type_caster {
23public:
24 typedef instance<type> instance_type;
25
26 static std::string name() { return type_id<type>(); }
27
28 type_caster() {
29 auto const& registered_types = get_internals().registered_types;
30 auto it = registered_types.find(type_id<type>());
31 if (it != registered_types.end())
32 typeinfo = &it->second;
33 }
34
35 bool load(PyObject *src, bool convert) {
36 if (src == nullptr || typeinfo == nullptr)
37 return false;
38 if (PyType_IsSubtype(Py_TYPE(src), typeinfo->type)) {
39 value = ((instance_type *) src)->value;
40 return true;
41 }
42 if (convert) {
43 for (auto &converter : typeinfo->implicit_conversions) {
44 temp = object(converter(src, typeinfo->type), false);
45 if (load(temp.ptr(), false))
46 return true;
47 }
48 }
49 return false;
50 }
51
52 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
53 if (policy == return_value_policy::automatic)
54 policy = return_value_policy::copy;
55 return cast(&src, policy, parent);
56 }
57
58 static PyObject *cast(const type *_src, return_value_policy policy, PyObject *parent) {
59 type *src = const_cast<type *>(_src);
60 if (src == nullptr) {
61 Py_INCREF(Py_None);
62 return Py_None;
63 }
64 // avoid an issue with internal references matching their parent's address
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020065 bool dont_cache = policy == return_value_policy::reference_internal &&
66 parent && ((instance<void> *) parent)->value == (void *) src;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020067 auto& internals = get_internals();
68 auto it_instance = internals.registered_instances.find(src);
69 if (it_instance != internals.registered_instances.end() && !dont_cache) {
70 PyObject *inst = it_instance->second;
71 Py_INCREF(inst);
72 return inst;
73 }
74 auto it = internals.registered_types.find(type_id<type>());
75 if (it == internals.registered_types.end()) {
76 std::string msg = std::string("Unregistered type : ") + type_id<type>();
77 PyErr_SetString(PyExc_TypeError, msg.c_str());
78 return nullptr;
79 }
80 auto &type_info = it->second;
81 instance_type *inst = (instance_type *) PyType_GenericAlloc(type_info.type, 0);
82 inst->value = src;
83 inst->owned = true;
84 inst->parent = nullptr;
85 if (policy == return_value_policy::automatic)
86 policy = return_value_policy::take_ownership;
87 handle_return_value_policy<type>(inst, policy, parent);
88 PyObject *inst_pyobj = (PyObject *) inst;
89 type_info.init_holder(inst_pyobj);
90 if (!dont_cache)
91 internals.registered_instances[inst->value] = inst_pyobj;
92 return inst_pyobj;
93 }
94
95 template <class T, typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0>
96 static void handle_return_value_policy(instance<T> *inst, return_value_policy policy, PyObject *parent) {
97 if (policy == return_value_policy::copy) {
98 inst->value = new T(*(inst->value));
99 } else if (policy == return_value_policy::reference) {
100 inst->owned = false;
101 } else if (policy == return_value_policy::reference_internal) {
102 inst->owned = false;
103 inst->parent = parent;
104 Py_XINCREF(parent);
105 }
106 }
107
108 template <class T, typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0>
109 static void handle_return_value_policy(instance<T> *inst, return_value_policy policy, PyObject *parent) {
110 if (policy == return_value_policy::copy) {
111 throw cast_error("return_value_policy = copy, but the object is non-copyable!");
112 } else if (policy == return_value_policy::reference) {
113 inst->owned = false;
114 } else if (policy == return_value_policy::reference_internal) {
115 inst->owned = false;
116 inst->parent = parent;
117 Py_XINCREF(parent);
118 }
119 }
120
121 operator type*() { return value; }
122 operator type&() { return *value; }
123protected:
124 type *value = nullptr;
125 const type_info *typeinfo = nullptr;
126 object temp;
127};
128
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200129#define PYBIND_TYPE_CASTER(type, py_name) \
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200130 protected: \
131 type value; \
132 public: \
133 static std::string name() { return py_name; } \
134 static PyObject *cast(const type *src, return_value_policy policy, PyObject *parent) { \
135 return cast(*src, policy, parent); \
136 } \
137 operator type*() { return &value; } \
138 operator type&() { return value; } \
139
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200140#define PYBIND_TYPE_CASTER_NUMBER(type, py_type, from_type, to_pytype) \
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200141 template <> class type_caster<type> { \
142 public: \
143 bool load(PyObject *src, bool) { \
144 value = (type) from_type(src); \
145 if (value == (type) -1 && PyErr_Occurred()) { \
146 PyErr_Clear(); \
147 return false; \
148 } \
149 return true; \
150 } \
151 static PyObject *cast(type src, return_value_policy /* policy */, PyObject * /* parent */) { \
152 return to_pytype((py_type) src); \
153 } \
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200154 PYBIND_TYPE_CASTER(type, #type); \
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200155 };
156
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200157PYBIND_TYPE_CASTER_NUMBER(int32_t, long, PyLong_AsLong, PyLong_FromLong)
158PYBIND_TYPE_CASTER_NUMBER(uint32_t, unsigned long, PyLong_AsUnsignedLong, PyLong_FromUnsignedLong)
159PYBIND_TYPE_CASTER_NUMBER(int64_t, PY_LONG_LONG, PyLong_AsLongLong, PyLong_FromLongLong)
160PYBIND_TYPE_CASTER_NUMBER(uint64_t, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong, PyLong_FromUnsignedLongLong)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200161
162#if defined(__APPLE__) // size_t/ssize_t are separate types on Mac OS X
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200163PYBIND_TYPE_CASTER_NUMBER(ssize_t, Py_ssize_t, PyLong_AsSsize_t, PyLong_FromSsize_t)
164PYBIND_TYPE_CASTER_NUMBER(size_t, size_t, PyLong_AsSize_t, PyLong_FromSize_t)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200165#endif
166
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200167PYBIND_TYPE_CASTER_NUMBER(float, float, PyFloat_AsDouble, PyFloat_FromDouble)
168PYBIND_TYPE_CASTER_NUMBER(double, double, PyFloat_AsDouble, PyFloat_FromDouble)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200169
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200170template <> class type_caster<detail::void_type> {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200171public:
172 bool load(PyObject *, bool) { return true; }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200173 static PyObject *cast(detail::void_type, return_value_policy /* policy */, PyObject * /* parent */) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200174 Py_INCREF(Py_None);
175 return Py_None;
176 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200177 PYBIND_TYPE_CASTER(detail::void_type, "None");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200178};
179
180template <> class type_caster<bool> {
181public:
182 bool load(PyObject *src, bool) {
183 if (src == Py_True) { value = true; return true; }
184 else if (src == Py_False) { value = false; return true; }
185 else return false;
186 }
187 static PyObject *cast(bool src, return_value_policy /* policy */, PyObject * /* parent */) {
188 PyObject *result = src ? Py_True : Py_False;
189 Py_INCREF(result);
190 return result;
191 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200192 PYBIND_TYPE_CASTER(bool, "bool");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200193};
194
195template <> class type_caster<std::string> {
196public:
197 bool load(PyObject *src, bool) {
198 const char *ptr = PyUnicode_AsUTF8(src);
199 if (!ptr) { PyErr_Clear(); return false; }
200 value = std::string(ptr);
201 return true;
202 }
203 static PyObject *cast(const std::string &src, return_value_policy /* policy */, PyObject * /* parent */) {
204 return PyUnicode_FromString(src.c_str());
205 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200206 PYBIND_TYPE_CASTER(std::string, "str");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200207};
208
Wenzel Jakob2ac80e72015-07-22 00:59:01 +0200209#ifdef HAVE_WCHAR_H
210template <> class type_caster<std::wstring> {
211public:
212 bool load(PyObject *src, bool) {
213 const wchar_t *ptr = PyUnicode_AsWideCharString(src, nullptr);
214 if (!ptr) { PyErr_Clear(); return false; }
215 value = std::wstring(ptr);
216 return true;
217 }
218 static PyObject *cast(const std::wstring &src, return_value_policy /* policy */, PyObject * /* parent */) {
219 return PyUnicode_FromWideChar(src.c_str(), src.length());
220 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200221 PYBIND_TYPE_CASTER(std::wstring, "wstr");
Wenzel Jakob2ac80e72015-07-22 00:59:01 +0200222};
223#endif
224
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200225template <> class type_caster<char> {
226public:
227 bool load(PyObject *src, bool) {
228 char *ptr = PyUnicode_AsUTF8(src);
229 if (!ptr) { PyErr_Clear(); return false; }
230 value = ptr;
231 return true;
232 }
233
234 static PyObject *cast(const char *src, return_value_policy /* policy */, PyObject * /* parent */) {
235 return PyUnicode_FromString(src);
236 }
237
238 static PyObject *cast(char src, return_value_policy /* policy */, PyObject * /* parent */) {
239 char str[2] = { src, '\0' };
240 return PyUnicode_DecodeLatin1(str, 1, nullptr);
241 }
242
243 static std::string name() { return "str"; }
244
245 operator char*() { return value; }
246 operator char() { return *value; }
247protected:
248 char *value;
249};
250
251template <typename Value> struct type_caster<std::vector<Value>> {
252 typedef std::vector<Value> type;
253 typedef type_caster<Value> value_conv;
254public:
255 bool load(PyObject *src, bool convert) {
256 if (!PyList_Check(src))
257 return false;
258 size_t size = (size_t) PyList_GET_SIZE(src);
259 value.reserve(size);
260 value.clear();
261 for (size_t i=0; i<size; ++i) {
262 value_conv conv;
263 if (!conv.load(PyList_GetItem(src, (ssize_t) i), convert))
264 return false;
265 value.push_back((Value) conv);
266 }
267 return true;
268 }
269
270 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
271 PyObject *list = PyList_New(src.size());
272 size_t index = 0;
273 for (auto const &value: src) {
274 PyObject *value_ = value_conv::cast(value, policy, parent);
275 if (!value_) {
276 Py_DECREF(list);
277 return nullptr;
278 }
279 PyList_SetItem(list, index++, value_);
280 }
281 return list;
282 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200283 PYBIND_TYPE_CASTER(type, "list<" + value_conv::name() + ">");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200284};
285
286template <typename Key, typename Value> struct type_caster<std::map<Key, Value>> {
287public:
288 typedef std::map<Key, Value> type;
289 typedef type_caster<Key> key_conv;
290 typedef type_caster<Value> value_conv;
291
292 bool load(PyObject *src, bool convert) {
293 if (!PyDict_Check(src))
294 return false;
295
296 value.clear();
297 PyObject *key_, *value_;
298 ssize_t pos = 0;
299 key_conv kconv;
300 value_conv vconv;
301 while (PyDict_Next(src, &pos, &key_, &value_)) {
302 if (!kconv.load(key_, convert) || !vconv.load(value_, convert))
303 return false;
304 value[kconv] = vconv;
305 }
306 return true;
307 }
308
309 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
310 PyObject *dict = PyDict_New();
311 for (auto const &kv: src) {
312 PyObject *key = key_conv::cast(kv.first, policy, parent);
313 PyObject *value = value_conv::cast(kv.second, policy, parent);
314 if (!key || !value || PyDict_SetItem(dict, key, value) < 0) {
315 Py_XDECREF(key);
316 Py_XDECREF(value);
317 Py_DECREF(dict);
318 return nullptr;
319 }
320 Py_DECREF(key);
321 Py_DECREF(value);
322 }
323 return dict;
324 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200325 PYBIND_TYPE_CASTER(type, "dict<" + key_conv::name() + ", " + value_conv::name() + ">");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200326};
327
328template <typename T1, typename T2> class type_caster<std::pair<T1, T2>> {
329 typedef std::pair<T1, T2> type;
330public:
331 bool load(PyObject *src, bool convert) {
332 if (!PyTuple_Check(src) || PyTuple_Size(src) != 2)
333 return false;
334 if (!first.load(PyTuple_GetItem(src, 0), convert))
335 return false;
336 return second.load(PyTuple_GetItem(src, 1), convert);
337 }
338
339 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200340 PyObject *o1 = type_caster<typename detail::decay<T1>::type>::cast(src.first, policy, parent);
341 PyObject *o2 = type_caster<typename detail::decay<T2>::type>::cast(src.second, policy, parent);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200342 if (!o1 || !o2) {
343 Py_XDECREF(o1);
344 Py_XDECREF(o2);
345 return nullptr;
346 }
347 PyObject *tuple = PyTuple_New(2);
348 PyTuple_SetItem(tuple, 0, o1);
349 PyTuple_SetItem(tuple, 1, o2);
350 return tuple;
351 }
352
353 static std::string name() {
354 return "(" + type_caster<T1>::name() + ", " + type_caster<T2>::name() + ")";
355 }
356
357 operator type() {
358 return type(first, second);
359 }
360protected:
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200361 type_caster<typename detail::decay<T1>::type> first;
362 type_caster<typename detail::decay<T2>::type> second;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200363};
364
365template <typename ... Tuple> class type_caster<std::tuple<Tuple...>> {
366 typedef std::tuple<Tuple...> type;
367public:
368 enum { size = sizeof...(Tuple) };
369
370 bool load(PyObject *src, bool convert) {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200371 return load(src, convert, typename make_index_sequence<sizeof...(Tuple)>::type());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200372 }
373
374 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200375 return cast(src, policy, parent, typename make_index_sequence<size>::type());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200376 }
377
378 static std::string name() {
379 std::array<std::string, size> names {{
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200380 type_caster<typename detail::decay<Tuple>::type>::name()...
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200381 }};
382 std::string result("(");
383 int counter = 0;
384 for (auto const &name : names) {
385 result += name;
386 if (++counter < size)
387 result += ", ";
388 }
389 result += ")";
390 return result;
391 }
392
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200393 template <typename ReturnValue, typename Func> typename std::enable_if<!std::is_void<ReturnValue>::value, ReturnValue>::type call(Func &f) {
394 return call<ReturnValue, Func>(f, typename make_index_sequence<sizeof...(Tuple)>::type());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200395 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200396
397 template <typename ReturnValue, typename Func> typename std::enable_if<std::is_void<ReturnValue>::value, detail::void_type>::type call(Func &f) {
398 call<ReturnValue, Func>(f, typename make_index_sequence<sizeof...(Tuple)>::type());
399 return detail::void_type();
400 }
401
402 operator type() {
403 return cast(typename make_index_sequence<sizeof...(Tuple)>::type());
404 }
405
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200406protected:
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200407 template <typename ReturnValue, typename Func, size_t ... Index> ReturnValue call(Func &f, index_sequence<Index...>) {
408 return f((Tuple) std::get<Index>(value)...);
409 }
410
411 template <size_t ... Index> type cast(index_sequence<Index...>) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200412 return type((Tuple) std::get<Index>(value)...);
413 }
414
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200415 template <size_t ... Indices> bool load(PyObject *src, bool convert, index_sequence<Indices...>) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200416 if (!PyTuple_Check(src))
417 return false;
418 if (PyTuple_Size(src) != size)
419 return false;
420 std::array<bool, size> results {{
421 std::get<Indices>(value).load(PyTuple_GetItem(src, Indices), convert)...
422 }};
423 for (bool r : results)
424 if (!r)
425 return false;
426 return true;
427 }
428
429 /* Implementation: Convert a C++ tuple into a Python tuple */
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200430 template <size_t ... Indices> static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent, index_sequence<Indices...>) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200431 std::array<PyObject *, size> results {{
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200432 type_caster<typename detail::decay<Tuple>::type>::cast(std::get<Indices>(src), policy, parent)...
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200433 }};
434 bool success = true;
435 for (auto result : results)
436 if (result == nullptr)
437 success = false;
438 if (success) {
439 PyObject *tuple = PyTuple_New(size);
440 int counter = 0;
441 for (auto result : results)
442 PyTuple_SetItem(tuple, counter++, result);
443 return tuple;
444 } else {
445 for (auto result : results) {
446 Py_XDECREF(result);
447 }
448 return nullptr;
449 }
450 }
451
452protected:
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200453 std::tuple<type_caster<typename detail::decay<Tuple>::type>...> value;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200454};
455
456/// Type caster for holder types like std::shared_ptr, etc.
457template <typename type, typename holder_type> class type_caster_holder : public type_caster<type> {
458public:
459 typedef type_caster<type> parent;
460 bool load(PyObject *src, bool convert) {
461 if (!parent::load(src, convert))
462 return false;
463 holder = holder_type(parent::value);
464 return true;
465 }
466 explicit operator type*() { return this->value; }
467 explicit operator type&() { return *(this->value); }
468 explicit operator holder_type&() { return holder; }
469 explicit operator holder_type*() { return &holder; }
470protected:
471 holder_type holder;
472};
473
474template <> class type_caster<handle> {
475public:
476 bool load(PyObject *src) {
477 value = handle(src);
478 return true;
479 }
480 static PyObject *cast(const handle &src, return_value_policy /* policy */, PyObject * /* parent */) {
481 src.inc_ref();
482 return (PyObject *) src.ptr();
483 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200484 PYBIND_TYPE_CASTER(handle, "handle");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200485};
486
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200487#define PYBIND_TYPE_CASTER_PYTYPE(name) \
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200488 template <> class type_caster<name> { \
489 public: \
490 bool load(PyObject *src, bool) { value = name(src, true); return true; } \
491 static PyObject *cast(const name &src, return_value_policy /* policy */, PyObject * /* parent */) { \
492 src.inc_ref(); return (PyObject *) src.ptr(); \
493 } \
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200494 PYBIND_TYPE_CASTER(name, #name); \
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200495 };
496
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200497PYBIND_TYPE_CASTER_PYTYPE(object) PYBIND_TYPE_CASTER_PYTYPE(buffer)
498PYBIND_TYPE_CASTER_PYTYPE(capsule) PYBIND_TYPE_CASTER_PYTYPE(dict)
499PYBIND_TYPE_CASTER_PYTYPE(float_) PYBIND_TYPE_CASTER_PYTYPE(int_)
500PYBIND_TYPE_CASTER_PYTYPE(list) PYBIND_TYPE_CASTER_PYTYPE(slice)
501PYBIND_TYPE_CASTER_PYTYPE(tuple) PYBIND_TYPE_CASTER_PYTYPE(function)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200502
503NAMESPACE_END(detail)
504
505template <typename T> inline T cast(PyObject *object) {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200506 detail::type_caster<typename detail::decay<T>::type> conv;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200507 if (!conv.load(object, true))
508 throw cast_error("Unable to cast Python object to C++ type");
509 return conv;
510}
511
512template <typename T> inline object cast(const T &value, return_value_policy policy = return_value_policy::automatic, PyObject *parent = nullptr) {
513 if (policy == return_value_policy::automatic)
514 policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200515 return object(detail::type_caster<typename detail::decay<T>::type>::cast(value, policy, parent), false);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200516}
517
518template <typename T> inline T handle::cast() { return pybind::cast<T>(m_ptr); }
519
520template <typename ... Args> inline object handle::call(Args&&... args_) {
521 const size_t size = sizeof...(Args);
522 std::array<PyObject *, size> args{
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200523 { detail::type_caster<typename detail::decay<Args>::type>::cast(
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200524 std::forward<Args>(args_), return_value_policy::automatic, nullptr)... }
525 };
526 bool fail = false;
527 for (auto result : args)
528 if (result == nullptr)
529 fail = true;
530 if (fail) {
531 for (auto result : args) {
532 Py_XDECREF(result);
533 }
534 throw cast_error("handle::call(): unable to convert input arguments to Python objects");
535 }
536 PyObject *tuple = PyTuple_New(size);
537 int counter = 0;
538 for (auto result : args)
539 PyTuple_SetItem(tuple, counter++, result);
540 PyObject *result = PyObject_CallObject(m_ptr, tuple);
541 Py_DECREF(tuple);
542 return object(result, false);
543}
544
545NAMESPACE_END(pybind)