diff options
Diffstat (limited to 'examples/samplebinding')
-rw-r--r-- | examples/samplebinding/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/samplebinding/doc/bindings.h.rstinc | 2 | ||||
-rw-r--r-- | examples/samplebinding/doc/bindings.xml.rstinc | 31 | ||||
-rw-r--r-- | examples/samplebinding/doc/samplebinding.pyproject | 1 | ||||
-rw-r--r-- | examples/samplebinding/doc/samplebinding.rst | 36 |
5 files changed, 65 insertions, 10 deletions
diff --git a/examples/samplebinding/CMakeLists.txt b/examples/samplebinding/CMakeLists.txt index 896b3610c..4807904c1 100644 --- a/examples/samplebinding/CMakeLists.txt +++ b/examples/samplebinding/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + cmake_minimum_required(VERSION 3.18) cmake_policy(VERSION 3.18) @@ -132,7 +135,7 @@ target_compile_definitions(${sample_library} PRIVATE BINDINGS_BUILD) # Set up the options to pass to shiboken. set(shiboken_options --generator-set=shiboken --enable-parent-ctor-heuristic - --enable-return-value-heuristic --use-isnull-as-nb_nonzero + --enable-return-value-heuristic --use-isnull-as-nb-bool --avoid-protected-hack -I${CMAKE_SOURCE_DIR} -T${CMAKE_SOURCE_DIR} 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..60b9b1a48 --- /dev/null +++ b/examples/samplebinding/doc/bindings.xml.rstinc @@ -0,0 +1,31 @@ +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..f28798d52 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) @@ -165,33 +165,51 @@ Using CMake You can build and run this example by executing the following commands (slightly adapted to your file system layout) in a terminal: -macOS/Linux: +Run CMake on macOS/Linux: .. code-block:: bash cd ~/pyside-setup/examples/samplebinding + mkdir build + cd build + cmake .. -B. -G Ninja -DCMAKE_BUILD_TYPE=Release -On Windows: +Run CMake on Windows: .. code-block:: bash cd C:\pyside-setup\examples\samplebinding + mkdir build + cd build + cmake .. -B. -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=cl.exe + +To build: .. code-block:: bash - mkdir build - cd build - cmake -H.. -B. -G Ninja -DCMAKE_BUILD_TYPE=Release ninja 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 +++++++++++++++++++++++ @@ -207,13 +225,13 @@ passing the compiler on the command line: .. code-block:: bash - cmake -H.. -B. -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe + cmake -S.. -B. -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe or by using the -G option: .. code-block:: bash - cmake -H.. -B. -G "Visual Studio 14 Win64" + cmake -S.. -B. -G "Visual Studio 14 Win64" If the ``-G "Visual Studio 14 Win64"`` option is used, a ``sln`` file will be generated, and can be used with ``MSBuild`` |