aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6
diff options
context:
space:
mode:
authorFriedemann Kleint <[email protected]>2025-05-05 13:51:40 +0200
committerFriedemann Kleint <[email protected]>2025-05-09 06:53:45 +0200
commite9cc9edec946d010e1c68c8f1949e274550c5e80 (patch)
treea543ef2be0ea3f7e77687b7addb62fb4d5aacdbf /sources/shiboken6
parenta5c9bc4dce531d56ddb4ae1111361fe0f8b220a5 (diff)
shiboken6: Add code injection for the wrapper declaration
[ChangeLog][shiboken6] It is now possible to inject code into the the wrapper class declaration, which can be used to import base class members via the "using" keyword. Task-number: PYSIDE-3011 Change-Id: Iaf205c16aecd789ea39817999c4eb74e5005c2c3 Reviewed-by: Ece Cinucen <[email protected]> Reviewed-by: Cristian Maureira-Fredes <[email protected]>
Diffstat (limited to 'sources/shiboken6')
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem_enums.h1
-rw-r--r--sources/shiboken6/ApiExtractor/typesystemparser.cpp3
-rw-r--r--sources/shiboken6/doc/typesystem_codeinjection.rst151
-rw-r--r--sources/shiboken6/generator/shiboken/headergenerator.cpp4
4 files changed, 84 insertions, 75 deletions
diff --git a/sources/shiboken6/ApiExtractor/typesystem_enums.h b/sources/shiboken6/ApiExtractor/typesystem_enums.h
index c0c3da1f6..2b876efc4 100644
--- a/sources/shiboken6/ApiExtractor/typesystem_enums.h
+++ b/sources/shiboken6/ApiExtractor/typesystem_enums.h
@@ -36,6 +36,7 @@ enum CodeSnipPosition {
CodeSnipPositionEnd,
CodeSnipPositionDeclaration,
CodeSnipPositionPyOverride,
+ CodeSnipPositionWrapperDeclaration,
CodeSnipPositionAny
};
diff --git a/sources/shiboken6/ApiExtractor/typesystemparser.cpp b/sources/shiboken6/ApiExtractor/typesystemparser.cpp
index 1d747419f..8b2ad08de 100644
--- a/sources/shiboken6/ApiExtractor/typesystemparser.cpp
+++ b/sources/shiboken6/ApiExtractor/typesystemparser.cpp
@@ -341,7 +341,8 @@ ENUM_LOOKUP_BEGIN(TypeSystem::CodeSnipPosition, Qt::CaseInsensitive,
{u"beginning", TypeSystem::CodeSnipPositionBeginning},
{u"end", TypeSystem::CodeSnipPositionEnd},
{u"declaration", TypeSystem::CodeSnipPositionDeclaration},
- {u"override", TypeSystem::CodeSnipPositionPyOverride}
+ {u"override", TypeSystem::CodeSnipPositionPyOverride},
+ {u"wrapper-declaration", TypeSystem::CodeSnipPositionWrapperDeclaration}
};
ENUM_LOOKUP_LINEAR_SEARCH
diff --git a/sources/shiboken6/doc/typesystem_codeinjection.rst b/sources/shiboken6/doc/typesystem_codeinjection.rst
index 03d5f4b16..b5c146523 100644
--- a/sources/shiboken6/doc/typesystem_codeinjection.rst
+++ b/sources/shiboken6/doc/typesystem_codeinjection.rst
@@ -29,80 +29,83 @@ into the **C++ Wrapper** or the **Python Wrapper** (see
The ``position`` attribute specifies the location of the custom code in the
function.
-
-+---------------+------+-----------+--------------------------------------------------------------+
-|Parent Tag |Class |Position |Meaning |
-+===============+======+===========+==============================================================+
-|value-type, |native|beginning |Write to the beginning of a class wrapper ``.cpp`` file, right|
-|object-type | | |after the ``#include`` clauses. A common use would be to write|
-| | | |prototypes for custom functions whose definitions are put on a|
-| | | |``native/end`` code injection. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |Write to the end of a class wrapper ``.cpp`` file. Could be |
-| | | |used to write custom/helper functions definitions for |
-| | | |prototypes declared on ``native/beginning``. |
-| +------+-----------+--------------------------------------------------------------+
-| |target|beginning |Put custom code on the beginning of the wrapper initializer |
-| | | |function (``init_CLASS(PyObject *module)``). This could be |
-| | | |used to manipulate the ``PyCLASS_Type`` structure before |
-| | | |registering it on Python. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |Write the given custom code at the end of the class wrapper |
-| | | |initializer function (``init_CLASS(PyObject *module)``). The |
-| | | |code here will be executed after all the wrapped class |
-| | | |components have been initialized. |
-+---------------+------+-----------+--------------------------------------------------------------+
-|modify-function|native|beginning |Code here is put on the virtual method override of a C++ |
-| | | |wrapper class (the one responsible for passing C++ calls to a |
-| | | |Python override, if there is any), right after the C++ |
-| | | |arguments have been converted but before the Python call. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |This code injection is put in a virtual method override on the|
-| | | |C++ wrapper class, after the call to Python and before |
-| | | |dereferencing the Python method and tuple of arguments. |
-| +------+-----------+--------------------------------------------------------------+
-| |target|beginning |This code is injected on the Python method wrapper |
-| | | |(``PyCLASS_METHOD(...)``), right after the decisor have found |
-| | | |which signature to call and also after the conversion of the |
-| | | |arguments to be used, but before the actual call. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |This code is injected on the Python method wrapper |
-| | | |(``PyCLASS_METHOD(...)``), right after the C++ method call, |
-| | | |but still inside the scope created by the overload for each |
-| | | |signature. |
-| +------+-----------+--------------------------------------------------------------+
-| |shell |declaration|Used only for virtual functions. This code is injected at the |
-| | | |top. |
-| | +-----------+--------------------------------------------------------------+
-| | |override |Used only for virtual functions. The code is injected before |
-| | | |the code calling the Python override. |
-| | +-----------+--------------------------------------------------------------+
-| | |beginning |Used only for virtual functions. The code is injected when the|
-| | | |function does not has a Python implementation, then the code |
-| | | |is inserted before c++ call |
-| | +-----------+--------------------------------------------------------------+
-| | |end |Same as above, but the code is inserted after c++ call |
-+---------------+------+-----------+--------------------------------------------------------------+
-|typesystem |native|beginning |Write code to the beginning of the module ``.cpp`` file, right|
-| | | |after the ``#include`` clauses. This position has a similar |
-| | | |purpose as the ``native/beginning`` position on a wrapper |
-| | | |class ``.cpp`` file, namely write function prototypes, but not|
-| | | |restricted to this use. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |Write code to the end of the module ``.cpp`` file. Usually |
-| | | |implementations for function prototypes inserted at the |
-| | | |beginning of the file with a ``native/beginning`` code |
-| | | |injection. |
-| +------+-----------+--------------------------------------------------------------+
-| |target|beginning |Insert code at the start of the module initialization function|
-| | | |(``initMODULENAME()``), before the calling ``Py_InitModule``. |
-| | +-----------+--------------------------------------------------------------+
-| | |end |Insert code at the end of the module initialization function |
-| | | |(``initMODULENAME()``), but before the checking that emits a |
-| | | |fatal error in case of problems importing the module. |
-| | +-----------+--------------------------------------------------------------+
-| | |declaration|Insert code into module header. |
-+---------------+------+-----------+--------------------------------------------------------------+
++---------------+------+---------------------+--------------------------------------------------------------+
+|Parent Tag |Class |Position |Meaning |
++===============+======+=====================+==============================================================+
+|value-type, |native|beginning |Write to the beginning of a class wrapper ``.cpp`` file, right|
+|object-type | | |after the ``#include`` clauses. A common use would be to write|
+| | | |prototypes for custom functions whose definitions are put on a|
+| | | |``native/end`` code injection. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |Write to the end of a class wrapper ``.cpp`` file. Could be |
+| | | |used to write custom/helper functions definitions for |
+| | | |prototypes declared on ``native/beginning``. |
+| | +---------------------+--------------------------------------------------------------+
+| | | wrapper-declaration |Write into the declaration of the wrapper class, right after |
+| | | |the ``public:`` keyword. This can be used for importing base |
+| | | |class members via ``using``. |
+| +------+---------------------+--------------------------------------------------------------+
+| |target|beginning |Put custom code on the beginning of the wrapper initializer |
+| | | |function (``init_CLASS(PyObject *module)``). This could be |
+| | | |used to manipulate the ``PyCLASS_Type`` structure before |
+| | | |registering it on Python. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |Write the given custom code at the end of the class wrapper |
+| | | |initializer function (``init_CLASS(PyObject *module)``). The |
+| | | |code here will be executed after all the wrapped class |
+| | | |components have been initialized. |
++---------------+------+---------------------+--------------------------------------------------------------+
+|modify-function|native|beginning |Code here is put on the virtual method override of a C++ |
+| | | |wrapper class (the one responsible for passing C++ calls to a |
+| | | |Python override, if there is any), right after the C++ |
+| | | |arguments have been converted but before the Python call. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |This code injection is put in a virtual method override on the|
+| | | |C++ wrapper class, after the call to Python and before |
+| | | |dereferencing the Python method and tuple of arguments. |
+| +------+---------------------+--------------------------------------------------------------+
+| |target|beginning |This code is injected on the Python method wrapper |
+| | | |(``PyCLASS_METHOD(...)``), right after the decisor have found |
+| | | |which signature to call and also after the conversion of the |
+| | | |arguments to be used, but before the actual call. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |This code is injected on the Python method wrapper |
+| | | |(``PyCLASS_METHOD(...)``), right after the C++ method call, |
+| | | |but still inside the scope created by the overload for each |
+| | | |signature. |
+| +------+---------------------+--------------------------------------------------------------+
+| |shell |declaration |Used only for virtual functions. This code is injected at the |
+| | | |top. |
+| | +---------------------+--------------------------------------------------------------+
+| | |override |Used only for virtual functions. The code is injected before |
+| | | |the code calling the Python override. |
+| | +---------------------+--------------------------------------------------------------+
+| | |beginning |Used only for virtual functions. The code is injected when the|
+| | | |function does not has a Python implementation, then the code |
+| | | |is inserted before c++ call |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |Same as above, but the code is inserted after c++ call |
++---------------+------+---------------------+--------------------------------------------------------------+
+|typesystem |native|beginning |Write code to the beginning of the module ``.cpp`` file, right|
+| | | |after the ``#include`` clauses. This position has a similar |
+| | | |purpose as the ``native/beginning`` position on a wrapper |
+| | | |class ``.cpp`` file, namely write function prototypes, but not|
+| | | |restricted to this use. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |Write code to the end of the module ``.cpp`` file. Usually |
+| | | |implementations for function prototypes inserted at the |
+| | | |beginning of the file with a ``native/beginning`` code |
+| | | |injection. |
+| +------+---------------------+--------------------------------------------------------------+
+| |target|beginning |Insert code at the start of the module initialization function|
+| | | |(``initMODULENAME()``), before the calling ``Py_InitModule``. |
+| | +---------------------+--------------------------------------------------------------+
+| | |end |Insert code at the end of the module initialization function |
+| | | |(``initMODULENAME()``), but before the checking that emits a |
+| | | |fatal error in case of problems importing the module. |
+| | +---------------------+--------------------------------------------------------------+
+| | |declaration |Insert code into module header. |
++---------------+------+---------------------+--------------------------------------------------------------+
Anatomy of Code Injection
diff --git a/sources/shiboken6/generator/shiboken/headergenerator.cpp b/sources/shiboken6/generator/shiboken/headergenerator.cpp
index e27a768a5..965d80dad 100644
--- a/sources/shiboken6/generator/shiboken/headergenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/headergenerator.cpp
@@ -240,6 +240,10 @@ void HeaderGenerator::writeWrapperClassDeclaration(TextStream &s,
<< " : public " << metaClass->qualifiedCppName()
<< "\n{\npublic:\n" << indent;
+ writeClassCodeSnips(s, metaClass->typeEntry()->codeSnips(),
+ TypeSystem::CodeSnipPositionWrapperDeclaration,
+ TypeSystem::NativeCode, classContext);
+
writeProtectedEnums(s, classContext);
writeSpecialFunctions(s, wrapperName, classContext);