Using Code Templates¶
template¶
The template
node registers a template that can be used to avoid
duplicate code when extending the generated code, and it is a child of the
typesystem node.
<typesystem>
<template name="my_template">
// the code
</template>
</typesystem>
Use the insert-template
node to insert the template code (identified
by the template’s name
attribute) into the generated code base.
insert-template¶
The insert-template
node includes the code template identified by the
name attribute, and it can be a child of the inject-code,
conversion-rule or template nodes.
<inject-code class="target" position="beginning">
<insert-template name="my_template" />
</inject-code>
Use the replace
node to modify the template code.
replace¶
The replace
node allows you to modify template code before inserting it into
the generated code, and it can be a child of the insert-template node.
<insert-template name="my_template">
<replace from="..." to="..." />
</insert-template>
This node will replace the attribute from
with the value pointed by
to
.
Predefined Templates¶
There are a number of XML templates for conversion rules for STL and Qt types built into shiboken.
Templates for primitive-type:
Name |
Description |
|
Convert a PyLong to a C++ type |
Templates for container-type:
Some container types are built-in. In case they need to explicitly specified, the following templates can be used:
|
Convert a PySequence to a C++ pair (std::pair/QPair) |
|
Convert a C++ pair (std::pair/QPair) to a PyTuple |
|
Convert a C++ sequential container to a PyList |
|
Convert a C++ sequential container to a PySet |
|
Convert an iterable Python type to a C++ sequential container (STL/Qt) |
|
Convert an iterable Python type to a C++ sequential container supporting reserve() |
|
Convert an iterable Python type to a fixed-size array (std::array, std::span) |
|
Convert a PySequence to a set-type C++ container (std::set/QSet) |
|
Convert a std::map/std::unordered_map to a PyDict |
|
Convert a QMap/QHash to a PyDict |
|
Convert a PyDict to a std::map/std::unordered_map |
|
Convert a PyDict to a QMap/QHash |
|
Convert a std::multimap to a PyDict of value lists |
|
Convert a QMultiMap to a PyDict of value lists |
|
Convert a std::unordered_multimap to a PyDict of value lists |
|
Convert a QMultiHash to a PyDict of value lists |
|
Convert a PyDict of value lists to std::multimap/std::unordered_multimap |
|
Convert a PyDict of value lists to QMultiMap/QMultiHash |
An entry for the type std::list
using these templates looks like:
<container-type name="std::list" type="list">
<include file-name="list" location="global"/>
<conversion-rule>
<native-to-target>
<insert-template name="shiboken_conversion_cppsequence_to_pylist"/>
</native-to-target>
<target-to-native>
<add-conversion type="PySequence">
<insert-template name="shiboken_conversion_pyiterable_to_cppsequentialcontainer"/>
</add-conversion>
</target-to-native>
</conversion-rule>
</container-type>