cast.h 22.3 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
    pybind11/cast.h: Partial template specializations to cast between
Wenzel Jakob's avatar
Wenzel Jakob committed
3
4
5
6
7
8
9
10
    C++ and Python types

    Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

11
#pragma once
Wenzel Jakob's avatar
Wenzel Jakob committed
12

13
14
#include "pytypes.h"
#include "typeid.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
15
#include <array>
Wenzel Jakob's avatar
Wenzel Jakob committed
16
#include <limits>
Wenzel Jakob's avatar
Wenzel Jakob committed
17

18
NAMESPACE_BEGIN(pybind11)
Wenzel Jakob's avatar
Wenzel Jakob committed
19
20
NAMESPACE_BEGIN(detail)

21
#if PY_MAJOR_VERSION >= 3
22
#define PYBIND11_AS_STRING PyBytes_AsString
23
#else
24
#define PYBIND11_AS_STRING PyString_AsString
25
26
#endif

27
/** Linked list descriptor type for function signatures (produces smaller binaries
28
    compared to a previous solution using std::string and operator +=) */
29
30
31
32
33
34
35
36
37
38
39
40
class descr {
public:
    struct entry {
        const std::type_info *type = nullptr;
        const char *str = nullptr;
        entry *next = nullptr;
        entry(const std::type_info *type) : type(type) { }
        entry(const char *str) : str(str) { }
    };

    descr() { }
    descr(descr &&d) : first(d.first), last(d.last) { d.first = d.last = nullptr; }
41
42
    PYBIND11_NOINLINE descr(const char *str) { first = last = new entry { str }; }
    PYBIND11_NOINLINE descr(const std::type_info &type) { first = last = new entry { &type }; }
43

44
    PYBIND11_NOINLINE void operator+(const char *str) {
45
46
47
48
49
        entry *next = new entry { str };
        last->next = next;
        last = next;
    }

50
    PYBIND11_NOINLINE void operator+(const std::type_info *type) {
51
52
53
54
55
        entry *next = new entry { type };
        last->next = next;
        last = next;
    }

56
    PYBIND11_NOINLINE void operator+=(descr &&other) {
57
58
59
60
61
62
        last->next = other.first;
        while (last->next)
            last = last->next;
        other.first = other.last = nullptr;
    }

63
    PYBIND11_NOINLINE friend descr operator+(descr &&l, descr &&r) {
64
65
66
67
68
        descr result(std::move(l));
        result += std::move(r);
        return result;
    }

69
    PYBIND11_NOINLINE std::string str() const {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        std::string result;
        auto const& registered_types = get_internals().registered_types;
        for (entry *it = first; it != nullptr; it = it->next) {
            if (it->type) {
                auto it2 = registered_types.find(it->type);
                if (it2 != registered_types.end()) {
                    result += it2->second.type->tp_name;
                } else {
                    std::string tname(it->type->name());
                    detail::clean_type_id(tname);
                    result += tname;
                }
            } else {
                result += it->str;
            }
        }
        return result;
    }

89
    PYBIND11_NOINLINE ~descr() {
90
91
92
93
94
95
96
97
98
99
100
        while (first) {
            entry *tmp = first->next;
            delete first;
            first = tmp;
        }
    }

    entry *first = nullptr;
    entry *last = nullptr;
};

