Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
| 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 Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 11 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 12 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 13 | #include <pybind/pytypes.h> |
| 14 | #include <pybind/mpl.h> |
| 15 | #include <pybind/typeid.h> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <array> |
| 18 | |
| 19 | NAMESPACE_BEGIN(pybind) |
| 20 | NAMESPACE_BEGIN(detail) |
| 21 | |
| 22 | /// Generic type caster for objects stored on the heap |
| 23 | template <typename type> class type_caster { |
| 24 | public: |
| 25 | typedef instance<type> instance_type; |
| 26 | |
| 27 | static std::string name() { return type_id<type>(); } |
| 28 | |
| 29 | type_caster() { |
| 30 | auto const& registered_types = get_internals().registered_types; |
| 31 | auto it = registered_types.find(type_id<type>()); |
| 32 | if (it != registered_types.end()) |
| 33 | typeinfo = &it->second; |
| 34 | } |
| 35 | |
| 36 | bool load(PyObject *src, bool convert) { |
| 37 | if (src == nullptr || typeinfo == nullptr) |
| 38 | return false; |
| 39 | if (PyType_IsSubtype(Py_TYPE(src), typeinfo->type)) { |
| 40 | value = ((instance_type *) src)->value; |
| 41 | return true; |
| 42 | } |
| 43 | if (convert) { |
| 44 | for (auto &converter : typeinfo->implicit_conversions) { |
| 45 | temp = object(converter(src, typeinfo->type), false); |
| 46 | if (load(temp.ptr(), false)) |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) { |
| 54 | if (policy == return_value_policy::automatic) |
| 55 | policy = return_value_policy::copy; |
| 56 | return cast(&src, policy, parent); |
| 57 | } |
| 58 | |
| 59 | static PyObject *cast(const type *_src, return_value_policy policy, PyObject *parent) { |
| 60 | type *src = const_cast<type *>(_src); |
| 61 | if (src == nullptr) { |
| 62 | Py_INCREF(Py_None); |
| 63 | return Py_None; |
| 64 | } |
| 65 | // avoid an issue with internal references matching their parent's address |
| 66 | bool dont_cache = parent && ((instance<void> *) parent)->value == (void *) src; |
| 67 | 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; } |
| 123 | protected: |
| 124 | type *value = nullptr; |
| 125 | const type_info *typeinfo = nullptr; |
| 126 | object temp; |
| 127 | }; |
| 128 | |
| 129 | #define TYPE_CASTER(type, py_name) \ |
| 130 | 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 | |
| 140 | #define TYPE_CASTER_NUMBER(type, py_type, from_type, to_pytype) \ |
| 141 | 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 | } \ |
| 154 | TYPE_CASTER(type, #type); \ |
| 155 | }; |
| 156 | |
| 157 | TYPE_CASTER_NUMBER(int32_t, long, PyLong_AsLong, PyLong_FromLong) |
| 158 | TYPE_CASTER_NUMBER(uint32_t, unsigned long, PyLong_AsUnsignedLong, PyLong_FromUnsignedLong) |
| 159 | TYPE_CASTER_NUMBER(int64_t, PY_LONG_LONG, PyLong_AsLongLong, PyLong_FromLongLong) |
| 160 | TYPE_CASTER_NUMBER(uint64_t, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong, PyLong_FromUnsignedLongLong) |
| 161 | |
| 162 | #if defined(__APPLE__) // size_t/ssize_t are separate types on Mac OS X |
| 163 | TYPE_CASTER_NUMBER(ssize_t, Py_ssize_t, PyLong_AsSsize_t, PyLong_FromSsize_t) |
| 164 | TYPE_CASTER_NUMBER(size_t, size_t, PyLong_AsSize_t, PyLong_FromSize_t) |
| 165 | #endif |
| 166 | |
| 167 | TYPE_CASTER_NUMBER(float, float, PyFloat_AsDouble, PyFloat_FromDouble) |
| 168 | TYPE_CASTER_NUMBER(double, double, PyFloat_AsDouble, PyFloat_FromDouble) |
| 169 | |
| 170 | template <> class type_caster<mpl::detail::void_type> { |
| 171 | public: |
| 172 | bool load(PyObject *, bool) { return true; } |
| 173 | static PyObject *cast(mpl::detail::void_type, return_value_policy /* policy */, PyObject * /* parent */) { |
| 174 | Py_INCREF(Py_None); |
| 175 | return Py_None; |
| 176 | } |
| 177 | TYPE_CASTER(mpl::detail::void_type, "None"); |
| 178 | }; |
| 179 | |
| 180 | template <> class type_caster<bool> { |
| 181 | public: |
| 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 | } |
| 192 | TYPE_CASTER(bool, "bool"); |
| 193 | }; |
| 194 | |
| 195 | template <> class type_caster<std::string> { |
| 196 | public: |
| 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 | } |
| 206 | TYPE_CASTER(std::string, "str"); |
| 207 | }; |
| 208 | |
Wenzel Jakob | 2ac80e7 | 2015-07-22 00:59:01 +0200 | [diff] [blame^] | 209 | #ifdef HAVE_WCHAR_H |
| 210 | template <> class type_caster<std::wstring> { |
| 211 | public: |
| 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 | } |
| 221 | TYPE_CASTER(std::wstring, "wstr"); |
| 222 | }; |
| 223 | #endif |
| 224 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 225 | template <> class type_caster<char> { |
| 226 | public: |
| 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; } |
| 247 | protected: |
| 248 | char *value; |
| 249 | }; |
| 250 | |
| 251 | template <typename Value> struct type_caster<std::vector<Value>> { |
| 252 | typedef std::vector<Value> type; |
| 253 | typedef type_caster<Value> value_conv; |
| 254 | public: |
| 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 | } |
| 283 | TYPE_CASTER(type, "list<" + value_conv::name() + ">"); |
| 284 | }; |
| 285 | |
| 286 | template <typename Key, typename Value> struct type_caster<std::map<Key, Value>> { |
| 287 | public: |
| 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 | } |
| 325 | TYPE_CASTER(type, "dict<" + key_conv::name() + ", " + value_conv::name() + ">"); |
| 326 | }; |
| 327 | |
| 328 | template <typename T1, typename T2> class type_caster<std::pair<T1, T2>> { |
| 329 | typedef std::pair<T1, T2> type; |
| 330 | public: |
| 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) { |
| 340 | PyObject *o1 = type_caster<typename mpl::normalize_type<T1>::type>::cast(src.first, policy, parent); |
| 341 | PyObject *o2 = type_caster<typename mpl::normalize_type<T2>::type>::cast(src.second, policy, parent); |
| 342 | 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 | } |
| 360 | protected: |
| 361 | type_caster<typename mpl::normalize_type<T1>::type> first; |
| 362 | type_caster<typename mpl::normalize_type<T2>::type> second; |
| 363 | }; |
| 364 | |
| 365 | template <typename ... Tuple> class type_caster<std::tuple<Tuple...>> { |
| 366 | typedef std::tuple<Tuple...> type; |
| 367 | public: |
| 368 | enum { size = sizeof...(Tuple) }; |
| 369 | |
| 370 | bool load(PyObject *src, bool convert) { |
| 371 | return load(src, convert, typename mpl::make_index_sequence<sizeof...(Tuple)>::type()); |
| 372 | } |
| 373 | |
| 374 | static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) { |
| 375 | return cast(src, policy, parent, typename mpl::make_index_sequence<size>::type()); |
| 376 | } |
| 377 | |
| 378 | static std::string name() { |
| 379 | std::array<std::string, size> names {{ |
| 380 | type_caster<typename mpl::normalize_type<Tuple>::type>::name()... |
| 381 | }}; |
| 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 | |
| 393 | operator type() { |
| 394 | return cast(typename mpl::make_index_sequence<sizeof...(Tuple)>::type()); |
| 395 | } |
| 396 | protected: |
| 397 | template <size_t ... Index> type cast(mpl::index_sequence<Index...>) { |
| 398 | return type((Tuple) std::get<Index>(value)...); |
| 399 | } |
| 400 | |
| 401 | template <size_t ... Indices> bool load(PyObject *src, bool convert, mpl::index_sequence<Indices...>) { |
| 402 | if (!PyTuple_Check(src)) |
| 403 | return false; |
| 404 | if (PyTuple_Size(src) != size) |
| 405 | return false; |
| 406 | std::array<bool, size> results {{ |
| 407 | std::get<Indices>(value).load(PyTuple_GetItem(src, Indices), convert)... |
| 408 | }}; |
| 409 | for (bool r : results) |
| 410 | if (!r) |
| 411 | return false; |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | /* Implementation: Convert a C++ tuple into a Python tuple */ |
| 416 | template <size_t ... Indices> static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent, mpl::index_sequence<Indices...>) { |
| 417 | std::array<PyObject *, size> results {{ |
| 418 | type_caster<typename mpl::normalize_type<Tuple>::type>::cast(std::get<Indices>(src), policy, parent)... |
| 419 | }}; |
| 420 | bool success = true; |
| 421 | for (auto result : results) |
| 422 | if (result == nullptr) |
| 423 | success = false; |
| 424 | if (success) { |
| 425 | PyObject *tuple = PyTuple_New(size); |
| 426 | int counter = 0; |
| 427 | for (auto result : results) |
| 428 | PyTuple_SetItem(tuple, counter++, result); |
| 429 | return tuple; |
| 430 | } else { |
| 431 | for (auto result : results) { |
| 432 | Py_XDECREF(result); |
| 433 | } |
| 434 | return nullptr; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | protected: |
| 439 | std::tuple<type_caster<typename mpl::normalize_type<Tuple>::type>...> value; |
| 440 | }; |
| 441 | |
| 442 | /// Type caster for holder types like std::shared_ptr, etc. |
| 443 | template <typename type, typename holder_type> class type_caster_holder : public type_caster<type> { |
| 444 | public: |
| 445 | typedef type_caster<type> parent; |
| 446 | bool load(PyObject *src, bool convert) { |
| 447 | if (!parent::load(src, convert)) |
| 448 | return false; |
| 449 | holder = holder_type(parent::value); |
| 450 | return true; |
| 451 | } |
| 452 | explicit operator type*() { return this->value; } |
| 453 | explicit operator type&() { return *(this->value); } |
| 454 | explicit operator holder_type&() { return holder; } |
| 455 | explicit operator holder_type*() { return &holder; } |
| 456 | protected: |
| 457 | holder_type holder; |
| 458 | }; |
| 459 | |
| 460 | template <> class type_caster<handle> { |
| 461 | public: |
| 462 | bool load(PyObject *src) { |
| 463 | value = handle(src); |
| 464 | return true; |
| 465 | } |
| 466 | static PyObject *cast(const handle &src, return_value_policy /* policy */, PyObject * /* parent */) { |
| 467 | src.inc_ref(); |
| 468 | return (PyObject *) src.ptr(); |
| 469 | } |
| 470 | TYPE_CASTER(handle, "handle"); |
| 471 | }; |
| 472 | |
| 473 | #define TYPE_CASTER_PYTYPE(name) \ |
| 474 | template <> class type_caster<name> { \ |
| 475 | public: \ |
| 476 | bool load(PyObject *src, bool) { value = name(src, true); return true; } \ |
| 477 | static PyObject *cast(const name &src, return_value_policy /* policy */, PyObject * /* parent */) { \ |
| 478 | src.inc_ref(); return (PyObject *) src.ptr(); \ |
| 479 | } \ |
| 480 | TYPE_CASTER(name, #name); \ |
| 481 | }; |
| 482 | |
| 483 | TYPE_CASTER_PYTYPE(object) |
| 484 | TYPE_CASTER_PYTYPE(buffer) |
| 485 | TYPE_CASTER_PYTYPE(capsule) |
| 486 | TYPE_CASTER_PYTYPE(dict) |
| 487 | TYPE_CASTER_PYTYPE(float_) |
| 488 | TYPE_CASTER_PYTYPE(int_) |
| 489 | TYPE_CASTER_PYTYPE(list) |
| 490 | TYPE_CASTER_PYTYPE(slice) |
| 491 | TYPE_CASTER_PYTYPE(tuple) |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 492 | TYPE_CASTER_PYTYPE(function) |
Wenzel Jakob | 2ac80e7 | 2015-07-22 00:59:01 +0200 | [diff] [blame^] | 493 | TYPE_CASTER_PYTYPE(array) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 494 | |
| 495 | #undef TYPE_CASTER |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 496 | #undef TYPE_CASTER_PYTYPE |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 497 | #undef TYPE_CASTER_NUMBER |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 498 | |
| 499 | NAMESPACE_END(detail) |
| 500 | |
| 501 | template <typename T> inline T cast(PyObject *object) { |
| 502 | detail::type_caster<typename mpl::normalize_type<T>::type> conv; |
| 503 | if (!conv.load(object, true)) |
| 504 | throw cast_error("Unable to cast Python object to C++ type"); |
| 505 | return conv; |
| 506 | } |
| 507 | |
| 508 | template <typename T> inline object cast(const T &value, return_value_policy policy = return_value_policy::automatic, PyObject *parent = nullptr) { |
| 509 | if (policy == return_value_policy::automatic) |
| 510 | policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy; |
| 511 | return object(detail::type_caster<typename mpl::normalize_type<T>::type>::cast(value, policy, parent), false); |
| 512 | } |
| 513 | |
| 514 | template <typename T> inline T handle::cast() { return pybind::cast<T>(m_ptr); } |
| 515 | |
| 516 | template <typename ... Args> inline object handle::call(Args&&... args_) { |
| 517 | const size_t size = sizeof...(Args); |
| 518 | std::array<PyObject *, size> args{ |
| 519 | { detail::type_caster<typename mpl::normalize_type<Args>::type>::cast( |
| 520 | std::forward<Args>(args_), return_value_policy::automatic, nullptr)... } |
| 521 | }; |
| 522 | bool fail = false; |
| 523 | for (auto result : args) |
| 524 | if (result == nullptr) |
| 525 | fail = true; |
| 526 | if (fail) { |
| 527 | for (auto result : args) { |
| 528 | Py_XDECREF(result); |
| 529 | } |
| 530 | throw cast_error("handle::call(): unable to convert input arguments to Python objects"); |
| 531 | } |
| 532 | PyObject *tuple = PyTuple_New(size); |
| 533 | int counter = 0; |
| 534 | for (auto result : args) |
| 535 | PyTuple_SetItem(tuple, counter++, result); |
| 536 | PyObject *result = PyObject_CallObject(m_ptr, tuple); |
| 537 | Py_DECREF(tuple); |
| 538 | return object(result, false); |
| 539 | } |
| 540 | |
| 541 | NAMESPACE_END(pybind) |