|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// |
| 4 | +// From pybind11 documentation |
| 5 | +// |
| 6 | + |
| 7 | +#include <pybind11/pybind11.h> |
| 8 | + |
| 9 | +#include "../ns_inty.h" |
| 10 | + |
| 11 | +namespace pybind11 { namespace detail { |
| 12 | + template <> struct type_caster<ns::inty2> { |
| 13 | + public: |
| 14 | + /** |
| 15 | + * This macro establishes the name 'inty' in |
| 16 | + * function signatures and declares a local variable |
| 17 | + * 'value' of type inty |
| 18 | + */ |
| 19 | + PYBIND11_TYPE_CASTER(ns::inty2, const_name("swtest_base.inty")); |
| 20 | + |
| 21 | + /** |
| 22 | + * Conversion part 1 (Python->C++): convert a PyObject into a inty |
| 23 | + * instance or return false upon failure. The second argument |
| 24 | + * indicates whether implicit conversions should be applied. |
| 25 | + */ |
| 26 | + bool load(handle src, bool) { |
| 27 | + /* Extract PyObject from handle */ |
| 28 | + PyObject *source = src.ptr(); |
| 29 | + /* Try converting into a Python integer value */ |
| 30 | + PyObject *tmp = PyNumber_Long(source); |
| 31 | + if (!tmp) |
| 32 | + return false; |
| 33 | + /* Now try to convert into a C++ int */ |
| 34 | + value.long_value = PyLong_AsLong(tmp); |
| 35 | + Py_DECREF(tmp); |
| 36 | + /* Ensure return code was OK (to avoid out-of-range errors etc) */ |
| 37 | + return !(value.long_value == -1 && !PyErr_Occurred()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Conversion part 2 (C++ -> Python): convert an inty instance into |
| 42 | + * a Python object. The second and third arguments are used to |
| 43 | + * indicate the return value policy and parent object (for |
| 44 | + * ``return_value_policy::reference_internal``) and are generally |
| 45 | + * ignored by implicit casters. |
| 46 | + */ |
| 47 | + static handle cast(ns::inty2 src, return_value_policy /* policy */, handle /* parent */) { |
| 48 | + return PyLong_FromLong(src.long_value); |
| 49 | + } |
| 50 | + }; |
| 51 | +}} // namespace PYBIND11_NAMESPACE::detail |
0 commit comments