101
class type_caster_custom {
Wenzel Jakob's avatar
Wenzel Jakob committed
102
public:
103
    PYBIND11_NOINLINE type_caster_custom(const std::type_info *type_info) {
104
        auto & registered_types = get_internals().registered_types;
105
        auto it = registered_types.find(type_info);
106
        if (it != registered_types.end()) {
Wenzel Jakob's avatar
Wenzel Jakob committed
107
            typeinfo = &it->second;
108
109
110
111
112
113
114
115
116
117
118
        } else {
            /* Unknown type?! Since std::type_info* often varies across
               module boundaries, the following does an explicit check */
            for (auto const &type : registered_types) {
                if (strcmp(type.first->name(), type_info->name()) == 0) {
                    registered_types[type_info] = type.second;
                    typeinfo = &type.second;
                    break;
                }
            }
        }
Wenzel Jakob's avatar
Wenzel Jakob committed
119
120
    }

121
    PYBIND11_NOINLINE bool load(PyObject *src, bool convert) {
Wenzel Jakob's avatar
Wenzel Jakob committed
122
123
124
        if (src == nullptr || typeinfo == nullptr)
            return false;
        if (PyType_IsSubtype(Py_TYPE(src), typeinfo->type)) {
125
            value = ((instance<void> *) src)->value;
Wenzel Jakob's avatar
Wenzel Jakob committed
126
127
128
129
130
131
132
133
134
135
136
137
            return true;
        }
        if (convert) {
            for (auto &converter : typeinfo->implicit_conversions) {
                temp = object(converter(src, typeinfo->type), false);
                if (load(temp.ptr(), false))
                    return true;
            }
        }
        return false;
    }

138
139
140
    PYBIND11_NOINLINE static PyObject *cast(const void *_src, return_value_policy policy, PyObject *parent,
                                            const std::type_info *type_info, void *(*copy_constructor)(const void *)) {
        void *src = const_cast<void *>(_src);
Wenzel Jakob's avatar
Wenzel Jakob committed
141
142
143
144
145
        if (src == nullptr) {
            Py_INCREF(Py_None);
            return Py_None;
        }
        // avoid an issue with internal references matching their parent's address
Wenzel Jakob's avatar
Wenzel Jakob committed
146
147
        bool dont_cache = policy == return_value_policy::reference_internal &&
                          parent && ((instance<void> *) parent)->value == (void *) src;
Wenzel Jakob's avatar
Wenzel Jakob committed
148
149
150
151
152
153
154
        auto& internals = get_internals();
        auto it_instance = internals.registered_instances.find(src);
        if (it_instance != internals.registered_instances.end() && !dont_cache) {
            PyObject *inst = it_instance->second;
            Py_INCREF(inst);
            return inst;
        }
155
        auto it = internals.registered_types.find(type_info);
Wenzel Jakob's avatar
Wenzel Jakob committed
156
        if (it == internals.registered_types.end()) {
157
158
            std::string msg = std::string("Unregistered type : ") + type_info->name();
            detail::clean_type_id(msg);
Wenzel Jakob's avatar
Wenzel Jakob committed
159
160
161
            PyErr_SetString(PyExc_TypeError, msg.c_str());
            return nullptr;
        }
162
163
        auto &reg_type = it->second;
        instance<void> *inst = (instance<void> *) PyType_GenericAlloc(reg_type.type, 0);
Wenzel Jakob's avatar
Wenzel Jakob committed
164
165
166
167
168
169
        inst->value = src;
        inst->owned = true;
        inst->parent = nullptr;
        if (policy == return_value_policy::automatic)
            policy = return_value_policy::take_ownership;
        if (policy == return_value_policy::copy) {
170
171
172
            inst->value = copy_constructor(inst->value);
            if (inst->value == nullptr)
                throw cast_error("return_value_policy = copy, but the object is non-copyable!");
Wenzel Jakob's avatar
Wenzel Jakob committed
173
174
175
176
177
178
179
        } else if (policy == return_value_policy::reference) {
            inst->owned = false;
        } else if (policy == return_value_policy::reference_internal) {
            inst->owned = false;
            inst->parent = parent;
            Py_XINCREF(parent);
        }
180
181
182
183
184
        PyObject *inst_pyobj = (PyObject *) inst;
        reg_type.init_holder(inst_pyobj);
        if (!dont_cache)
            internals.registered_instances[inst->value] = inst_pyobj;
        return inst_pyobj;
Wenzel Jakob's avatar
Wenzel Jakob committed
185
186
187
188
    }

protected:
    const type_info *typeinfo = nullptr;
189
    void *value = nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
190
191
192
    object temp;
};

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/// Generic type caster for objects stored on the heap
template <typename type> class type_caster : public type_caster_custom {
public:
    static descr name() { return typeid(type); }

    type_caster() : type_caster_custom(&typeid(type)) { }

    static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
        if (policy == return_value_policy::automatic)
            policy = return_value_policy::copy;
        return type_caster_custom::cast(&src, policy, parent, &typeid(type), &copy_constructor);
    }

    static PyObject *cast(const type *src, return_value_policy policy, PyObject *parent) {
        return type_caster_custom::cast(src, policy, parent, &typeid(type), &copy_constructor);
    }

    operator type*() { return (type *) value; }
Wenzel Jakob's avatar
Wenzel Jakob committed
211
    operator type&() { return *((type *) value); }
212
213
214
protected:
    template <typename T = type, typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0>
    static void *copy_constructor(const void *arg) {
Wenzel Jakob's avatar
Wenzel Jakob committed
215
        return new type(*((const type *)arg));
216
217
218
219
220
    }
    template <typename T = type, typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0>
    static void *copy_constructor(const void *) { return nullptr; }
};

221
#define PYBIND11_TYPE_CASTER(type, py_name) \
Wenzel Jakob's avatar
Wenzel Jakob committed
222
223
224
    protected: \
        type value; \
    public: \
Wenzel Jakob's avatar
Wenzel Jakob committed
225
        static descr name() { return py_name; } \
Wenzel Jakob's avatar
Wenzel Jakob committed
226
227
228
229
230
231
        static PyObject *cast(const type *src, return_value_policy policy, PyObject *parent) { \
            return cast(*src, policy, parent); \
        } \
        operator type*() { return &value; } \
        operator type&() { return value; } \

232
#define PYBIND11_TYPE_CASTER_NUMBER(type, py_type, from_type, to_pytype) \
Wenzel Jakob's avatar
Wenzel Jakob committed
233
234
235
    template <> class type_caster<type> { \
    public: \
        bool load(PyObject *src, bool) { \
Wenzel Jakob's avatar
Wenzel Jakob committed
236
237
            py_type py_value = from_type(src); \
            if ((py_value == (py_type) -1 && PyErr_Occurred()) || \
Wenzel Jakob's avatar
Wenzel Jakob committed
238
239
                (std::numeric_limits<type>::is_integer && \
                 sizeof(py_type) != sizeof(type) && \
Wenzel Jakob's avatar
Wenzel Jakob committed
240
241
                 (py_value < (py_type) std::numeric_limits<type>::min() || \
                  py_value > (py_type) std::numeric_limits<type>::max()))) { \
Wenzel Jakob's avatar
Wenzel Jakob committed
242
243
244
                PyErr_Clear(); \
                return false; \
            } \
Wenzel Jakob's avatar
Wenzel Jakob committed
245
            value = (type) py_value; \
Wenzel Jakob's avatar
Wenzel Jakob committed
246
247
248
249
250
            return true; \
        } \
        static PyObject *cast(type src, return_value_policy /* policy */, PyObject * /* parent */) { \
            return to_pytype((py_type) src); \
        } \
251
        PYBIND11_TYPE_CASTER(type, #type); \
Wenzel Jakob's avatar
Wenzel Jakob committed
252
253
    };

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#if PY_MAJOR_VERSION >= 3
#define PyLong_AsUnsignedLongLong_Fixed PyLong_AsUnsignedLongLong
#define PyLong_AsLongLong_Fixed PyLong_AsLongLong
#else
inline PY_LONG_LONG PyLong_AsLongLong_Fixed(PyObject *o) {
    if (PyInt_Check(o))
        return (PY_LONG_LONG) PyLong_AsLong(o);
    else
        return ::PyLong_AsLongLong(o);
}

inline unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong_Fixed(PyObject *o) {
    if (PyInt_Check(o))
        return (unsigned PY_LONG_LONG) PyLong_AsUnsignedLong(o);
    else
        return ::PyLong_AsUnsignedLongLong(o);
}
#endif

273
274
275
276
277
278
279
280
PYBIND11_TYPE_CASTER_NUMBER(int8_t, long, PyLong_AsLong, PyLong_FromLong)
PYBIND11_TYPE_CASTER_NUMBER(uint8_t, unsigned long, PyLong_AsUnsignedLong, PyLong_FromUnsignedLong)
PYBIND11_TYPE_CASTER_NUMBER(int16_t, long, PyLong_AsLong, PyLong_FromLong)
PYBIND11_TYPE_CASTER_NUMBER(uint16_t, unsigned long, PyLong_AsUnsignedLong, PyLong_FromUnsignedLong)
PYBIND11_TYPE_CASTER_NUMBER(int32_t, long, PyLong_AsLong, PyLong_FromLong)
PYBIND11_TYPE_CASTER_NUMBER(uint32_t, unsigned long, PyLong_AsUnsignedLong, PyLong_FromUnsignedLong)
PYBIND11_TYPE_CASTER_NUMBER(int64_t, PY_LONG_LONG, PyLong_AsLongLong_Fixed, PyLong_FromLongLong)
PYBIND11_TYPE_CASTER_NUMBER(uint64_t, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong_Fixed, PyLong_FromUnsignedLongLong)
Wenzel Jakob's avatar
Wenzel Jakob committed
281
282

#if defined(__APPLE__) // size_t/ssize_t are separate types on Mac OS X
283
#if PY_MAJOR_VERSION >= 3
284
285
PYBIND11_TYPE_CASTER_NUMBER(ssize_t, Py_ssize_t, PyLong_AsSsize_t, PyLong_FromSsize_t)
PYBIND11_TYPE_CASTER_NUMBER(size_t, size_t, PyLong_AsSize_t, PyLong_FromSize_t)
286
#else
287
288
PYBIND11_TYPE_CASTER_NUMBER(ssize_t, PY_LONG_LONG, PyLong_AsLongLong_Fixed, PyLong_FromLongLong)
PYBIND11_TYPE_CASTER_NUMBER(size_t, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong_Fixed, PyLong_FromUnsignedLongLong)
289
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
290
291
#endif

292
293
PYBIND11_TYPE_CASTER_NUMBER(float, double, PyFloat_AsDouble, PyFloat_FromDouble)
PYBIND11_TYPE_CASTER_NUMBER(double, double, PyFloat_AsDouble, PyFloat_FromDouble)
Wenzel Jakob's avatar
Wenzel Jakob committed
294

295
template <> class type_caster<void_type> {
Wenzel Jakob's avatar
Wenzel Jakob committed
296
297
public:
    bool load(PyObject *, bool) { return true; }
298
    static PyObject *cast(void_type, return_value_policy /* policy */, PyObject * /* parent */) {
Wenzel Jakob's avatar
Wenzel Jakob committed
299
300
301
        Py_INCREF(Py_None);
        return Py_None;
    }
302
    PYBIND11_TYPE_CASTER(void_type, "None");
Wenzel Jakob's avatar
Wenzel Jakob committed
303
304
};

305
306
307
template <> class type_caster<void> : public type_caster<void_type> {
};

Wenzel Jakob's avatar
Wenzel Jakob committed
308
309
310
311
312
313
314
315
316
317
318
319
template <> class type_caster<bool> {
public:
    bool load(PyObject *src, bool) {
        if (src == Py_True) { value = true; return true; }
        else if (src == Py_False) { value = false; return true; }
        else return false;
    }
    static PyObject *cast(bool src, return_value_policy /* policy */, PyObject * /* parent */) {
        PyObject *result = src ? Py_True : Py_False;
        Py_INCREF(result);
        return result;
    }
320
    PYBIND11_TYPE_CASTER(bool, "bool");
Wenzel Jakob's avatar
Wenzel Jakob committed
321
322
323
324
325
};

template <> class type_caster<std::string> {
public:
    bool load(PyObject *src, bool) {
326
327
#if PY_MAJOR_VERSION < 3
        if (PyString_Check(src)) { value = PyString_AsString(src); return true; }
328
#endif
329
330
331
        object temp(PyUnicode_AsUTF8String(src), false);
        const char *ptr = nullptr;
        if (temp)
332
            ptr = PYBIND11_AS_STRING(temp.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
333
        if (!ptr) { PyErr_Clear(); return false; }
334
        value = ptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
335
336
337
338
339
        return true;
    }
    static PyObject *cast(const std::string &src, return_value_policy /* policy */, PyObject * /* parent */) {
        return PyUnicode_FromString(src.c_str());
    }
340
    PYBIND11_TYPE_CASTER(std::string, "str");
Wenzel Jakob's avatar
Wenzel Jakob committed
341
342
343
344
345
};

template <> class type_caster<char> {
public:
    bool load(PyObject *src, bool) {
346
347
#if PY_MAJOR_VERSION < 3
        if (PyString_Check(src)) { value = PyString_AsString(src); return true; }
348
#endif
349
350
351
        object temp(PyUnicode_AsUTF8String(src), false);
        const char *ptr = nullptr;
        if (temp)
352
            ptr = PYBIND11_AS_STRING(temp.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        if (!ptr) { PyErr_Clear(); return false; }
        value = ptr;
        return true;
    }

    static PyObject *cast(const char *src, return_value_policy /* policy */, PyObject * /* parent */) {
        return PyUnicode_FromString(src);
    }

    static PyObject *cast(char src, return_value_policy /* policy */, PyObject * /* parent */) {
        char str[2] = { src, '\0' };
        return PyUnicode_DecodeLatin1(str, 1, nullptr);
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
367
    static descr name() { return "str"; }
Wenzel Jakob's avatar
Wenzel Jakob committed
368

369
370
    operator char*() { return (char *) value.c_str(); }
    operator char() { if (value.length() > 0) return value[0]; else return '\0'; }
Wenzel Jakob's avatar
Wenzel Jakob committed
371
protected:
372
    std::string value;
Wenzel Jakob's avatar
Wenzel Jakob committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
};

template <typename T1, typename T2> class type_caster<std::pair<T1, T2>> {
    typedef std::pair<T1, T2> type;
public:
    bool load(PyObject *src, bool convert) {
        if (!PyTuple_Check(src) || PyTuple_Size(src) != 2)
            return false;
        if (!first.load(PyTuple_GetItem(src, 0), convert))
            return false;
        return second.load(PyTuple_GetItem(src, 1), convert);
    }

    static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
387
388
        PyObject *o1 = type_caster<typename decay<T1>::type>::cast(src.first, policy, parent);
        PyObject *o2 = type_caster<typename decay<T2>::type>::cast(src.second, policy, parent);
Wenzel Jakob's avatar
Wenzel Jakob committed
389
390
391
392
393
394
395
396
397
398
399
        if (!o1 || !o2) {
            Py_XDECREF(o1);
            Py_XDECREF(o2);
            return nullptr;
        }
        PyObject *tuple = PyTuple_New(2);
        PyTuple_SetItem(tuple, 0, o1);
        PyTuple_SetItem(tuple, 1, o2);
        return tuple;
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
400
    static descr name() {
401
        class descr result("(");
Wenzel Jakob's avatar
Wenzel Jakob committed
402
        result += std::move(type_caster<typename decay<T1>::type>::name());
403
        result += ", ";
Wenzel Jakob's avatar
Wenzel Jakob committed
404
        result += std::move(type_caster<typename decay<T2>::type>::name());
405
406
        result += ")";
        return result;
Wenzel Jakob's avatar
Wenzel Jakob committed
407
408
409
410
411
412
    }

    operator type() {
        return type(first, second);
    }
protected:
413
414
    type_caster<typename decay<T1>::type> first;
    type_caster<typename decay<T2>::type> second;
Wenzel Jakob's avatar
Wenzel Jakob committed
415
416
};

417
template <typename... Tuple> class type_caster<std::tuple<Tuple...>> {
Wenzel Jakob's avatar
Wenzel Jakob committed
418
419
420
421
422
    typedef std::tuple<Tuple...> type;
public:
    enum { size = sizeof...(Tuple) };

    bool load(PyObject *src, bool convert) {
Wenzel Jakob's avatar
Wenzel Jakob committed
423
        return load(src, convert, typename make_index_sequence<sizeof...(Tuple)>::type());
Wenzel Jakob's avatar
Wenzel Jakob committed
424
425
426
    }

    static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
Wenzel Jakob's avatar
Wenzel Jakob committed
427
        return cast(src, policy, parent, typename make_index_sequence<size>::type());
Wenzel Jakob's avatar
Wenzel Jakob committed
428
429
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
430
431
432
    static descr name(const char **keywords = nullptr, const char **values = nullptr) {
        std::array<class descr, size> names {{
            type_caster<typename decay<Tuple>::type>::name()...
Wenzel Jakob's avatar
Wenzel Jakob committed
433
        }};
434
435
436
437
        class descr result("(");
        for (int i=0; i<size; ++i) {
            if (keywords && keywords[i]) {
                result += keywords[i];
438
439
                result += " : ";
            }
Wenzel Jakob's avatar
Wenzel Jakob committed
440
            result += std::move(names[i]);
441
            if (values && values[i]) {
442
                result += " = ";
443
                result += values[i];
444
            }
445
            if (i+1 < size)
Wenzel Jakob's avatar
Wenzel Jakob committed
446
447
448
449
450
451
                result += ", ";
        }
        result += ")";
        return result;
    }

452
453
    template <typename ReturnValue, typename Func> typename std::enable_if<!std::is_void<ReturnValue>::value, ReturnValue>::type call(Func &&f) {
        return call<ReturnValue>(std::forward<Func>(f), typename make_index_sequence<sizeof...(Tuple)>::type());
Wenzel Jakob's avatar
Wenzel Jakob committed
454
455
    }

456
    template <typename ReturnValue, typename Func> typename std::enable_if<std::is_void<ReturnValue>::value, void_type>::type call(Func &&f) {
457
        call<ReturnValue>(std::forward<Func>(f), typename make_index_sequence<sizeof...(Tuple)>::type());
458
        return void_type();
Wenzel Jakob's avatar
Wenzel Jakob committed
459
460
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
461
    operator type() {
Wenzel Jakob's avatar
Wenzel Jakob committed
462
        return cast(typename make_index_sequence<sizeof...(Tuple)>::type());
Wenzel Jakob's avatar
Wenzel Jakob committed
463
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
464

Wenzel Jakob's avatar
Wenzel Jakob committed
465
protected:
466
    template <typename ReturnValue, typename Func, size_t ... Index> ReturnValue call(Func &&f, index_sequence<Index...>) {
Wenzel Jakob's avatar
Wenzel Jakob committed
467
468
469
470
        return f((Tuple) std::get<Index>(value)...);
    }

    template <size_t ... Index> type cast(index_sequence<Index...>) {
Wenzel Jakob's avatar
Wenzel Jakob committed
471
472
473
        return type((Tuple) std::get<Index>(value)...);
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
474
    template <size_t ... Indices> bool load(PyObject *src, bool convert, index_sequence<Indices...>) {
Wenzel Jakob's avatar
Wenzel Jakob committed
475
476
477
478
479
        if (!PyTuple_Check(src))
            return false;
        if (PyTuple_Size(src) != size)
            return false;
        std::array<bool, size> results {{
480
            (PyTuple_GET_ITEM(src, Indices) != nullptr ? std::get<Indices>(value).load(PyTuple_GET_ITEM(src, Indices), convert) : false)...
Wenzel Jakob's avatar
Wenzel Jakob committed
481
        }};
482
        (void) convert; /* avoid a warning when the tuple is empty */
Wenzel Jakob's avatar
Wenzel Jakob committed
483
484
485
486
487
488
489
        for (bool r : results)
            if (!r)
                return false;
        return true;
    }

    /* Implementation: Convert a C++ tuple into a Python tuple */
Wenzel Jakob's avatar
Wenzel Jakob committed
490
    template <size_t ... Indices> static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent, index_sequence<Indices...>) {
Wenzel Jakob's avatar
Wenzel Jakob committed
491
        std::array<PyObject *, size> results {{
492
            type_caster<typename decay<Tuple>::type>::cast(std::get<Indices>(src), policy, parent)...
Wenzel Jakob's avatar
Wenzel Jakob committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        }};
        bool success = true;
        for (auto result : results)
            if (result == nullptr)
                success = false;
        if (success) {
            PyObject *tuple = PyTuple_New(size);
            int counter = 0;
            for (auto result : results)
                PyTuple_SetItem(tuple, counter++, result);
            return tuple;
        } else {
            for (auto result : results) {
                Py_XDECREF(result);
            }
            return nullptr;
        }
    }

protected:
513
    std::tuple<type_caster<typename decay<Tuple>::type>...> value;
Wenzel Jakob's avatar
Wenzel Jakob committed
514
515
516
517
518
519
520
521
522
};

/// Type caster for holder types like std::shared_ptr, etc.
template <typename type, typename holder_type> class type_caster_holder : public type_caster<type> {
public:
    typedef type_caster<type> parent;
    bool load(PyObject *src, bool convert) {
        if (!parent::load(src, convert))
            return false;
523
        holder = holder_type((type *) parent::value);
Wenzel Jakob's avatar
Wenzel Jakob committed
524
525
526
527
528
529
530
531
532
533
        return true;
    }
    explicit operator type*() { return this->value; }
    explicit operator type&() { return *(this->value); }
    explicit operator holder_type&() { return holder; }
    explicit operator holder_type*() { return &holder; }
protected:
    holder_type holder;
};

534
#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type) \
535
536
537
538
539
    namespace pybind11 { namespace detail { \
    template <typename type> class type_caster<holder_type> \
        : public type_caster_holder<type, holder_type> { }; \
    }}

Wenzel Jakob's avatar
Wenzel Jakob committed
540
541
542
543
544
545
546
547
548
549
template <> class type_caster<handle> {
public:
    bool load(PyObject *src) {
        value = handle(src);
        return true;
    }
    static PyObject *cast(const handle &src, return_value_policy /* policy */, PyObject * /* parent */) {
        src.inc_ref();
        return (PyObject *) src.ptr();
    }
550
    PYBIND11_TYPE_CASTER(handle, "handle");
Wenzel Jakob's avatar
Wenzel Jakob committed
551
552
};

553
#define PYBIND11_TYPE_CASTER_PYTYPE(name) \
Wenzel Jakob's avatar
Wenzel Jakob committed
554
555
556
557
558
559
    template <> class type_caster<name> { \
    public: \
        bool load(PyObject *src, bool) { value = name(src, true); return true; } \
        static PyObject *cast(const name &src, return_value_policy /* policy */, PyObject * /* parent */) { \
            src.inc_ref(); return (PyObject *) src.ptr(); \
        } \
560
        PYBIND11_TYPE_CASTER(name, #name); \
Wenzel Jakob's avatar
Wenzel Jakob committed
561
562
    };

563
564
565
566
567
PYBIND11_TYPE_CASTER_PYTYPE(object)  PYBIND11_TYPE_CASTER_PYTYPE(buffer)
PYBIND11_TYPE_CASTER_PYTYPE(capsule) PYBIND11_TYPE_CASTER_PYTYPE(dict)
PYBIND11_TYPE_CASTER_PYTYPE(float_)  PYBIND11_TYPE_CASTER_PYTYPE(int_)
PYBIND11_TYPE_CASTER_PYTYPE(list)    PYBIND11_TYPE_CASTER_PYTYPE(slice)
PYBIND11_TYPE_CASTER_PYTYPE(tuple)   PYBIND11_TYPE_CASTER_PYTYPE(function)
Wenzel Jakob's avatar
Wenzel Jakob committed
568
569
570
571

NAMESPACE_END(detail)

template <typename T> inline T cast(PyObject *object) {
Wenzel Jakob's avatar
Wenzel Jakob committed
572
    detail::type_caster<typename detail::decay<T>::type> conv;
Wenzel Jakob's avatar
Wenzel Jakob committed
573
574
575
576
577
578
579
580
    if (!conv.load(object, true))
        throw cast_error("Unable to cast Python object to C++ type");
    return conv;
}

template <typename T> inline object cast(const T &value, return_value_policy policy = return_value_policy::automatic, PyObject *parent = nullptr) {
    if (policy == return_value_policy::automatic)
        policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
Wenzel Jakob's avatar
Wenzel Jakob committed
581
    return object(detail::type_caster<typename detail::decay<T>::type>::cast(value, policy, parent), false);
Wenzel Jakob's avatar
Wenzel Jakob committed
582
583
}

584
template <typename T> inline T handle::cast() { return pybind11::cast<T>(m_ptr); }
585
template <> inline void handle::cast() { return; }
Wenzel Jakob's avatar
Wenzel Jakob committed
586

587
template <typename... Args> inline object handle::call(Args&&... args_) {
Wenzel Jakob's avatar
Wenzel Jakob committed
588
589
    const size_t size = sizeof...(Args);
    std::array<PyObject *, size> args{
Wenzel Jakob's avatar
Wenzel Jakob committed
590
        { detail::type_caster<typename detail::decay<Args>::type>::cast(
Wenzel Jakob's avatar
Wenzel Jakob committed
591
            std::forward<Args>(args_), return_value_policy::reference, nullptr)... }
Wenzel Jakob's avatar
Wenzel Jakob committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
    };
    bool fail = false;
    for (auto result : args)
        if (result == nullptr)
            fail = true;
    if (fail) {
        for (auto result : args) {
            Py_XDECREF(result);
        }
        throw cast_error("handle::call(): unable to convert input arguments to Python objects");
    }
    PyObject *tuple = PyTuple_New(size);
    int counter = 0;
    for (auto result : args)
        PyTuple_SetItem(tuple, counter++, result);
    PyObject *result = PyObject_CallObject(m_ptr, tuple);
    Py_DECREF(tuple);
609
610
    if (result == nullptr && PyErr_Occurred())
        throw error_already_set();
Wenzel Jakob's avatar
Wenzel Jakob committed
611
612
613
    return object(result, false);
}

614
NAMESPACE_END(pybind11)