| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 60abf29 commit 86d825f
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,9 +283,8 @@ The binding code also needs a few minor adaptations (highlighted): | |||
| 283 | 283 | PYBIND11_PLUGIN(example) { | |
| 284 | 284 | py::module m("example", "pybind11 example plugin"); | |
| 285 | 285 | ||
| 286 | - py::class_<PyAnimal> animal(m, "Animal"); | ||
| 286 | + py::class_<Animal, std::unique_ptr<Animal>, PyAnimal /* <--- trampoline*/> animal(m, "Animal"); | ||
| 287 | 287 | animal | |
| 288 | - .alias<Animal>() | ||
| 289 | 288 | .def(py::init<>()) | |
| 290 | 289 | .def("go", &Animal::go); | |
| 291 | 290 | ||
@@ -297,10 +296,10 @@ The binding code also needs a few minor adaptations (highlighted): | |||
| 297 | 296 | return m.ptr(); | |
| 298 | 297 | } | |
| 299 | 298 | ||
| 300 | - Importantly, the trampoline helper class is used as the template argument to | ||
| 301 | - :class:`class_`, and a call to :func:`class_::alias` informs the binding | ||
| 302 | - generator that this is merely an alias for the underlying type ``Animal``. | ||
| 303 | - Following this, we are able to define a constructor as usual. | ||
| 299 | + Importantly, pybind11 is made aware of the trampoline trampoline helper class | ||
| 300 | + by specifying it as the *third* template argument to :class:`class_`. The | ||
| 301 | + second argument with the unique pointer is simply the default holder type used | ||
| 302 | + by pybind11. Following this, we are able to define a constructor as usual. | ||
| 304 | 303 | ||
| 305 | 304 | The Python session below shows how to override ``Animal::go`` and invoke it via | |
| 306 | 305 | a virtual method call. | |
@@ -321,12 +320,12 @@ a virtual method call. | |||
| 321 | 320 | ||
| 322 | 321 | .. warning:: | |
| 323 | 322 | ||
| 324 | - Both :func:`PYBIND11_OVERLOAD` and :func:`PYBIND11_OVERLOAD_PURE` are | ||
| 325 | - macros, which means that they can get confused by commas in a template | ||
| 326 | - argument such as ``PYBIND11_OVERLOAD(MyReturnValue<T1, T2>, myFunc)``. In | ||
| 327 | - this case, the preprocessor assumes that the comma indicates the beginnning | ||
| 328 | - of the next parameter. Use a ``typedef`` to bind the template to another | ||
| 329 | - name and use it in the macro to avoid this problem. | ||
| 323 | + The :func:`PYBIND11_OVERLOAD_*` calls are all just macros, which means that | ||
| 324 | + they can get confused by commas in a template argument such as | ||
| 325 | + ``PYBIND11_OVERLOAD(MyReturnValue<T1, T2>, myFunc)``. In this case, the | ||
| 326 | + preprocessor assumes that the comma indicates the beginnning of the next | ||
| 327 | + parameter. Use a ``typedef`` to bind the template to another name and use | ||
| 328 | + it in the macro to avoid this problem. | ||
| 330 | 329 | ||
| 331 | 330 | .. seealso:: | |
| 332 | 331 | ||
@@ -369,9 +368,8 @@ be realized as follows (important changes highlighted): | |||
| 369 | 368 | PYBIND11_PLUGIN(example) { | |
| 370 | 369 | py::module m("example", "pybind11 example plugin"); | |
| 371 | 370 | ||
| 372 | - py::class_<PyAnimal> animal(m, "Animal"); | ||
| 371 | + py::class_<Animal, std::unique_ptr<Animal>, PyAnimal> animal(m, "Animal"); | ||
| 373 | 372 | animal | |
| 374 | - .alias<Animal>() | ||
| 375 | 373 | .def(py::init<>()) | |
| 376 | 374 | .def("go", &Animal::go); | |
| 377 | 375 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,9 +5,11 @@ Changelog | |||
| 5 | 5 | ||
| 6 | 6 | 1.8 (Not yet released) | |
| 7 | 7 | ---------------------- | |
| 8 | + * Redesigned virtual call mechanism and user-facing syntax (breaking change!) | ||
| 8 | 9 | * Prevent implicit conversion of floating point values to integral types in | |
| 9 | 10 | function arguments | |
| 10 | 11 | * Transparent conversion of sparse and dense Eigen data types | |
| 12 | + * ``std::vector<>`` type bindings analogous to Boost.Python's ``indexing_suite`` | ||
| 11 | 13 | * Fixed incorrect default return value policy for functions returning a shared | |
| 12 | 14 | pointer | |
| 13 | 15 | * Don't allow casting a ``None`` value into a C++ lvalue reference | |
@@ -16,10 +18,19 @@ Changelog | |||
| 16 | 18 | * Extended ``str`` type to also work with ``bytes`` instances | |
| 17 | 19 | * Added ``[[noreturn]]`` attribute to ``pybind11_fail()`` to quench some | |
| 18 | 20 | compiler warnings | |
| 21 | + * List function arguments in exception text when the dispatch code cannot find | ||
| 22 | + a matching overload | ||
| 19 | 23 | * Various minor ``iterator`` and ``make_iterator()`` improvements | |
| 24 | + * Transparently support ``__bool__`` on Python 2.x and Python 3.x | ||
| 25 | + * Fixed issue with destructor of unpickled object not being called | ||
| 20 | 26 | * Minor CMake build system improvements on Windows | |
| 21 | 27 | * Many ``mkdoc.py`` improvements (enumerations, template arguments, ``DOC()`` | |
| 22 | 28 | macro accepts more arguments) | |
| 29 | + * New ``pybind11::args`` and ``pybind11::kwargs`` types to create functions which | ||
| 30 | + take an arbitrary number of arguments and keyword arguments | ||
| 31 | + * New syntax to call a Python function from C++ using ``*args`` and ``*kwargs`` | ||
| 32 | + * Added an ``ExtraFlags`` template argument to the NumPy ``array_t<>`` wrapper. This | ||
| 33 | + can be used to disable an enforced cast that may lose precision | ||
| 23 | 34 | * Documentation improvements (pickling support, ``keep_alive``) | |
| 24 | 35 | ||
| 25 | 36 | 1.7 (April 30, 2016) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,15 +82,11 @@ void runExample12Virtual(Example12 *ex) { | |||
| 82 | 82 | } | |
| 83 | 83 | ||
| 84 | 84 | void init_ex12(py::module &m) { | |
| 85 | - /* Important: use the wrapper type as a template | ||
| 86 | - argument to class_<>, but use the original name | ||
| 87 | - to denote the type */ | ||
| 88 | - py::class_<PyExample12>(m, "Example12") | ||
| 89 | - /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */ | ||
| 90 | - .alias<Example12>() | ||
| 85 | + /* Important: indicate the trampoline class PyExample12 using the third | ||
| 86 | + argument to py::class_. The second argument with the unique pointer | ||
| 87 | + is simply the default holder type used by pybind11. */ | ||
| 88 | + py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12") | ||
| 91 | 89 | .def(py::init<int>()) | |
| 92 | - /* Copy constructor (not needed in this case, but should generally be declared in this way) */ | ||
| 93 | - .def(py::init<const PyExample12 &>()) | ||
| 94 | 90 | /* Reference original class in function definitions */ | |
| 95 | 91 | .def("run", &Example12::run) | |
| 96 | 92 | .def("run_bool", &Example12::run_bool) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,8 +42,7 @@ void init_issues(py::module &m) { | |||
| 42 | 42 | } | |
| 43 | 43 | }; | |
| 44 | 44 | ||
| 45 | - py::class_<DispatchIssue> base(m2, "DispatchIssue"); | ||
| 46 | - base.alias<Base>() | ||
| 45 | + py::class_<Base, std::unique_ptr<Base>, DispatchIssue>(m2, "DispatchIssue") | ||
| 47 | 46 | .def(py::init<>()) | |
| 48 | 47 | .def("dispatch", &Base::dispatch); | |
| 49 | 48 | ||
@@ -108,4 +107,28 @@ void init_issues(py::module &m) { | |||
| 108 | 107 | // (no id): don't cast doubles to ints | |
| 109 | 108 | m2.def("expect_float", [](float f) { return f; }); | |
| 110 | 109 | m2.def("expect_int", [](int i) { return i; }); | |
| 110 | + | ||
| 111 | + // (no id): don't invoke Python dispatch code when instantiating C++ | ||
| 112 | + // classes that were not extended on the Python side | ||
| 113 | + struct A { | ||
| 114 | + virtual ~A() {} | ||
| 115 | + virtual void f() { std::cout << "A.f()" << std::endl; } | ||
| 116 | + }; | ||
| 117 | + | ||
| 118 | + struct PyA : A { | ||
| 119 | + PyA() { std::cout << "PyA.PyA()" << std::endl; } | ||
| 120 | + | ||
| 121 | + void f() override { | ||
| 122 | + std::cout << "PyA.f()" << std::endl; | ||
| 123 | + PYBIND11_OVERLOAD(void, A, f); | ||
| 124 | + } | ||
| 125 | + }; | ||
| 126 | + | ||
| 127 | + auto call_f = [](A *a) { a->f(); }; | ||
| 128 | + | ||
| 129 | + pybind11::class_<A, std::unique_ptr<A>, PyA>(m2, "A") | ||
| 130 | + .def(py::init<>()) | ||
| 131 | + .def("f", &A::f); | ||
| 132 | + | ||
| 133 | + m2.def("call_f", call_f); | ||
| 111 | 134 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | from example.issues import iterator_passthrough | |
| 10 | 10 | from example.issues import ElementList, ElementA, print_element | |
| 11 | 11 | from example.issues import expect_float, expect_int | |
| 12 | + from example.issues import A, call_f | ||
| 12 | 13 | import gc | |
| 13 | 14 | ||
| 14 | 15 | print_cchar("const char *") | |
@@ -55,3 +56,19 @@ def dispatch(self): | |||
| 55 | 56 | print("Failed as expected: " + str(e)) | |
| 56 | 57 | ||
| 57 | 58 | print(expect_float(12)) | |
| 59 | + | ||
| 60 | + class B(A): | ||
| 61 | + def __init__(self): | ||
| 62 | + super(B, self).__init__() | ||
| 63 | + | ||
| 64 | + def f(self): | ||
| 65 | + print("In python f()") | ||
| 66 | + | ||
| 67 | + print("C++ version") | ||
| 68 | + a = A() | ||
| 69 | + call_f(a) | ||
| 70 | + | ||
| 71 | + print("Python version") | ||
| 72 | + b = B() | ||
| 73 | + call_f(b) | ||
| 74 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,3 +12,9 @@ Failed as expected: Incompatible function arguments. The following argument type | |||
| 12 | 12 | 1. (int) -> int | |
| 13 | 13 | Invoked with: 5.2 | |
| 14 | 14 | 12.0 | |
| 15 | + C++ version | ||
| 16 | + A.f() | ||
| 17 | + Python version | ||
| 18 | + PyA.PyA() | ||
| 19 | + PyA.f() | ||
| 20 | + In python f() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,6 +65,7 @@ enum op_type : int; | |||
| 65 | 65 | struct undefined_t; | |
| 66 | 66 | template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_; | |
| 67 | 67 | template <typename... Args> struct init; | |
| 68 | + template <typename... Args> struct init_alias; | ||
| 68 | 69 | inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret); | |
| 69 | 70 | ||
| 70 | 71 | /// Internal data structure which holds metadata about a keyword argument | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -503,7 +503,7 @@ class module : public object { | |||
| 503 | 503 | NAMESPACE_BEGIN(detail) | |
| 504 | 504 | /// Generic support for creating new Python heap types | |
| 505 | 505 | class generic_type : public object { | |
| 506 | - template <typename type, typename holder_type> friend class class_; | ||
| 506 | + template <typename type, typename holder_type, typename type_alias> friend class class_; | ||
| 507 | 507 | public: | |
| 508 | 508 | PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) | |
| 509 | 509 | protected: | |
@@ -721,7 +721,7 @@ class generic_type : public object { | |||
| 721 | 721 | }; | |
| 722 | 722 | NAMESPACE_END(detail) | |
| 723 | 723 | ||
| 724 | - template <typename type, typename holder_type = std::unique_ptr<type>> | ||
| 724 | + template <typename type, typename holder_type = std::unique_ptr<type>, typename type_alias = type> | ||
| 725 | 725 | class class_ : public detail::generic_type { | |
| 726 | 726 | public: | |
| 727 | 727 | typedef detail::instance<type, holder_type> instance_type; | |
@@ -743,6 +743,11 @@ class class_ : public detail::generic_type { | |||
| 743 | 743 | detail::process_attributes<Extra...>::init(extra..., &record); | |
| 744 | 744 | ||
| 745 | 745 | detail::generic_type::initialize(&record); | |
| 746 | + | ||
| 747 | + if (!std::is_same<type, type_alias>::value) { | ||
| 748 | + auto &instances = pybind11::detail::get_internals().registered_types_cpp; | ||
| 749 | + instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))]; | ||
| 750 | + } | ||
| 746 | 751 | } | |
| 747 | 752 | ||
| 748 | 753 | template <typename Func, typename... Extra> | |
@@ -780,6 +785,12 @@ class class_ : public detail::generic_type { | |||
| 780 | 785 | return *this; | |
| 781 | 786 | } | |
| 782 | 787 | ||
| 788 | + template <typename... Args, typename... Extra> | ||
| 789 | + class_ &def(const detail::init_alias<Args...> &init, const Extra&... extra) { | ||
| 790 | + init.template execute<type>(*this, extra...); | ||
| 791 | + return *this; | ||
| 792 | + } | ||
| 793 | + | ||
| 783 | 794 | template <typename Func> class_& def_buffer(Func &&func) { | |
| 784 | 795 | struct capture { Func func; }; | |
| 785 | 796 | capture *ptr = new capture { std::forward<Func>(func) }; | |
@@ -856,11 +867,6 @@ class class_ : public detail::generic_type { | |||
| 856 | 867 | return *this; | |
| 857 | 868 | } | |
| 858 | 869 | ||
| 859 | - template <typename target> class_ alias() { | ||
| 860 | - auto &instances = pybind11::detail::get_internals().registered_types_cpp; | ||
| 861 | - instances[std::type_index(typeid(target))] = instances[std::type_index(typeid(type))]; | ||
| 862 | - return *this; | ||
| 863 | - } | ||
| 864 | 870 | private: | |
| 865 | 871 | /// Initialize holder object, variant 1: object derives from enable_shared_from_this | |
| 866 | 872 | template <typename T> | |
@@ -959,9 +965,31 @@ template <typename Type> class enum_ : public class_<Type> { | |||
| 959 | 965 | ||
| 960 | 966 | NAMESPACE_BEGIN(detail) | |
| 961 | 967 | template <typename... Args> struct init { | |
| 962 | - template <typename Base, typename Holder, typename... Extra> void execute(pybind11::class_<Base, Holder> &class_, const Extra&... extra) const { | ||
| 968 | + template <typename Base, typename Holder, typename Alias, typename... Extra, | ||
| 969 | + typename std::enable_if<std::is_same<Base, Alias>::value, int>::type = 0> | ||
| 970 | + void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { | ||
| 963 | 971 | /// Function which calls a specific C++ in-place constructor | |
| 964 | - class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, extra...); | ||
| 972 | + class_.def("__init__", [](Base *self, Args... args) { new (self) Base(args...); }, extra...); | ||
| 973 | + } | ||
| 974 | + | ||
| 975 | + template <typename Base, typename Holder, typename Alias, typename... Extra, | ||
| 976 | + typename std::enable_if<!std::is_same<Base, Alias>::value && | ||
| 977 | + std::is_constructible<Base, Args...>::value, int>::type = 0> | ||
| 978 | + void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { | ||
| 979 | + handle cl_type = class_; | ||
| 980 | + class_.def("__init__", [cl_type](handle self, Args... args) { | ||
| 981 | + if (self.get_type() == cl_type) | ||
| 982 | + new (self.cast<Base *>()) Base(args...); | ||
| 983 | + else | ||
| 984 | + new (self.cast<Alias *>()) Alias(args...); | ||
| 985 | + }, extra...); | ||
| 986 | + } | ||
| 987 | + | ||
| 988 | + template <typename Base, typename Holder, typename Alias, typename... Extra, | ||
| 989 | + typename std::enable_if<!std::is_same<Base, Alias>::value && | ||
| 990 | + !std::is_constructible<Base, Args...>::value, int>::type = 0> | ||
| 991 | + void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { | ||
| 992 | + class_.def("__init__", [](Alias *self, Args... args) { new (self) Alias(args...); }, extra...); | ||
| 965 | 993 | } | |
| 966 | 994 | }; | |
| 967 | 995 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments