From ce1a52c4eb037eac1aab6da39f4b5555906c3842 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 23 Mar 2023 13:33:17 +0100 Subject: Unify the shiboken sample binding example description Merge the description from the shiboken documentation into the newly added example samplebinding.rst file, re-using the existing image. Add the sections from the shiboken documentation to the source files via .rstinc files. Task-number: PYSIDE-1106 Pick-to: 6.5 Change-Id: I22f7302aa10ce2a47283b01315b2d4c9daf9f9ed Reviewed-by: Cristian Maureira-Fredes --- examples/samplebinding/doc/bindings.h.rstinc | 2 ++ examples/samplebinding/doc/bindings.xml.rstinc | 32 ++++++++++++++++++++++ examples/samplebinding/doc/samplebinding.pyproject | 1 + examples/samplebinding/doc/samplebinding.rst | 17 ++++++++++-- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 examples/samplebinding/doc/bindings.h.rstinc create mode 100644 examples/samplebinding/doc/bindings.xml.rstinc (limited to 'examples/samplebinding') diff --git a/examples/samplebinding/doc/bindings.h.rstinc b/examples/samplebinding/doc/bindings.h.rstinc new file mode 100644 index 000000000..e2a0b6fef --- /dev/null +++ b/examples/samplebinding/doc/bindings.h.rstinc @@ -0,0 +1,2 @@ +The Shiboken generator needs a header file that includes +the types we are interested in: diff --git a/examples/samplebinding/doc/bindings.xml.rstinc b/examples/samplebinding/doc/bindings.xml.rstinc new file mode 100644 index 000000000..8d078ce7f --- /dev/null +++ b/examples/samplebinding/doc/bindings.xml.rstinc @@ -0,0 +1,32 @@ +Shiboken requires an XML-based typesystem file that defines the +relationship between C++ and Python types. + +It declares the two aforementioned classes. One of them as an +“object-type” and the other as a “value-type”. The main difference is that +object-types are passed around in generated code as pointers, whereas +value-types are copied (value semantics). + +By specifying the names of these classes in the typesystem file, Shiboken +automatically tries to generate bindings for all methods of those +classes. You need not mention all the methods manually in the XML file, unless +you want to modify them. + +Object ownership rules +---------------------- + +Shiboken doesn't know if Python or C++ are responsible for freeing the C++ +objects that were allocated in the Python code, and assuming this might lead to +errors. There can be cases where Python should release the C++ memory when the +reference count of the Python object becomes zero, but it should never delete +the underlying C++ object just from assuming that it will not be deleted by +underlying C++ library, or if it's maybe parented to another object (like +QWidgets). + +In our case, the :code:`clone()` method is only called inside the C++ library, +and we assume that the C++ code takes care of releasing the cloned object. + +As for :code:`addIcecreamFlavor()`, we know that a :code:`Truck` owns the +:code:`Icecream` object, and will remove it once the :code:`Truck` is +destroyed. That's why the ownership is set to “c++” in the typesystem file, +so that the C++ objects are not deleted when the corresponding Python names +go out of scope. diff --git a/examples/samplebinding/doc/samplebinding.pyproject b/examples/samplebinding/doc/samplebinding.pyproject index 82c485a09..b0786355f 100644 --- a/examples/samplebinding/doc/samplebinding.pyproject +++ b/examples/samplebinding/doc/samplebinding.pyproject @@ -1,5 +1,6 @@ { "files": ["../bindings.h", + "../bindings.xml", "../icecream.cpp", "../icecream.h", "../macros.h", diff --git a/examples/samplebinding/doc/samplebinding.rst b/examples/samplebinding/doc/samplebinding.rst index 8d74be281..e96e99df4 100644 --- a/examples/samplebinding/doc/samplebinding.rst +++ b/examples/samplebinding/doc/samplebinding.rst @@ -54,8 +54,8 @@ done by specifying a special XML file called a typesystem file. In the typesystem file you specify things like: - * which C++ classes should have bindings (Icecream) and what kind of - semantics (value / object) + * Which C++ classes should have bindings (Icecream, Truck) and with what + kind of semantics (value / object) * Ownership rules (who deletes the C++ objects, C++ or Python) @@ -186,12 +186,25 @@ On Windows: ninja install cd .. +Use the Python module ++++++++++++++++++++++ + The final example can then be run by: .. code-block:: bash python main.py +In the ``main.py`` script, two types are derived from :code:`Icecream` for +different “flavors” after importing the classes from the :code:`Universe` +module. Then, a :code:`truck` is created to deliver some regular flavored +Icecreams and two special ones. + +If the delivery fails, a new :code:`truck` is created with the old flavors +copied over, and a new *magical* flavor that will surely satisfy all customers. + +Try running it to see if the ice creams are delivered. + Windows troubleshooting +++++++++++++++++++++++ -- cgit v1.2.3