cast.h revision 11986
113223Sodanrc@yahoo.com.br/*
213223Sodanrc@yahoo.com.br    pybind11/cast.h: Partial template specializations to cast between
313223Sodanrc@yahoo.com.br    C++ and Python types
413223Sodanrc@yahoo.com.br
513223Sodanrc@yahoo.com.br    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
613223Sodanrc@yahoo.com.br
713223Sodanrc@yahoo.com.br    All rights reserved. Use of this source code is governed by a
813223Sodanrc@yahoo.com.br    BSD-style license that can be found in the LICENSE file.
913223Sodanrc@yahoo.com.br*/
1013223Sodanrc@yahoo.com.br
1113223Sodanrc@yahoo.com.br#pragma once
1213223Sodanrc@yahoo.com.br
1313223Sodanrc@yahoo.com.br#include "pytypes.h"
1413223Sodanrc@yahoo.com.br#include "typeid.h"
1513223Sodanrc@yahoo.com.br#include "descr.h"
1613223Sodanrc@yahoo.com.br#include <array>
1713223Sodanrc@yahoo.com.br#include <limits>
1813223Sodanrc@yahoo.com.br
1913223Sodanrc@yahoo.com.brNAMESPACE_BEGIN(pybind11)
2013223Sodanrc@yahoo.com.brNAMESPACE_BEGIN(detail)
2113223Sodanrc@yahoo.com.br
2213223Sodanrc@yahoo.com.br/// Additional type information which does not fit into the PyTypeObject
2313223Sodanrc@yahoo.com.brstruct type_info {
2413223Sodanrc@yahoo.com.br    PyTypeObject *type;
2513223Sodanrc@yahoo.com.br    size_t type_size;
2613223Sodanrc@yahoo.com.br    void (*init_holder)(PyObject *, const void *);
2713223Sodanrc@yahoo.com.br    std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
2813223Sodanrc@yahoo.com.br    std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
2913223Sodanrc@yahoo.com.br    std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
3013223Sodanrc@yahoo.com.br    buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
3113223Sodanrc@yahoo.com.br    void *get_buffer_data = nullptr;
3213223Sodanrc@yahoo.com.br    /** A simple type never occurs as a (direct or indirect) parent
3313223Sodanrc@yahoo.com.br     * of a class that makes use of multiple inheritance */
3413223Sodanrc@yahoo.com.br    bool simple_type = true;
3513223Sodanrc@yahoo.com.br};
3613223Sodanrc@yahoo.com.br
3713223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline internals &get_internals() {
3813223Sodanrc@yahoo.com.br    static internals *internals_ptr = nullptr;
3913223Sodanrc@yahoo.com.br    if (internals_ptr)
4013223Sodanrc@yahoo.com.br        return *internals_ptr;
4113223Sodanrc@yahoo.com.br    handle builtins(PyEval_GetBuiltins());
4213223Sodanrc@yahoo.com.br    const char *id = PYBIND11_INTERNALS_ID;
4313223Sodanrc@yahoo.com.br    if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
4413223Sodanrc@yahoo.com.br        internals_ptr = capsule(builtins[id]);
4513223Sodanrc@yahoo.com.br    } else {
4613223Sodanrc@yahoo.com.br        internals_ptr = new internals();
4713223Sodanrc@yahoo.com.br        #if defined(WITH_THREAD)
4813223Sodanrc@yahoo.com.br            PyEval_InitThreads();
4913223Sodanrc@yahoo.com.br            PyThreadState *tstate = PyThreadState_Get();
5013223Sodanrc@yahoo.com.br            internals_ptr->tstate = PyThread_create_key();
5113223Sodanrc@yahoo.com.br            PyThread_set_key_value(internals_ptr->tstate, tstate);
5213223Sodanrc@yahoo.com.br            internals_ptr->istate = tstate->interp;
5313223Sodanrc@yahoo.com.br        #endif
5413223Sodanrc@yahoo.com.br        builtins[id] = capsule(internals_ptr);
5513223Sodanrc@yahoo.com.br        internals_ptr->registered_exception_translators.push_front(
5613223Sodanrc@yahoo.com.br            [](std::exception_ptr p) -> void {
5713223Sodanrc@yahoo.com.br                try {
5813223Sodanrc@yahoo.com.br                    if (p) std::rethrow_exception(p);
5913223Sodanrc@yahoo.com.br                } catch (error_already_set &e)           { e.restore();                                    return;
6013223Sodanrc@yahoo.com.br                } catch (const builtin_exception &e)     { e.set_error();                                  return;
6113223Sodanrc@yahoo.com.br                } catch (const std::bad_alloc &e)        { PyErr_SetString(PyExc_MemoryError,   e.what()); return;
6213223Sodanrc@yahoo.com.br                } catch (const std::domain_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
6313223Sodanrc@yahoo.com.br                } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError,    e.what()); return;
6413223Sodanrc@yahoo.com.br                } catch (const std::length_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
6513223Sodanrc@yahoo.com.br                } catch (const std::out_of_range &e)     { PyErr_SetString(PyExc_IndexError,    e.what()); return;
6613223Sodanrc@yahoo.com.br                } catch (const std::range_error &e)      { PyErr_SetString(PyExc_ValueError,    e.what()); return;
6713223Sodanrc@yahoo.com.br                } catch (const std::exception &e)        { PyErr_SetString(PyExc_RuntimeError,  e.what()); return;
6813223Sodanrc@yahoo.com.br                } catch (...) {
6913223Sodanrc@yahoo.com.br                    PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
7013223Sodanrc@yahoo.com.br                    return;
7113223Sodanrc@yahoo.com.br                }
7213223Sodanrc@yahoo.com.br            }
7313223Sodanrc@yahoo.com.br        );
7413223Sodanrc@yahoo.com.br    }
7513223Sodanrc@yahoo.com.br    return *internals_ptr;
7613223Sodanrc@yahoo.com.br}
7713223Sodanrc@yahoo.com.br
7813223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) {
7913940Sodanrc@yahoo.com.br    auto const &type_dict = get_internals().registered_types_py;
8013940Sodanrc@yahoo.com.br    do {
8113223Sodanrc@yahoo.com.br        auto it = type_dict.find(type);
8213223Sodanrc@yahoo.com.br        if (it != type_dict.end())
8313223Sodanrc@yahoo.com.br            return (detail::type_info *) it->second;
8413223Sodanrc@yahoo.com.br        type = type->tp_base;
8513223Sodanrc@yahoo.com.br        if (!type)
8613223Sodanrc@yahoo.com.br            return nullptr;
8713223Sodanrc@yahoo.com.br    } while (true);
8813223Sodanrc@yahoo.com.br}
8913223Sodanrc@yahoo.com.br
9013223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_info &tp,
9113223Sodanrc@yahoo.com.br                                                          bool throw_if_missing = false) {
9213223Sodanrc@yahoo.com.br    auto &types = get_internals().registered_types_cpp;
9313223Sodanrc@yahoo.com.br
9413223Sodanrc@yahoo.com.br    auto it = types.find(std::type_index(tp));
9513223Sodanrc@yahoo.com.br    if (it != types.end())
9613223Sodanrc@yahoo.com.br        return (detail::type_info *) it->second;
9713223Sodanrc@yahoo.com.br    if (throw_if_missing) {
9813223Sodanrc@yahoo.com.br        std::string tname = tp.name();
9913223Sodanrc@yahoo.com.br        detail::clean_type_id(tname);
10013223Sodanrc@yahoo.com.br        pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \"" + tname + "\"");
10113223Sodanrc@yahoo.com.br    }
10213223Sodanrc@yahoo.com.br    return nullptr;
10313223Sodanrc@yahoo.com.br}
10413223Sodanrc@yahoo.com.br
10513223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
10613223Sodanrc@yahoo.com.br    detail::type_info *type_info = get_type_info(tp, throw_if_missing);
10713223Sodanrc@yahoo.com.br    return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
10813223Sodanrc@yahoo.com.br}
10913223Sodanrc@yahoo.com.br
11013477Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline bool isinstance_generic(handle obj, const std::type_info &tp) {
11113477Sodanrc@yahoo.com.br    const auto type = detail::get_type_handle(tp, false);
11213477Sodanrc@yahoo.com.br    if (!type)
11313477Sodanrc@yahoo.com.br        return false;
11413223Sodanrc@yahoo.com.br
11513223Sodanrc@yahoo.com.br    const auto result = PyObject_IsInstance(obj.ptr(), type.ptr());
11613223Sodanrc@yahoo.com.br    if (result == -1)
11713223Sodanrc@yahoo.com.br        throw error_already_set();
11813223Sodanrc@yahoo.com.br    return result != 0;
11913223Sodanrc@yahoo.com.br}
12013223Sodanrc@yahoo.com.br
12113223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline std::string error_string() {
12213477Sodanrc@yahoo.com.br    if (!PyErr_Occurred()) {
12313477Sodanrc@yahoo.com.br        PyErr_SetString(PyExc_RuntimeError, "Unknown internal error occurred");
12413477Sodanrc@yahoo.com.br        return "Unknown internal error occurred";
12513477Sodanrc@yahoo.com.br    }
12613223Sodanrc@yahoo.com.br
12713223Sodanrc@yahoo.com.br    error_scope scope; // Preserve error state
12813223Sodanrc@yahoo.com.br
12913223Sodanrc@yahoo.com.br    std::string errorString;
13013223Sodanrc@yahoo.com.br    if (scope.type) {
13113223Sodanrc@yahoo.com.br        errorString += handle(scope.type).attr("__name__").cast<std::string>();
13213223Sodanrc@yahoo.com.br        errorString += ": ";
13313223Sodanrc@yahoo.com.br    }
13413223Sodanrc@yahoo.com.br    if (scope.value)
13513223Sodanrc@yahoo.com.br        errorString += (std::string) str(scope.value);
13613223Sodanrc@yahoo.com.br
13713223Sodanrc@yahoo.com.br    PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
13813223Sodanrc@yahoo.com.br
13913223Sodanrc@yahoo.com.br#if PY_MAJOR_VERSION >= 3
14013223Sodanrc@yahoo.com.br    if (scope.trace != nullptr)
14113223Sodanrc@yahoo.com.br        PyException_SetTraceback(scope.value, scope.trace);
14213223Sodanrc@yahoo.com.br#endif
14313223Sodanrc@yahoo.com.br
14413223Sodanrc@yahoo.com.br    if (scope.trace) {
14513223Sodanrc@yahoo.com.br        PyTracebackObject *trace = (PyTracebackObject *) scope.trace;
14613223Sodanrc@yahoo.com.br
14713223Sodanrc@yahoo.com.br        /* Get the deepest trace possible */
14813223Sodanrc@yahoo.com.br        while (trace->tb_next)
14913223Sodanrc@yahoo.com.br            trace = trace->tb_next;
15013223Sodanrc@yahoo.com.br
15113223Sodanrc@yahoo.com.br        PyFrameObject *frame = trace->tb_frame;
15213223Sodanrc@yahoo.com.br        errorString += "\n\nAt:\n";
15313223Sodanrc@yahoo.com.br        while (frame) {
15413223Sodanrc@yahoo.com.br            int lineno = PyFrame_GetLineNumber(frame);
15513223Sodanrc@yahoo.com.br            errorString +=
15613223Sodanrc@yahoo.com.br                "  " + handle(frame->f_code->co_filename).cast<std::string>() +
15713223Sodanrc@yahoo.com.br                "(" + std::to_string(lineno) + "): " +
15813223Sodanrc@yahoo.com.br                handle(frame->f_code->co_name).cast<std::string>() + "\n";
15913223Sodanrc@yahoo.com.br            frame = frame->f_back;
16013223Sodanrc@yahoo.com.br        }
16113223Sodanrc@yahoo.com.br        trace = trace->tb_next;
16213223Sodanrc@yahoo.com.br    }
16313223Sodanrc@yahoo.com.br
16413223Sodanrc@yahoo.com.br    return errorString;
16513223Sodanrc@yahoo.com.br}
16613223Sodanrc@yahoo.com.br
16713223Sodanrc@yahoo.com.brPYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail::type_info *type ) {
16813223Sodanrc@yahoo.com.br    auto &instances = get_internals().registered_instances;
16913223Sodanrc@yahoo.com.br    auto range = instances.equal_range(ptr);
17013223Sodanrc@yahoo.com.br    for (auto it = range.first; it != range.second; ++it) {
17113477Sodanrc@yahoo.com.br        auto instance_type = detail::get_type_info(Py_TYPE(it->second));
17213223Sodanrc@yahoo.com.br        if (instance_type && instance_type == type)
17313223Sodanrc@yahoo.com.br            return handle((PyObject *) it->second);
17413223Sodanrc@yahoo.com.br    }
17513223Sodanrc@yahoo.com.br    return handle();
17613223Sodanrc@yahoo.com.br}
17713223Sodanrc@yahoo.com.br
17813223Sodanrc@yahoo.com.brinline PyThreadState *get_thread_state_unchecked() {
17913223Sodanrc@yahoo.com.br#if   PY_VERSION_HEX < 0x03000000
18013223Sodanrc@yahoo.com.br    return _PyThreadState_Current;
18113223Sodanrc@yahoo.com.br#elif PY_VERSION_HEX < 0x03050000
18213223Sodanrc@yahoo.com.br    return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current);
18313223Sodanrc@yahoo.com.br#elif PY_VERSION_HEX < 0x03050200
18413223Sodanrc@yahoo.com.br    return (PyThreadState*) _PyThreadState_Current.value;
18513223Sodanrc@yahoo.com.br#else
18613223Sodanrc@yahoo.com.br    return _PyThreadState_UncheckedGet();
18713223Sodanrc@yahoo.com.br#endif
18813223Sodanrc@yahoo.com.br}
18913223Sodanrc@yahoo.com.br
19013223Sodanrc@yahoo.com.br// Forward declaration
19113223Sodanrc@yahoo.com.brinline void keep_alive_impl(handle nurse, handle patient);
19213223Sodanrc@yahoo.com.br
19313223Sodanrc@yahoo.com.brclass type_caster_generic {
19413223Sodanrc@yahoo.com.brpublic:
19513223Sodanrc@yahoo.com.br    PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
19613223Sodanrc@yahoo.com.br     : typeinfo(get_type_info(type_info)) { }
19713223Sodanrc@yahoo.com.br
19813223Sodanrc@yahoo.com.br    PYBIND11_NOINLINE bool load(handle src, bool convert) {
19913223Sodanrc@yahoo.com.br        if (!src)
20013223Sodanrc@yahoo.com.br            return false;
20113223Sodanrc@yahoo.com.br        return load(src, convert, Py_TYPE(src.ptr()));
20213223Sodanrc@yahoo.com.br    }
20313223Sodanrc@yahoo.com.br
20413223Sodanrc@yahoo.com.br    bool load(handle src, bool convert, PyTypeObject *tobj) {
20513223Sodanrc@yahoo.com.br        if (!src || !typeinfo)
20613223Sodanrc@yahoo.com.br            return false;
20713223Sodanrc@yahoo.com.br        if (src.is_none()) {
20813223Sodanrc@yahoo.com.br            value = nullptr;
20913223Sodanrc@yahoo.com.br            return true;
21013223Sodanrc@yahoo.com.br        }
21113223Sodanrc@yahoo.com.br
21213223Sodanrc@yahoo.com.br        if (typeinfo->simple_type) { /* Case 1: no multiple inheritance etc. involved */
21313223Sodanrc@yahoo.com.br            /* Check if we can safely perform a reinterpret-style cast */
21413223Sodanrc@yahoo.com.br            if (PyType_IsSubtype(tobj, typeinfo->type)) {
21513223Sodanrc@yahoo.com.br                value = reinterpret_cast<instance<void> *>(src.ptr())->value;
21613223Sodanrc@yahoo.com.br                return true;
21713223Sodanrc@yahoo.com.br            }
21813223Sodanrc@yahoo.com.br        } else { /* Case 2: multiple inheritance */
21913223Sodanrc@yahoo.com.br            /* Check if we can safely perform a reinterpret-style cast */
22013223Sodanrc@yahoo.com.br            if (tobj == typeinfo->type) {
22113223Sodanrc@yahoo.com.br                value = reinterpret_cast<instance<void> *>(src.ptr())->value;
22213223Sodanrc@yahoo.com.br                return true;
22313223Sodanrc@yahoo.com.br            }
22413223Sodanrc@yahoo.com.br
22513223Sodanrc@yahoo.com.br            /* If this is a python class, also check the parents recursively */
22613223Sodanrc@yahoo.com.br            auto const &type_dict = get_internals().registered_types_py;
22713223Sodanrc@yahoo.com.br            bool new_style_class = PyType_Check(tobj);
22813223Sodanrc@yahoo.com.br            if (type_dict.find(tobj) == type_dict.end() && new_style_class && tobj->tp_bases) {
22913223Sodanrc@yahoo.com.br                auto parents = reinterpret_borrow<tuple>(tobj->tp_bases);
23013223Sodanrc@yahoo.com.br                for (handle parent : parents) {
23113223Sodanrc@yahoo.com.br                    bool result = load(src, convert, (PyTypeObject *) parent.ptr());
23213223Sodanrc@yahoo.com.br                    if (result)
23313223Sodanrc@yahoo.com.br                        return true;
23413223Sodanrc@yahoo.com.br                }
23513223Sodanrc@yahoo.com.br            }
23613223Sodanrc@yahoo.com.br
23713223Sodanrc@yahoo.com.br            /* Try implicit casts */
23813223Sodanrc@yahoo.com.br            for (auto &cast : typeinfo->implicit_casts) {
23913223Sodanrc@yahoo.com.br                type_caster_generic sub_caster(*cast.first);
24013223Sodanrc@yahoo.com.br                if (sub_caster.load(src, convert)) {
24113223Sodanrc@yahoo.com.br                    value = cast.second(sub_caster.value);
24213223Sodanrc@yahoo.com.br                    return true;
24313223Sodanrc@yahoo.com.br                }
24413223Sodanrc@yahoo.com.br            }
24513223Sodanrc@yahoo.com.br        }
24613223Sodanrc@yahoo.com.br
24713223Sodanrc@yahoo.com.br        /* Perform an implicit conversion */
24813223Sodanrc@yahoo.com.br        if (convert) {
24913223Sodanrc@yahoo.com.br            for (auto &converter : typeinfo->implicit_conversions) {
25013223Sodanrc@yahoo.com.br                temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
25113223Sodanrc@yahoo.com.br                if (load(temp, false))
25213223Sodanrc@yahoo.com.br                    return true;
25313223Sodanrc@yahoo.com.br            }
25413445Sodanrc@yahoo.com.br            for (auto &converter : *typeinfo->direct_conversions) {
25513445Sodanrc@yahoo.com.br                if (converter(src.ptr(), value))
25613445Sodanrc@yahoo.com.br                    return true;
25713445Sodanrc@yahoo.com.br            }
25813445Sodanrc@yahoo.com.br        }
25913445Sodanrc@yahoo.com.br        return false;
26013445Sodanrc@yahoo.com.br    }
26113445Sodanrc@yahoo.com.br
26213445Sodanrc@yahoo.com.br    PYBIND11_NOINLINE static handle cast(const void *_src, return_value_policy policy, handle parent,
26313445Sodanrc@yahoo.com.br                                         const std::type_info *type_info,
26413445Sodanrc@yahoo.com.br                                         const std::type_info *type_info_backup,
26513445Sodanrc@yahoo.com.br                                         void *(*copy_constructor)(const void *),
26613445Sodanrc@yahoo.com.br                                         void *(*move_constructor)(const void *),
26713445Sodanrc@yahoo.com.br                                         const void *existing_holder = nullptr) {
26813445Sodanrc@yahoo.com.br        void *src = const_cast<void *>(_src);
26913445Sodanrc@yahoo.com.br        if (src == nullptr)
27013445Sodanrc@yahoo.com.br            return none().inc_ref();
27113477Sodanrc@yahoo.com.br
27213477Sodanrc@yahoo.com.br        auto &internals = get_internals();
27313477Sodanrc@yahoo.com.br
27413477Sodanrc@yahoo.com.br        auto it = internals.registered_types_cpp.find(std::type_index(*type_info));
27513477Sodanrc@yahoo.com.br        if (it == internals.registered_types_cpp.end()) {
27613477Sodanrc@yahoo.com.br            type_info = type_info_backup;
27713477Sodanrc@yahoo.com.br            it = internals.registered_types_cpp.find(std::type_index(*type_info));
27813477Sodanrc@yahoo.com.br        }
27913477Sodanrc@yahoo.com.br
28013477Sodanrc@yahoo.com.br        if (it == internals.registered_types_cpp.end()) {
28113477Sodanrc@yahoo.com.br            std::string tname = type_info->name();
28213477Sodanrc@yahoo.com.br            detail::clean_type_id(tname);
28313477Sodanrc@yahoo.com.br            std::string msg = "Unregistered type : " + tname;
28413477Sodanrc@yahoo.com.br            PyErr_SetString(PyExc_TypeError, msg.c_str());
28513477Sodanrc@yahoo.com.br            return handle();
28613477Sodanrc@yahoo.com.br        }
28713477Sodanrc@yahoo.com.br
28813477Sodanrc@yahoo.com.br        auto tinfo = (const detail::type_info *) it->second;
28913477Sodanrc@yahoo.com.br
29013477Sodanrc@yahoo.com.br        auto it_instances = internals.registered_instances.equal_range(src);
29113477Sodanrc@yahoo.com.br        for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
29213477Sodanrc@yahoo.com.br            auto instance_type = detail::get_type_info(Py_TYPE(it_i->second));
29313477Sodanrc@yahoo.com.br            if (instance_type && instance_type == tinfo)
29413223Sodanrc@yahoo.com.br                return handle((PyObject *) it_i->second).inc_ref();
29513223Sodanrc@yahoo.com.br        }
29613223Sodanrc@yahoo.com.br
29713445Sodanrc@yahoo.com.br        auto inst = reinterpret_steal<object>(PyType_GenericAlloc(tinfo->type, 0));
29813223Sodanrc@yahoo.com.br
29913223Sodanrc@yahoo.com.br        auto wrapper = (instance<void> *) inst.ptr();
30013223Sodanrc@yahoo.com.br
30113223Sodanrc@yahoo.com.br        wrapper->value = nullptr;
30213223Sodanrc@yahoo.com.br        wrapper->owned = false;
30313223Sodanrc@yahoo.com.br
30413223Sodanrc@yahoo.com.br        switch (policy) {
30513223Sodanrc@yahoo.com.br            case return_value_policy::automatic:
30613223Sodanrc@yahoo.com.br            case return_value_policy::take_ownership:
30713223Sodanrc@yahoo.com.br                wrapper->value = src;
30813223Sodanrc@yahoo.com.br                wrapper->owned = true;
30913223Sodanrc@yahoo.com.br                break;
31013223Sodanrc@yahoo.com.br
31113223Sodanrc@yahoo.com.br            case return_value_policy::automatic_reference:
31213223Sodanrc@yahoo.com.br            case return_value_policy::reference:
31313223Sodanrc@yahoo.com.br                wrapper->value = src;
31413223Sodanrc@yahoo.com.br                wrapper->owned = false;
31513223Sodanrc@yahoo.com.br                break;
31613223Sodanrc@yahoo.com.br
31713223Sodanrc@yahoo.com.br            case return_value_policy::copy:
31813223Sodanrc@yahoo.com.br                if (copy_constructor)
31913223Sodanrc@yahoo.com.br                    wrapper->value = copy_constructor(src);
32013223Sodanrc@yahoo.com.br                else
32113223Sodanrc@yahoo.com.br                    throw cast_error("return_value_policy = copy, but the "
32213223Sodanrc@yahoo.com.br                                     "object is non-copyable!");
32313223Sodanrc@yahoo.com.br                wrapper->owned = true;
32413223Sodanrc@yahoo.com.br                break;
32513223Sodanrc@yahoo.com.br
32613223Sodanrc@yahoo.com.br            case return_value_policy::move:
32713223Sodanrc@yahoo.com.br                if (move_constructor)
32813223Sodanrc@yahoo.com.br                    wrapper->value = move_constructor(src);
32913223Sodanrc@yahoo.com.br                else if (copy_constructor)
33013223Sodanrc@yahoo.com.br                    wrapper->value = copy_constructor(src);
33113223Sodanrc@yahoo.com.br                else
33213223Sodanrc@yahoo.com.br                    throw cast_error("return_value_policy = move, but the "
33313223Sodanrc@yahoo.com.br                                     "object is neither movable nor copyable!");
33413223Sodanrc@yahoo.com.br                wrapper->owned = true;
33513223Sodanrc@yahoo.com.br                break;
33613223Sodanrc@yahoo.com.br
33713223Sodanrc@yahoo.com.br            case return_value_policy::reference_internal:
33813223Sodanrc@yahoo.com.br                wrapper->value = src;
33913223Sodanrc@yahoo.com.br                wrapper->owned = false;
34013223Sodanrc@yahoo.com.br                detail::keep_alive_impl(inst, parent);
34113223Sodanrc@yahoo.com.br                break;
34213223Sodanrc@yahoo.com.br
34313223Sodanrc@yahoo.com.br            default:
34413223Sodanrc@yahoo.com.br                throw cast_error("unhandled return_value_policy: should not happen!");
34513223Sodanrc@yahoo.com.br        }
34613223Sodanrc@yahoo.com.br
34713223Sodanrc@yahoo.com.br        tinfo->init_holder(inst.ptr(), existing_holder);
34813223Sodanrc@yahoo.com.br
34913223Sodanrc@yahoo.com.br        internals.registered_instances.emplace(wrapper->value, inst.ptr());
35013223Sodanrc@yahoo.com.br
35113223Sodanrc@yahoo.com.br        return inst.release();
35213223Sodanrc@yahoo.com.br    }
35313223Sodanrc@yahoo.com.br
35413223Sodanrc@yahoo.com.brprotected:
35513223Sodanrc@yahoo.com.br    const type_info *typeinfo = nullptr;
35613223Sodanrc@yahoo.com.br    void *value = nullptr;
35713223Sodanrc@yahoo.com.br    object temp;
35813223Sodanrc@yahoo.com.br};
35913223Sodanrc@yahoo.com.br
36013223Sodanrc@yahoo.com.br/* Determine suitable casting operator */
36113223Sodanrc@yahoo.com.brtemplate <typename T>
36213223Sodanrc@yahoo.com.brusing cast_op_type = typename std::conditional<std::is_pointer<typename std::remove_reference<T>::type>::value,
36313223Sodanrc@yahoo.com.br    typename std::add_pointer<intrinsic_t<T>>::type,
36413223Sodanrc@yahoo.com.br    typename std::add_lvalue_reference<intrinsic_t<T>>::type>::type;
36513223Sodanrc@yahoo.com.br
36613223Sodanrc@yahoo.com.br// std::is_copy_constructible isn't quite enough: it lets std::vector<T> (and similar) through when
36713223Sodanrc@yahoo.com.br// T is non-copyable, but code containing such a copy constructor fails to actually compile.
36813223Sodanrc@yahoo.com.brtemplate <typename T, typename SFINAE = void> struct is_copy_constructible : std::is_copy_constructible<T> {};
36913223Sodanrc@yahoo.com.br
37013223Sodanrc@yahoo.com.br// Specialization for types that appear to be copy constructible but also look like stl containers
37113223Sodanrc@yahoo.com.br// (we specifically check for: has `value_type` and `reference` with `reference = value_type&`): if
37213223Sodanrc@yahoo.com.br// so, copy constructability depends on whether the value_type is copy constructible.
37313223Sodanrc@yahoo.com.brtemplate <typename Container> struct is_copy_constructible<Container, enable_if_t<
37413223Sodanrc@yahoo.com.br        std::is_copy_constructible<Container>::value &&
37513223Sodanrc@yahoo.com.br        std::is_same<typename Container::value_type &, typename Container::reference>::value
37613223Sodanrc@yahoo.com.br    >> : std::is_copy_constructible<typename Container::value_type> {};
37713223Sodanrc@yahoo.com.br
37813223Sodanrc@yahoo.com.br/// Generic type caster for objects stored on the heap
37913223Sodanrc@yahoo.com.brtemplate <typename type> class type_caster_base : public type_caster_generic {
38013223Sodanrc@yahoo.com.br    using itype = intrinsic_t<type>;
38113223Sodanrc@yahoo.com.brpublic:
38213223Sodanrc@yahoo.com.br    static PYBIND11_DESCR name() { return type_descr(_<type>()); }
38313223Sodanrc@yahoo.com.br
38413223Sodanrc@yahoo.com.br    type_caster_base() : type_caster_base(typeid(type)) { }
38513223Sodanrc@yahoo.com.br    explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
38613223Sodanrc@yahoo.com.br
38713223Sodanrc@yahoo.com.br    static handle cast(const itype &src, return_value_policy policy, handle parent) {
38813223Sodanrc@yahoo.com.br        if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
38913223Sodanrc@yahoo.com.br            policy = return_value_policy::copy;
39013223Sodanrc@yahoo.com.br        return cast(&src, policy, parent);
39113223Sodanrc@yahoo.com.br    }
39213223Sodanrc@yahoo.com.br
39313223Sodanrc@yahoo.com.br    static handle cast(itype &&src, return_value_policy, handle parent) {
39413223Sodanrc@yahoo.com.br        return cast(&src, return_value_policy::move, parent);
39513223Sodanrc@yahoo.com.br    }
39613223Sodanrc@yahoo.com.br
39713223Sodanrc@yahoo.com.br    static handle cast(const itype *src, return_value_policy policy, handle parent) {
39813223Sodanrc@yahoo.com.br        return type_caster_generic::cast(
39913223Sodanrc@yahoo.com.br            src, policy, parent, src ? &typeid(*src) : nullptr, &typeid(type),
40013223Sodanrc@yahoo.com.br            make_copy_constructor(src), make_move_constructor(src));
40113223Sodanrc@yahoo.com.br    }
40213223Sodanrc@yahoo.com.br
40313223Sodanrc@yahoo.com.br    template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
40413223Sodanrc@yahoo.com.br
40513223Sodanrc@yahoo.com.br    operator itype*() { return (type *) value; }
40613223Sodanrc@yahoo.com.br    operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
40713223Sodanrc@yahoo.com.br
40813223Sodanrc@yahoo.com.brprotected:
40913223Sodanrc@yahoo.com.br    typedef void *(*Constructor)(const void *stream);
41013223Sodanrc@yahoo.com.br#if !defined(_MSC_VER)
41113223Sodanrc@yahoo.com.br    /* Only enabled when the types are {copy,move}-constructible *and* when the type
41213223Sodanrc@yahoo.com.br       does not have a private operator new implementaton. */
41313223Sodanrc@yahoo.com.br    template <typename T = type, typename = enable_if_t<is_copy_constructible<T>::value>> static auto make_copy_constructor(const T *value) -> decltype(new T(*value), Constructor(nullptr)) {
41413223Sodanrc@yahoo.com.br        return [](const void *arg) -> void * { return new T(*((const T *) arg)); }; }
41513223Sodanrc@yahoo.com.br    template <typename T = type> static auto make_move_constructor(const T *value) -> decltype(new T(std::move(*((T *) value))), Constructor(nullptr)) {
41613223Sodanrc@yahoo.com.br        return [](const void *arg) -> void * { return (void *) new T(std::move(*((T *) arg))); }; }
41713223Sodanrc@yahoo.com.br#else
41813223Sodanrc@yahoo.com.br    /* Visual Studio 2015's SFINAE implementation doesn't yet handle the above robustly in all situations.
41913223Sodanrc@yahoo.com.br       Use a workaround that only tests for constructibility for now. */
42013223Sodanrc@yahoo.com.br    template <typename T = type, typename = enable_if_t<is_copy_constructible<T>::value>>
42113223Sodanrc@yahoo.com.br    static Constructor make_copy_constructor(const T *value) {
42213223Sodanrc@yahoo.com.br        return [](const void *arg) -> void * { return new T(*((const T *)arg)); }; }
42313223Sodanrc@yahoo.com.br    template <typename T = type, typename = enable_if_t<std::is_move_constructible<T>::value>>
42413223Sodanrc@yahoo.com.br    static Constructor make_move_constructor(const T *value) {
42513223Sodanrc@yahoo.com.br        return [](const void *arg) -> void * { return (void *) new T(std::move(*((T *)arg))); }; }
42613223Sodanrc@yahoo.com.br#endif
42713223Sodanrc@yahoo.com.br
42813223Sodanrc@yahoo.com.br    static Constructor make_copy_constructor(...) { return nullptr; }
42913223Sodanrc@yahoo.com.br    static Constructor make_move_constructor(...) { return nullptr; }
43013223Sodanrc@yahoo.com.br};
43113223Sodanrc@yahoo.com.br
43213223Sodanrc@yahoo.com.brtemplate <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };
43313223Sodanrc@yahoo.com.brtemplate <typename type> using make_caster = type_caster<intrinsic_t<type>>;
43413223Sodanrc@yahoo.com.br
43513223Sodanrc@yahoo.com.br// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
43613223Sodanrc@yahoo.com.brtemplate <typename T> typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
43713223Sodanrc@yahoo.com.br    return caster.operator typename make_caster<T>::template cast_op_type<T>();
43813223Sodanrc@yahoo.com.br}
43913223Sodanrc@yahoo.com.brtemplate <typename T> typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &&caster) {
44013223Sodanrc@yahoo.com.br    return cast_op<T>(caster);
44113223Sodanrc@yahoo.com.br}
44213223Sodanrc@yahoo.com.br
44313223Sodanrc@yahoo.com.brtemplate <typename type> class type_caster<std::reference_wrapper<type>> : public type_caster_base<type> {
44413223Sodanrc@yahoo.com.brpublic:
44513223Sodanrc@yahoo.com.br    static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
44613223Sodanrc@yahoo.com.br        return type_caster_base<type>::cast(&src.get(), policy, parent);
44713223Sodanrc@yahoo.com.br    }
44813223Sodanrc@yahoo.com.br    template <typename T> using cast_op_type = std::reference_wrapper<type>;
44913223Sodanrc@yahoo.com.br    operator std::reference_wrapper<type>() { return std::ref(*((type *) this->value)); }
45013223Sodanrc@yahoo.com.br};
45113223Sodanrc@yahoo.com.br
45213223Sodanrc@yahoo.com.br#define PYBIND11_TYPE_CASTER(type, py_name) \
45313223Sodanrc@yahoo.com.br    protected: \
45413223Sodanrc@yahoo.com.br        type value; \
45513223Sodanrc@yahoo.com.br    public: \
45613223Sodanrc@yahoo.com.br        static PYBIND11_DESCR name() { return type_descr(py_name); } \
45713223Sodanrc@yahoo.com.br        static handle cast(const type *src, return_value_policy policy, handle parent) { \
45813223Sodanrc@yahoo.com.br            return cast(*src, policy, parent); \
45913223Sodanrc@yahoo.com.br        } \
46013223Sodanrc@yahoo.com.br        operator type*() { return &value; } \
46113223Sodanrc@yahoo.com.br        operator type&() { return value; } \
46213223Sodanrc@yahoo.com.br        template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>
46313223Sodanrc@yahoo.com.br
46413223Sodanrc@yahoo.com.br
46513223Sodanrc@yahoo.com.brtemplate <typename T>
46613223Sodanrc@yahoo.com.brstruct type_caster<T, enable_if_t<std::is_arithmetic<T>::value>> {
46713223Sodanrc@yahoo.com.br    typedef typename std::conditional<sizeof(T) <= sizeof(long), long, long long>::type _py_type_0;
46813223Sodanrc@yahoo.com.br    typedef typename std::conditional<std::is_signed<T>::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>::type _py_type_1;
46913223Sodanrc@yahoo.com.br    typedef typename std::conditional<std::is_floating_point<T>::value, double, _py_type_1>::type py_type;
47013223Sodanrc@yahoo.com.brpublic:
47113223Sodanrc@yahoo.com.br
47213223Sodanrc@yahoo.com.br    bool load(handle src, bool) {
47313223Sodanrc@yahoo.com.br        py_type py_value;
47413223Sodanrc@yahoo.com.br
47513445Sodanrc@yahoo.com.br        if (!src) {
47613445Sodanrc@yahoo.com.br            return false;
47713445Sodanrc@yahoo.com.br        } if (std::is_floating_point<T>::value) {
47813223Sodanrc@yahoo.com.br            py_value = (py_type) PyFloat_AsDouble(src.ptr());
47913223Sodanrc@yahoo.com.br        } else if (sizeof(T) <= sizeof(long)) {
48013223Sodanrc@yahoo.com.br            if (PyFloat_Check(src.ptr()))
48113223Sodanrc@yahoo.com.br                return false;
48213223Sodanrc@yahoo.com.br            if (std::is_signed<T>::value)
48313445Sodanrc@yahoo.com.br                py_value = (py_type) PyLong_AsLong(src.ptr());
48413223Sodanrc@yahoo.com.br            else
48513445Sodanrc@yahoo.com.br                py_value = (py_type) PyLong_AsUnsignedLong(src.ptr());
48613445Sodanrc@yahoo.com.br        } else {
48713445Sodanrc@yahoo.com.br            if (PyFloat_Check(src.ptr()))
48813223Sodanrc@yahoo.com.br                return false;
48913223Sodanrc@yahoo.com.br            if (std::is_signed<T>::value)
49013223Sodanrc@yahoo.com.br                py_value = (py_type) PYBIND11_LONG_AS_LONGLONG(src.ptr());
49113223Sodanrc@yahoo.com.br            else
49213223Sodanrc@yahoo.com.br                py_value = (py_type) PYBIND11_LONG_AS_UNSIGNED_LONGLONG(src.ptr());
49313223Sodanrc@yahoo.com.br        }
49413223Sodanrc@yahoo.com.br
49513223Sodanrc@yahoo.com.br        if ((py_value == (py_type) -1 && PyErr_Occurred()) ||
49613223Sodanrc@yahoo.com.br            (std::is_integral<T>::value && sizeof(py_type) != sizeof(T) &&
49713223Sodanrc@yahoo.com.br               (py_value < (py_type) std::numeric_limits<T>::min() ||
49813223Sodanrc@yahoo.com.br                py_value > (py_type) std::numeric_limits<T>::max()))) {
49913223Sodanrc@yahoo.com.br#if PY_VERSION_HEX < 0x03000000
50013223Sodanrc@yahoo.com.br            bool type_error = PyErr_ExceptionMatches(PyExc_SystemError);
50113223Sodanrc@yahoo.com.br#else
50213223Sodanrc@yahoo.com.br            bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
50313223Sodanrc@yahoo.com.br#endif
50413223Sodanrc@yahoo.com.br            PyErr_Clear();
50513223Sodanrc@yahoo.com.br            if (type_error && PyNumber_Check(src.ptr())) {
50613223Sodanrc@yahoo.com.br                auto tmp = reinterpret_borrow<object>(std::is_floating_point<T>::value
50713223Sodanrc@yahoo.com.br                                                      ? PyNumber_Float(src.ptr())
50813223Sodanrc@yahoo.com.br                                                      : PyNumber_Long(src.ptr()));
50913223Sodanrc@yahoo.com.br                PyErr_Clear();
51013223Sodanrc@yahoo.com.br                return load(tmp, false);
51113223Sodanrc@yahoo.com.br            }
51213223Sodanrc@yahoo.com.br            return false;
51313223Sodanrc@yahoo.com.br        }
51413223Sodanrc@yahoo.com.br
51513223Sodanrc@yahoo.com.br        value = (T) py_value;
51613223Sodanrc@yahoo.com.br        return true;
51713223Sodanrc@yahoo.com.br    }
518
519    static handle cast(T src, return_value_policy /* policy */, handle /* parent */) {
520        if (std::is_floating_point<T>::value) {
521            return PyFloat_FromDouble((double) src);
522        } else if (sizeof(T) <= sizeof(long)) {
523            if (std::is_signed<T>::value)
524                return PyLong_FromLong((long) src);
525            else
526                return PyLong_FromUnsignedLong((unsigned long) src);
527        } else {
528            if (std::is_signed<T>::value)
529                return PyLong_FromLongLong((long long) src);
530            else
531                return PyLong_FromUnsignedLongLong((unsigned long long) src);
532        }
533    }
534
535    PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
536};
537
538template<typename T> struct void_caster {
539public:
540    bool load(handle, bool) { return false; }
541    static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
542        return none().inc_ref();
543    }
544    PYBIND11_TYPE_CASTER(T, _("None"));
545};
546
547template <> class type_caster<void_type> : public void_caster<void_type> {};
548
549template <> class type_caster<void> : public type_caster<void_type> {
550public:
551    using type_caster<void_type>::cast;
552
553    bool load(handle h, bool) {
554        if (!h) {
555            return false;
556        } else if (h.is_none()) {
557            value = nullptr;
558            return true;
559        }
560
561        /* Check if this is a capsule */
562        if (isinstance<capsule>(h)) {
563            value = reinterpret_borrow<capsule>(h);
564            return true;
565        }
566
567        /* Check if this is a C++ type */
568        if (get_type_info((PyTypeObject *) h.get_type().ptr())) {
569            value = ((instance<void> *) h.ptr())->value;
570            return true;
571        }
572
573        /* Fail */
574        return false;
575    }
576
577    static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
578        if (ptr)
579            return capsule(ptr).release();
580        else
581            return none().inc_ref();
582    }
583
584    template <typename T> using cast_op_type = void*&;
585    operator void *&() { return value; }
586    static PYBIND11_DESCR name() { return type_descr(_("capsule")); }
587private:
588    void *value = nullptr;
589};
590
591template <> class type_caster<std::nullptr_t> : public type_caster<void_type> { };
592
593template <> class type_caster<bool> {
594public:
595    bool load(handle src, bool) {
596        if (!src) return false;
597        else if (src.ptr() == Py_True) { value = true; return true; }
598        else if (src.ptr() == Py_False) { value = false; return true; }
599        else return false;
600    }
601    static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
602        return handle(src ? Py_True : Py_False).inc_ref();
603    }
604    PYBIND11_TYPE_CASTER(bool, _("bool"));
605};
606
607template <> class type_caster<std::string> {
608public:
609    bool load(handle src, bool) {
610        object temp;
611        handle load_src = src;
612        if (!src) {
613            return false;
614        } else if (PyUnicode_Check(load_src.ptr())) {
615            temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(load_src.ptr()));
616            if (!temp) { PyErr_Clear(); return false; }  // UnicodeEncodeError
617            load_src = temp;
618        }
619        char *buffer;
620        ssize_t length;
621        int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(load_src.ptr(), &buffer, &length);
622        if (err == -1) { PyErr_Clear(); return false; }  // TypeError
623        value = std::string(buffer, (size_t) length);
624        success = true;
625        return true;
626    }
627
628    static handle cast(const std::string &src, return_value_policy /* policy */, handle /* parent */) {
629        return PyUnicode_FromStringAndSize(src.c_str(), (ssize_t) src.length());
630    }
631
632    PYBIND11_TYPE_CASTER(std::string, _(PYBIND11_STRING_NAME));
633protected:
634    bool success = false;
635};
636
637template <typename type, typename deleter> class type_caster<std::unique_ptr<type, deleter>> {
638public:
639    static handle cast(std::unique_ptr<type, deleter> &&src, return_value_policy policy, handle parent) {
640        handle result = type_caster_base<type>::cast(src.get(), policy, parent);
641        if (result)
642            src.release();
643        return result;
644    }
645    static PYBIND11_DESCR name() { return type_caster_base<type>::name(); }
646};
647
648template <> class type_caster<std::wstring> {
649public:
650    bool load(handle src, bool) {
651        object temp;
652        handle load_src = src;
653        if (!src) {
654            return false;
655        } else if (!PyUnicode_Check(load_src.ptr())) {
656            temp = reinterpret_steal<object>(PyUnicode_FromObject(load_src.ptr()));
657            if (!temp) { PyErr_Clear(); return false; }
658            load_src = temp;
659        }
660        wchar_t *buffer = nullptr;
661        ssize_t length = -1;
662#if PY_MAJOR_VERSION >= 3
663        buffer = PyUnicode_AsWideCharString(load_src.ptr(), &length);
664#else
665        temp = reinterpret_steal<object>(
666            sizeof(wchar_t) == sizeof(short)
667                ? PyUnicode_AsUTF16String(load_src.ptr())
668                : PyUnicode_AsUTF32String(load_src.ptr()));
669        if (temp) {
670            int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), (char **) &buffer, &length);
671            if (err == -1) { buffer = nullptr; }  // TypeError
672            length = length / (ssize_t) sizeof(wchar_t) - 1; ++buffer; // Skip BOM
673        }
674#endif
675        if (!buffer) { PyErr_Clear(); return false; }
676        value = std::wstring(buffer, (size_t) length);
677        success = true;
678        return true;
679    }
680
681    static handle cast(const std::wstring &src, return_value_policy /* policy */, handle /* parent */) {
682        return PyUnicode_FromWideChar(src.c_str(), (ssize_t) src.length());
683    }
684
685    PYBIND11_TYPE_CASTER(std::wstring, _(PYBIND11_STRING_NAME));
686protected:
687    bool success = false;
688};
689
690template <> class type_caster<char> : public type_caster<std::string> {
691public:
692    bool load(handle src, bool convert) {
693        if (src.is_none()) return true;
694        return type_caster<std::string>::load(src, convert);
695    }
696
697    static handle cast(const char *src, return_value_policy /* policy */, handle /* parent */) {
698        if (src == nullptr) return none().inc_ref();
699        return PyUnicode_FromString(src);
700    }
701
702    static handle cast(char src, return_value_policy /* policy */, handle /* parent */) {
703        char str[2] = { src, '\0' };
704        return PyUnicode_DecodeLatin1(str, 1, nullptr);
705    }
706
707    operator char*() { return success ? (char *) value.c_str() : nullptr; }
708    operator char&() { return value[0]; }
709
710    static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); }
711};
712
713template <> class type_caster<wchar_t> : public type_caster<std::wstring> {
714public:
715    bool load(handle src, bool convert) {
716        if (src.is_none()) return true;
717        return type_caster<std::wstring>::load(src, convert);
718    }
719
720    static handle cast(const wchar_t *src, return_value_policy /* policy */, handle /* parent */) {
721        if (src == nullptr) return none().inc_ref();
722        return PyUnicode_FromWideChar(src, (ssize_t) wcslen(src));
723    }
724
725    static handle cast(wchar_t src, return_value_policy /* policy */, handle /* parent */) {
726        wchar_t wstr[2] = { src, L'\0' };
727        return PyUnicode_FromWideChar(wstr, 1);
728    }
729
730    operator wchar_t*() { return success ? (wchar_t *) value.c_str() : nullptr; }
731    operator wchar_t&() { return value[0]; }
732
733    static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); }
734};
735
736template <typename T1, typename T2> class type_caster<std::pair<T1, T2>> {
737    typedef std::pair<T1, T2> type;
738public:
739    bool load(handle src, bool convert) {
740        if (!isinstance<sequence>(src))
741            return false;
742        const auto seq = reinterpret_borrow<sequence>(src);
743        if (seq.size() != 2)
744            return false;
745        return first.load(seq[0], convert) && second.load(seq[1], convert);
746    }
747
748    static handle cast(const type &src, return_value_policy policy, handle parent) {
749        auto o1 = reinterpret_steal<object>(make_caster<T1>::cast(src.first, policy, parent));
750        auto o2 = reinterpret_steal<object>(make_caster<T2>::cast(src.second, policy, parent));
751        if (!o1 || !o2)
752            return handle();
753        tuple result(2);
754        PyTuple_SET_ITEM(result.ptr(), 0, o1.release().ptr());
755        PyTuple_SET_ITEM(result.ptr(), 1, o2.release().ptr());
756        return result.release();
757    }
758
759    static PYBIND11_DESCR name() {
760        return type_descr(
761            _("Tuple[") + make_caster<T1>::name() + _(", ") + make_caster<T2>::name() + _("]")
762        );
763    }
764
765    template <typename T> using cast_op_type = type;
766
767    operator type() {
768        return type(cast_op<T1>(first), cast_op<T2>(second));
769    }
770protected:
771    make_caster<T1> first;
772    make_caster<T2> second;
773};
774
775template <typename... Tuple> class type_caster<std::tuple<Tuple...>> {
776    using type = std::tuple<Tuple...>;
777    using indices = make_index_sequence<sizeof...(Tuple)>;
778    static constexpr auto size = sizeof...(Tuple);
779
780public:
781    bool load(handle src, bool convert) {
782        if (!isinstance<sequence>(src))
783            return false;
784        const auto seq = reinterpret_borrow<sequence>(src);
785        if (seq.size() != size)
786            return false;
787        return load_impl(seq, convert, indices{});
788    }
789
790    static handle cast(const type &src, return_value_policy policy, handle parent) {
791        return cast_impl(src, policy, parent, indices{});
792    }
793
794    static PYBIND11_DESCR name() {
795        return type_descr(_("Tuple[") + detail::concat(make_caster<Tuple>::name()...) + _("]"));
796    }
797
798    template <typename T> using cast_op_type = type;
799
800    operator type() { return implicit_cast(indices{}); }
801
802protected:
803    template <size_t... Is>
804    type implicit_cast(index_sequence<Is...>) { return type(cast_op<Tuple>(std::get<Is>(value))...); }
805
806    static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
807
808    template <size_t... Is>
809    bool load_impl(const sequence &seq, bool convert, index_sequence<Is...>) {
810        for (bool r : {std::get<Is>(value).load(seq[Is], convert)...})
811            if (!r)
812                return false;
813        return true;
814    }
815
816    static handle cast_impl(const type &, return_value_policy, handle,
817                            index_sequence<>) { return tuple().release(); }
818
819    /* Implementation: Convert a C++ tuple into a Python tuple */
820    template <size_t... Is>
821    static handle cast_impl(const type &src, return_value_policy policy, handle parent, index_sequence<Is...>) {
822        std::array<object, size> entries {{
823            reinterpret_steal<object>(make_caster<Tuple>::cast(std::get<Is>(src), policy, parent))...
824        }};
825        for (const auto &entry: entries)
826            if (!entry)
827                return handle();
828        tuple result(size);
829        int counter = 0;
830        for (auto & entry: entries)
831            PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
832        return result.release();
833    }
834
835protected:
836    std::tuple<make_caster<Tuple>...> value;
837};
838
839/// Type caster for holder types like std::shared_ptr, etc.
840template <typename type, typename holder_type> class type_caster_holder : public type_caster_base<type> {
841public:
842    using base = type_caster_base<type>;
843    using base::base;
844    using base::cast;
845    using base::typeinfo;
846    using base::value;
847    using base::temp;
848
849    PYBIND11_NOINLINE bool load(handle src, bool convert) {
850        return load(src, convert, Py_TYPE(src.ptr()));
851    }
852
853    bool load(handle src, bool convert, PyTypeObject *tobj) {
854        if (!src || !typeinfo)
855            return false;
856        if (src.is_none()) {
857            value = nullptr;
858            return true;
859        }
860
861        if (typeinfo->simple_type) { /* Case 1: no multiple inheritance etc. involved */
862            /* Check if we can safely perform a reinterpret-style cast */
863            if (PyType_IsSubtype(tobj, typeinfo->type))
864                return load_value_and_holder(src);
865        } else { /* Case 2: multiple inheritance */
866            /* Check if we can safely perform a reinterpret-style cast */
867            if (tobj == typeinfo->type)
868                return load_value_and_holder(src);
869
870            /* If this is a python class, also check the parents recursively */
871            auto const &type_dict = get_internals().registered_types_py;
872            bool new_style_class = PyType_Check(tobj);
873            if (type_dict.find(tobj) == type_dict.end() && new_style_class && tobj->tp_bases) {
874                auto parents = reinterpret_borrow<tuple>(tobj->tp_bases);
875                for (handle parent : parents) {
876                    bool result = load(src, convert, (PyTypeObject *) parent.ptr());
877                    if (result)
878                        return true;
879                }
880            }
881
882            if (try_implicit_casts(src, convert))
883                return true;
884        }
885
886        if (convert) {
887            for (auto &converter : typeinfo->implicit_conversions) {
888                temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
889                if (load(temp, false))
890                    return true;
891            }
892        }
893
894        return false;
895    }
896
897    bool load_value_and_holder(handle src) {
898        auto inst = (instance<type, holder_type> *) src.ptr();
899        value = (void *) inst->value;
900        if (inst->holder_constructed) {
901            holder = inst->holder;
902            return true;
903        } else {
904            throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
905#if defined(NDEBUG)
906                             "(compile in debug mode for type information)");
907#else
908                             "of type '" + type_id<holder_type>() + "''");
909#endif
910        }
911    }
912
913    template <typename T = holder_type, detail::enable_if_t<!std::is_constructible<T, const T &, type*>::value, int> = 0>
914    bool try_implicit_casts(handle, bool) { return false; }
915
916    template <typename T = holder_type, detail::enable_if_t<std::is_constructible<T, const T &, type*>::value, int> = 0>
917    bool try_implicit_casts(handle src, bool convert) {
918        for (auto &cast : typeinfo->implicit_casts) {
919            type_caster_holder sub_caster(*cast.first);
920            if (sub_caster.load(src, convert)) {
921                value = cast.second(sub_caster.value);
922                holder = holder_type(sub_caster.holder, (type *) value);
923                return true;
924            }
925        }
926        return false;
927    }
928
929    explicit operator type*() { return this->value; }
930    explicit operator type&() { return *(this->value); }
931    explicit operator holder_type*() { return &holder; }
932
933    // Workaround for Intel compiler bug
934    // see pybind11 issue 94
935    #if defined(__ICC) || defined(__INTEL_COMPILER)
936    operator holder_type&() { return holder; }
937    #else
938    explicit operator holder_type&() { return holder; }
939    #endif
940
941    static handle cast(const holder_type &src, return_value_policy, handle) {
942        return type_caster_generic::cast(
943            src.get(), return_value_policy::take_ownership, handle(),
944            src.get() ? &typeid(*src.get()) : nullptr, &typeid(type),
945            nullptr, nullptr, &src);
946    }
947
948protected:
949    holder_type holder;
950};
951
952/// Specialize for the common std::shared_ptr, so users don't need to
953template <typename T>
954class type_caster<std::shared_ptr<T>> : public type_caster_holder<T, std::shared_ptr<T>> { };
955
956/// Create a specialization for custom holder types (silently ignores std::shared_ptr)
957#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type) \
958    namespace pybind11 { namespace detail { \
959    template <typename type> \
960    class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
961        : public type_caster_holder<type, holder_type> { }; \
962    }}
963
964// PYBIND11_DECLARE_HOLDER_TYPE holder types:
965template <typename base, typename holder> struct is_holder_type :
966    std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
967// Specialization for always-supported unique_ptr holders:
968template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
969    std::true_type {};
970
971template <typename T> struct handle_type_name { static PYBIND11_DESCR name() { return _<T>(); } };
972template <> struct handle_type_name<bytes> { static PYBIND11_DESCR name() { return _(PYBIND11_BYTES_NAME); } };
973template <> struct handle_type_name<args> { static PYBIND11_DESCR name() { return _("*args"); } };
974template <> struct handle_type_name<kwargs> { static PYBIND11_DESCR name() { return _("**kwargs"); } };
975
976template <typename type>
977struct pyobject_caster {
978    template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
979    bool load(handle src, bool /* convert */) { value = src; return static_cast<bool>(value); }
980
981    template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
982    bool load(handle src, bool /* convert */) {
983        if (!isinstance<type>(src))
984            return false;
985        value = reinterpret_borrow<type>(src);
986        return true;
987    }
988
989    static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
990        return src.inc_ref();
991    }
992    PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name());
993};
994
995template <typename T>
996class type_caster<T, enable_if_t<is_pyobject<T>::value>> : public pyobject_caster<T> { };
997
998// Our conditions for enabling moving are quite restrictive:
999// At compile time:
1000// - T needs to be a non-const, non-pointer, non-reference type
1001// - type_caster<T>::operator T&() must exist
1002// - the type must be move constructible (obviously)
1003// At run-time:
1004// - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
1005//   must have ref_count() == 1)h
1006// If any of the above are not satisfied, we fall back to copying.
1007template <typename T, typename SFINAE = void> struct move_is_plain_type : std::false_type {};
1008template <typename T> struct move_is_plain_type<T, enable_if_t<
1009        !std::is_void<T>::value && !std::is_pointer<T>::value && !std::is_reference<T>::value && !std::is_const<T>::value
1010    >> : std::true_type { };
1011template <typename T, typename SFINAE = void> struct move_always : std::false_type {};
1012template <typename T> struct move_always<T, enable_if_t<
1013        move_is_plain_type<T>::value &&
1014        !std::is_copy_constructible<T>::value && std::is_move_constructible<T>::value &&
1015        std::is_same<decltype(std::declval<type_caster<T>>().operator T&()), T&>::value
1016    >> : std::true_type { };
1017template <typename T, typename SFINAE = void> struct move_if_unreferenced : std::false_type {};
1018template <typename T> struct move_if_unreferenced<T, enable_if_t<
1019        move_is_plain_type<T>::value &&
1020        !move_always<T>::value && std::is_move_constructible<T>::value &&
1021        std::is_same<decltype(std::declval<type_caster<T>>().operator T&()), T&>::value
1022    >> : std::true_type { };
1023template <typename T> using move_never = std::integral_constant<bool, !move_always<T>::value && !move_if_unreferenced<T>::value>;
1024
1025// Detect whether returning a `type` from a cast on type's type_caster is going to result in a
1026// reference or pointer to a local variable of the type_caster.  Basically, only
1027// non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
1028// everything else returns a reference/pointer to a local variable.
1029template <typename type> using cast_is_temporary_value_reference = bool_constant<
1030    (std::is_reference<type>::value || std::is_pointer<type>::value) &&
1031    !std::is_base_of<type_caster_generic, make_caster<type>>::value
1032>;
1033
1034// Basic python -> C++ casting; throws if casting fails
1035template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &handle) {
1036    if (!conv.load(handle, true)) {
1037#if defined(NDEBUG)
1038        throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)");
1039#else
1040        throw cast_error("Unable to cast Python instance of type " +
1041            (std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "''");
1042#endif
1043    }
1044    return conv;
1045}
1046// Wrapper around the above that also constructs and returns a type_caster
1047template <typename T> make_caster<T> load_type(const handle &handle) {
1048    make_caster<T> conv;
1049    load_type(conv, handle);
1050    return conv;
1051}
1052
1053NAMESPACE_END(detail)
1054
1055// pytype -> C++ type
1056template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1057T cast(const handle &handle) {
1058    using namespace detail;
1059    static_assert(!cast_is_temporary_value_reference<T>::value,
1060            "Unable to cast type to reference: value is local to type caster");
1061    return cast_op<T>(load_type<T>(handle));
1062}
1063
1064// pytype -> pytype (calls converting constructor)
1065template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
1066T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
1067
1068// C++ type -> py::object
1069template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1070object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference,
1071            handle parent = handle()) {
1072    if (policy == return_value_policy::automatic)
1073        policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
1074    else if (policy == return_value_policy::automatic_reference)
1075        policy = std::is_pointer<T>::value ? return_value_policy::reference : return_value_policy::copy;
1076    return reinterpret_steal<object>(detail::make_caster<T>::cast(value, policy, parent));
1077}
1078
1079template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); }
1080template <> inline void handle::cast() const { return; }
1081
1082template <typename T>
1083detail::enable_if_t<detail::move_always<T>::value || detail::move_if_unreferenced<T>::value, T> move(object &&obj) {
1084    if (obj.ref_count() > 1)
1085#if defined(NDEBUG)
1086        throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references"
1087            " (compile in debug mode for details)");
1088#else
1089        throw cast_error("Unable to move from Python " + (std::string) str(obj.get_type()) +
1090                " instance to C++ " + type_id<T>() + " instance: instance has multiple references");
1091#endif
1092
1093    // Move into a temporary and return that, because the reference may be a local value of `conv`
1094    T ret = std::move(detail::load_type<T>(obj).operator T&());
1095    return ret;
1096}
1097
1098// Calling cast() on an rvalue calls pybind::cast with the object rvalue, which does:
1099// - If we have to move (because T has no copy constructor), do it.  This will fail if the moved
1100//   object has multiple references, but trying to copy will fail to compile.
1101// - If both movable and copyable, check ref count: if 1, move; otherwise copy
1102// - Otherwise (not movable), copy.
1103template <typename T> detail::enable_if_t<detail::move_always<T>::value, T> cast(object &&object) {
1104    return move<T>(std::move(object));
1105}
1106template <typename T> detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
1107    if (object.ref_count() > 1)
1108        return cast<T>(object);
1109    else
1110        return move<T>(std::move(object));
1111}
1112template <typename T> detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
1113    return cast<T>(object);
1114}
1115
1116template <typename T> T object::cast() const & { return pybind11::cast<T>(*this); }
1117template <typename T> T object::cast() && { return pybind11::cast<T>(std::move(*this)); }
1118template <> inline void object::cast() const & { return; }
1119template <> inline void object::cast() && { return; }
1120
1121NAMESPACE_BEGIN(detail)
1122
1123// Declared in pytypes.h:
1124template <typename T, enable_if_t<!is_pyobject<T>::value, int>>
1125object object_or_cast(T &&o) { return pybind11::cast(std::forward<T>(o)); }
1126
1127struct overload_unused {}; // Placeholder type for the unneeded (and dead code) static variable in the OVERLOAD_INT macro
1128template <typename ret_type> using overload_caster_t = conditional_t<
1129    cast_is_temporary_value_reference<ret_type>::value, make_caster<ret_type>, overload_unused>;
1130
1131// Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
1132// store the result in the given variable.  For other types, this is a no-op.
1133template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&o, make_caster<T> &caster) {
1134    return cast_op<T>(load_type(caster, o));
1135}
1136template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&, overload_unused &) {
1137    pybind11_fail("Internal error: cast_ref fallback invoked"); }
1138
1139// Trampoline use: Having a pybind11::cast with an invalid reference type is going to static_assert, even
1140// though if it's in dead code, so we provide a "trampoline" to pybind11::cast that only does anything in
1141// cases where pybind11::cast is valid.
1142template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&o) {
1143    return pybind11::cast<T>(std::move(o)); }
1144template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&) {
1145    pybind11_fail("Internal error: cast_safe fallback invoked"); }
1146template <> inline void cast_safe<void>(object &&) {}
1147
1148NAMESPACE_END(detail)
1149
1150template <return_value_policy policy = return_value_policy::automatic_reference,
1151          typename... Args> tuple make_tuple(Args&&... args_) {
1152    const size_t size = sizeof...(Args);
1153    std::array<object, size> args {
1154        { reinterpret_steal<object>(detail::make_caster<Args>::cast(
1155            std::forward<Args>(args_), policy, nullptr))... }
1156    };
1157    for (auto &arg_value : args) {
1158        if (!arg_value) {
1159#if defined(NDEBUG)
1160            throw cast_error("make_tuple(): unable to convert arguments to Python object (compile in debug mode for details)");
1161#else
1162            throw cast_error("make_tuple(): unable to convert arguments of types '" +
1163                (std::string) type_id<std::tuple<Args...>>() + "' to Python object");
1164#endif
1165        }
1166    }
1167    tuple result(size);
1168    int counter = 0;
1169    for (auto &arg_value : args)
1170        PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1171    return result;
1172}
1173
1174/// Annotation for keyword arguments
1175struct arg {
1176    constexpr explicit arg(const char *name) : name(name) { }
1177    template <typename T> arg_v operator=(T &&value) const;
1178
1179    const char *name;
1180};
1181
1182/// Annotation for keyword arguments with values
1183struct arg_v : arg {
1184    template <typename T>
1185    arg_v(const char *name, T &&x, const char *descr = nullptr)
1186        : arg(name),
1187          value(reinterpret_steal<object>(
1188              detail::make_caster<T>::cast(x, return_value_policy::automatic, {})
1189          )),
1190          descr(descr)
1191#if !defined(NDEBUG)
1192        , type(type_id<T>())
1193#endif
1194    { }
1195
1196    object value;
1197    const char *descr;
1198#if !defined(NDEBUG)
1199    std::string type;
1200#endif
1201};
1202
1203template <typename T>
1204arg_v arg::operator=(T &&value) const { return {name, std::forward<T>(value)}; }
1205
1206/// Alias for backward compatibility -- to be removed in version 2.0
1207template <typename /*unused*/> using arg_t = arg_v;
1208
1209inline namespace literals {
1210/// String literal version of arg
1211constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
1212}
1213
1214NAMESPACE_BEGIN(detail)
1215
1216/// Helper class which loads arguments for C++ functions called from Python
1217template <typename... Args>
1218class argument_loader {
1219    using itypes = type_list<intrinsic_t<Args>...>;
1220    using indices = make_index_sequence<sizeof...(Args)>;
1221
1222public:
1223    static constexpr auto has_kwargs = std::is_same<itypes, type_list<args, kwargs>>::value;
1224    static constexpr auto has_args = has_kwargs || std::is_same<itypes, type_list<args>>::value;
1225
1226    static PYBIND11_DESCR arg_names() { return detail::concat(make_caster<Args>::name()...); }
1227
1228    bool load_args(handle args, handle kwargs, bool convert) {
1229        return load_impl(args, kwargs, convert, itypes{});
1230    }
1231
1232    template <typename Return, typename Func>
1233    enable_if_t<!std::is_void<Return>::value, Return> call(Func &&f) {
1234        return call_impl<Return>(std::forward<Func>(f), indices{});
1235    }
1236
1237    template <typename Return, typename Func>
1238    enable_if_t<std::is_void<Return>::value, void_type> call(Func &&f) {
1239        call_impl<Return>(std::forward<Func>(f), indices{});
1240        return void_type();
1241    }
1242
1243private:
1244    bool load_impl(handle args_, handle, bool convert, type_list<args>) {
1245        std::get<0>(value).load(args_, convert);
1246        return true;
1247    }
1248
1249    bool load_impl(handle args_, handle kwargs_, bool convert, type_list<args, kwargs>) {
1250        std::get<0>(value).load(args_, convert);
1251        std::get<1>(value).load(kwargs_, convert);
1252        return true;
1253    }
1254
1255    bool load_impl(handle args, handle, bool convert, ... /* anything else */) {
1256        return load_impl_sequence(args, convert, indices{});
1257    }
1258
1259    static constexpr bool load_impl_sequence(handle, bool, index_sequence<>) { return true; }
1260
1261    template <size_t... Is>
1262    bool load_impl_sequence(handle src, bool convert, index_sequence<Is...>) {
1263        for (bool r : {std::get<Is>(value).load(PyTuple_GET_ITEM(src.ptr(), Is), convert)...})
1264            if (!r)
1265                return false;
1266        return true;
1267    }
1268
1269    template <typename Return, typename Func, size_t... Is>
1270    Return call_impl(Func &&f, index_sequence<Is...>) {
1271        return std::forward<Func>(f)(cast_op<Args>(std::get<Is>(value))...);
1272    }
1273
1274private:
1275    std::tuple<make_caster<Args>...> value;
1276};
1277
1278NAMESPACE_BEGIN(constexpr_impl)
1279/// Implementation details for constexpr functions
1280constexpr int first(int i) { return i; }
1281template <typename T, typename... Ts>
1282constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); }
1283
1284constexpr int last(int /*i*/, int result) { return result; }
1285template <typename T, typename... Ts>
1286constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); }
1287NAMESPACE_END(constexpr_impl)
1288
1289/// Return the index of the first type in Ts which satisfies Predicate<T>
1290template <template<typename> class Predicate, typename... Ts>
1291constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate<Ts>::value...); }
1292
1293/// Return the index of the last type in Ts which satisfies Predicate<T>
1294template <template<typename> class Predicate, typename... Ts>
1295constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate<Ts>::value...); }
1296
1297/// Helper class which collects only positional arguments for a Python function call.
1298/// A fancier version below can collect any argument, but this one is optimal for simple calls.
1299template <return_value_policy policy>
1300class simple_collector {
1301public:
1302    template <typename... Ts>
1303    explicit simple_collector(Ts &&...values)
1304        : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) { }
1305
1306    const tuple &args() const & { return m_args; }
1307    dict kwargs() const { return {}; }
1308
1309    tuple args() && { return std::move(m_args); }
1310
1311    /// Call a Python function and pass the collected arguments
1312    object call(PyObject *ptr) const {
1313        PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1314        if (!result)
1315            throw error_already_set();
1316        return reinterpret_steal<object>(result);
1317    }
1318
1319private:
1320    tuple m_args;
1321};
1322
1323/// Helper class which collects positional, keyword, * and ** arguments for a Python function call
1324template <return_value_policy policy>
1325class unpacking_collector {
1326public:
1327    template <typename... Ts>
1328    explicit unpacking_collector(Ts &&...values) {
1329        // Tuples aren't (easily) resizable so a list is needed for collection,
1330        // but the actual function call strictly requires a tuple.
1331        auto args_list = list();
1332        int _[] = { 0, (process(args_list, std::forward<Ts>(values)), 0)... };
1333        ignore_unused(_);
1334
1335        m_args = std::move(args_list);
1336    }
1337
1338    const tuple &args() const & { return m_args; }
1339    const dict &kwargs() const & { return m_kwargs; }
1340
1341    tuple args() && { return std::move(m_args); }
1342    dict kwargs() && { return std::move(m_kwargs); }
1343
1344    /// Call a Python function and pass the collected arguments
1345    object call(PyObject *ptr) const {
1346        PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1347        if (!result)
1348            throw error_already_set();
1349        return reinterpret_steal<object>(result);
1350    }
1351
1352private:
1353    template <typename T>
1354    void process(list &args_list, T &&x) {
1355        auto o = reinterpret_steal<object>(detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1356        if (!o) {
1357#if defined(NDEBUG)
1358            argument_cast_error();
1359#else
1360            argument_cast_error(std::to_string(args_list.size()), type_id<T>());
1361#endif
1362        }
1363        args_list.append(o);
1364    }
1365
1366    void process(list &args_list, detail::args_proxy ap) {
1367        for (const auto &a : ap)
1368            args_list.append(a);
1369    }
1370
1371    void process(list &/*args_list*/, arg_v a) {
1372        if (m_kwargs.contains(a.name)) {
1373#if defined(NDEBUG)
1374            multiple_values_error();
1375#else
1376            multiple_values_error(a.name);
1377#endif
1378        }
1379        if (!a.value) {
1380#if defined(NDEBUG)
1381            argument_cast_error();
1382#else
1383            argument_cast_error(a.name, a.type);
1384#endif
1385        }
1386        m_kwargs[a.name] = a.value;
1387    }
1388
1389    void process(list &/*args_list*/, detail::kwargs_proxy kp) {
1390        if (!kp)
1391            return;
1392        for (const auto &k : reinterpret_borrow<dict>(kp)) {
1393            if (m_kwargs.contains(k.first)) {
1394#if defined(NDEBUG)
1395                multiple_values_error();
1396#else
1397                multiple_values_error(str(k.first));
1398#endif
1399            }
1400            m_kwargs[k.first] = k.second;
1401        }
1402    }
1403
1404    [[noreturn]] static void multiple_values_error() {
1405        throw type_error("Got multiple values for keyword argument "
1406                         "(compile in debug mode for details)");
1407    }
1408
1409    [[noreturn]] static void multiple_values_error(std::string name) {
1410        throw type_error("Got multiple values for keyword argument '" + name + "'");
1411    }
1412
1413    [[noreturn]] static void argument_cast_error() {
1414        throw cast_error("Unable to convert call argument to Python object "
1415                         "(compile in debug mode for details)");
1416    }
1417
1418    [[noreturn]] static void argument_cast_error(std::string name, std::string type) {
1419        throw cast_error("Unable to convert call argument '" + name
1420                         + "' of type '" + type + "' to Python object");
1421    }
1422
1423private:
1424    tuple m_args;
1425    dict m_kwargs;
1426};
1427
1428/// Collect only positional arguments for a Python function call
1429template <return_value_policy policy, typename... Args,
1430          typename = enable_if_t<all_of_t<is_positional, Args...>::value>>
1431simple_collector<policy> collect_arguments(Args &&...args) {
1432    return simple_collector<policy>(std::forward<Args>(args)...);
1433}
1434
1435/// Collect all arguments, including keywords and unpacking (only instantiated when needed)
1436template <return_value_policy policy, typename... Args,
1437          typename = enable_if_t<!all_of_t<is_positional, Args...>::value>>
1438unpacking_collector<policy> collect_arguments(Args &&...args) {
1439    // Following argument order rules for generalized unpacking according to PEP 448
1440    static_assert(
1441        constexpr_last<is_positional, Args...>() < constexpr_first<is_keyword_or_ds, Args...>()
1442        && constexpr_last<is_s_unpacking, Args...>() < constexpr_first<is_ds_unpacking, Args...>(),
1443        "Invalid function call: positional args must precede keywords and ** unpacking; "
1444        "* unpacking must precede ** unpacking"
1445    );
1446    return unpacking_collector<policy>(std::forward<Args>(args)...);
1447}
1448
1449template <typename Derived>
1450template <return_value_policy policy, typename... Args>
1451object object_api<Derived>::operator()(Args &&...args) const {
1452    return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1453}
1454
1455template <typename Derived>
1456template <return_value_policy policy, typename... Args>
1457object object_api<Derived>::call(Args &&...args) const {
1458    return operator()<policy>(std::forward<Args>(args)...);
1459}
1460
1461NAMESPACE_END(detail)
1462
1463#define PYBIND11_MAKE_OPAQUE(Type) \
1464    namespace pybind11 { namespace detail { \
1465        template<> class type_caster<Type> : public type_caster_base<Type> { }; \
1466    }}
1467
1468NAMESPACE_END(pybind11)
1469