diff options
author | Stephen Kelly <steveire@gmail.com> | 2016-04-23 17:24:11 +0200 |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2016-10-31 15:38:08 +0000 |
commit | 64eb5f8e1320feb78c56ec0acb7399ee6085770d (patch) | |
tree | 6ced89f400b6f4208fb5ad2fbd8501a20f1751bd /tests/GenerateSipBindings | |
parent | e052fc95db845a5e0f0b450c8fbffb35f0bbc638 (diff) | |
download | extra-cmake-modules-64eb5f8e1320feb78c56ec0acb7399ee6085770d.tar.gz extra-cmake-modules-64eb5f8e1320feb78c56ec0acb7399ee6085770d.tar.bz2 |
Add the PythonModuleGeneration module
This can be used by KF5 libraries to generate python 2 and 3 bindings.
Diffstat (limited to 'tests/GenerateSipBindings')
-rw-r--r-- | tests/GenerateSipBindings/CMakeLists.txt | 30 | ||||
-rw-r--r-- | tests/GenerateSipBindings/cpplib.cpp | 143 | ||||
-rw-r--r-- | tests/GenerateSipBindings/cpplib.h | 113 | ||||
-rw-r--r-- | tests/GenerateSipBindings/rules_SipTest.py | 17 | ||||
-rw-r--r-- | tests/GenerateSipBindings/testscript.py | 87 |
5 files changed, 390 insertions, 0 deletions
diff --git a/tests/GenerateSipBindings/CMakeLists.txt b/tests/GenerateSipBindings/CMakeLists.txt new file mode 100644 index 00000000..c48b1bce --- /dev/null +++ b/tests/GenerateSipBindings/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.3) + +project(GenerateSipBindings) + +find_package(Qt5Core REQUIRED) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON) + +set(CMAKE_CXX_STANDARD 14) + +add_library(CppLib SHARED cpplib.cpp) +target_link_libraries(CppLib PUBLIC Qt5::Core) +target_compile_features(CppLib PUBLIC cxx_nullptr) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../find-modules) + +find_package(PythonModuleGeneration REQUIRED) + +ecm_generate_python_binding( + TARGET CppLib + PYTHONNAMESPACE PyTest + MODULENAME CppLib + RULES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/rules_SipTest.py" + SIP_DEPENDS + QtCore/QtCoremod.sip + HEADERS + cpplib.h +) diff --git a/tests/GenerateSipBindings/cpplib.cpp b/tests/GenerateSipBindings/cpplib.cpp new file mode 100644 index 00000000..c5b7a5fb --- /dev/null +++ b/tests/GenerateSipBindings/cpplib.cpp @@ -0,0 +1,143 @@ + +#include "cpplib.h" + +MyObject::MyObject(QObject* parent) + : QObject(parent) +{ + +} + +double MyObject::unnamedParameters(int i, double d) +{ + return i * d; +} + +int MyObject::addThree(int input) const +{ + return input + 3; +} + +QList<int> MyObject::addThree(QList<int> input) const +{ + auto output = input; + std::transform(output.begin(), output.end(), + output.begin(), + [](int in) { return in + 3; }); + return output; +} + +const QString MyObject::addThree(const QString& input, const QString& prefix) const +{ + return prefix + input + QStringLiteral("Three"); +} + +int MyObject::findNeedle(QStringList list, QString needle, Qt::MatchFlags flags) const +{ + if (flags & Qt::MatchStartsWith) { + auto it = std::find_if(list.begin(), list.end(), [needle](QString cand) { + return cand.startsWith(needle); + }); + if (it != list.end()) { + return std::distance(list.begin(), it); + } + return -1; + } + return list.indexOf(needle); +} + +int MyObject::qtEnumTest(QFlags<Qt::MatchFlag> flags) +{ + return flags; +} + +int MyObject::localEnumTest(QFlags<LocalEnum> flags) +{ + return flags; +} + +int MyObject::functionParam(std::function<int()> fn) +{ + return fn(); +} + +int MyObject::groups(unsigned int maxCount) const +{ + return maxCount; +} + +class FwdDecl +{ + +}; + +int MyObject::fwdDecl(const FwdDecl&) +{ + return 42; +} + +int MyObject::const_parameters(const int input, QObject* const obj) const +{ + if (obj) return input / 3; + return input / 2; +} + +NonCopyable::NonCopyable() + : mNum(new int(42)) +{ + +} + +NonCopyable::~NonCopyable() +{ + delete mNum; +} + +NonCopyableByMacro::NonCopyableByMacro() +{ + +} + +namespace SomeNS { + +NonCopyableInNS::NonCopyableInNS() + : mNum(new int(42)) +{ + +} + +NonCopyableInNS::~NonCopyableInNS() +{ + delete mNum; +} + +} + +void MyObject::publicSlot1() +{ + Q_EMIT publicSlotCalled(); +} + +void MyObject::publicSlot2() +{ + Q_EMIT publicSlotCalled(); +} + +void MyObject::protectedSlot1() +{ + Q_EMIT protectedSlotCalled(); +} + +void MyObject::protectedSlot2() +{ + Q_EMIT protectedSlotCalled(); +} + +void MyObject::privateSlot1() +{ + Q_EMIT privateSlotCalled(); +} + +void MyObject::privateSlot2() +{ + Q_EMIT privateSlotCalled(); +} diff --git a/tests/GenerateSipBindings/cpplib.h b/tests/GenerateSipBindings/cpplib.h new file mode 100644 index 00000000..f8db75b5 --- /dev/null +++ b/tests/GenerateSipBindings/cpplib.h @@ -0,0 +1,113 @@ + +#pragma once + +#include <QtCore/QObject> +#include <QtCore/QMap> + +#include <functional> + +class FwdDecl; + +class MyObject : public QObject +{ + Q_OBJECT +public: + MyObject(QObject* parent = nullptr); + + enum LocalEnum { + Val1 = 1, + Val2 + }; + Q_DECLARE_FLAGS(LocalEnums, LocalEnum) + + enum { + AnonVal1, + AnonVal2 + }; + + double unnamedParameters(int, double); + + int addThree(int input) const; + QList<int> addThree(QList<int> input) const; + + const QString addThree(const QString& input, const QString& prefix = QStringLiteral("Default")) const; + + int findNeedle(QStringList list, QString needle, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const; + + int qtEnumTest(QFlags<Qt::MatchFlag> flags); + int localEnumTest(QFlags<MyObject::LocalEnum> flags); + + int functionParam(std::function<int()> fn); + int groups(unsigned int maxCount = std::numeric_limits<uint>::max()) const; + + int const_parameters(const int input, QObject* const obj = 0) const; + + int fwdDecl(const FwdDecl& f); + int fwdDeclRef(FwdDecl& f); + +signals: + void publicSlotCalled(); + +Q_SIGNALS: + void privateSlotCalled(); + void protectedSlotCalled(); + +public slots: + void publicSlot1(); + +public Q_SLOTS: + void publicSlot2(); + +protected slots: + void protectedSlot1(); + +protected Q_SLOTS: + void protectedSlot2(); + +private slots: + void privateSlot1(); + +private Q_SLOTS: + void privateSlot2(); +}; + +class NonCopyable +{ +public: + NonCopyable(); + ~NonCopyable(); + +private: + int* const mNum; +}; + + +class NonCopyableByMacro +{ +public: + NonCopyableByMacro(); + +private: + Q_DISABLE_COPY(NonCopyableByMacro) +}; + +class HasPrivateDefaultCtor +{ +public: +private: + HasPrivateDefaultCtor(int param = 0); +}; + +namespace SomeNS { + +class NonCopyableInNS +{ +public: + NonCopyableInNS(); + ~NonCopyableInNS(); + +private: + int* const mNum; +}; + +} diff --git a/tests/GenerateSipBindings/rules_SipTest.py b/tests/GenerateSipBindings/rules_SipTest.py new file mode 100644 index 00000000..e0cd2b9e --- /dev/null +++ b/tests/GenerateSipBindings/rules_SipTest.py @@ -0,0 +1,17 @@ + +import os, sys + +import rules_engine +sys.path.append(os.path.dirname(os.path.dirname(rules_engine.__file__))) +import Qt5Ruleset + +def local_function_rules(): + return [ + ["MyObject", "fwdDecl", ".*", ".*", ".*", rules_engine.function_discard], + ["MyObject", "fwdDeclRef", ".*", ".*", ".*", rules_engine.function_discard], + ] + +class RuleSet(Qt5Ruleset.RuleSet): + def __init__(self): + Qt5Ruleset.RuleSet.__init__(self) + self._fn_db = rules_engine.FunctionRuleDb(lambda: local_function_rules() + Qt5Ruleset.function_rules()) diff --git a/tests/GenerateSipBindings/testscript.py b/tests/GenerateSipBindings/testscript.py new file mode 100644 index 00000000..6f2132a2 --- /dev/null +++ b/tests/GenerateSipBindings/testscript.py @@ -0,0 +1,87 @@ + +import sys + +from PyQt5 import QtCore + +sys.path.append(sys.argv[1]) + +import PyTest.CppLib + +mo = PyTest.CppLib.MyObject() + +assert(mo.addThree(39) == 42) + +assert(mo.addThree([38, 39, 40]) == [41, 42, 43]) + +assert(mo.addThree("SomeString") == "DefaultSomeStringThree") + +assert(mo.findNeedle(["One", "Two", "Three"], "Two") == 1) +assert(mo.findNeedle(["One", "Two", "Three"], "Four") == -1) +assert(mo.findNeedle(["One", "Two", "Three"], "Th") == 2) +assert(mo.findNeedle(["One", "Two", "Three"], "Th", QtCore.Qt.MatchExactly) == -1) + +assert(mo.const_parameters(30) == 15) +assert(mo.const_parameters(30, mo) == 10) + +assert(mo.qtEnumTest(QtCore.Qt.MatchContains | QtCore.Qt.MatchStartsWith) == 3) +assert(mo.localEnumTest(PyTest.CppLib.MyObject.Val2) == 2) + +class Reactor(QtCore.QObject): + def __init__(self, obj): + QtCore.QObject.__init__(self) + self.gotPrivateSlotCalledSignal = False + self.gotProtectedSlotCalledSignal = False + self.gotPublicSlotCalledSignal = False + + obj.privateSlotCalled.connect(self.react_to_privateSlotCalled) + obj.protectedSlotCalled.connect(self.react_to_protectedSlotCalled) + obj.publicSlotCalled.connect(self.react_to_publicSlotCalled) + + def react_to_privateSlotCalled(self): + self.gotPrivateSlotCalledSignal = True + + def react_to_protectedSlotCalled(self): + self.gotProtectedSlotCalledSignal = True + + def react_to_publicSlotCalled(self): + self.gotPublicSlotCalledSignal = True + +class Emitter(QtCore.QObject): + privateTrigger = QtCore.pyqtSignal() + protectedTrigger = QtCore.pyqtSignal() + publicTrigger = QtCore.pyqtSignal() + + def __init__(self, obj): + QtCore.QObject.__init__(self) + self.privateTrigger.connect(obj.privateSlot1) + self.protectedTrigger.connect(obj.protectedSlot1) + self.publicTrigger.connect(obj.publicSlot1) + + def emitSignalForPublic(self): + self.publicTrigger.emit() + + def emitSignalForPrivate(self): + self.privateTrigger.emit() + + def emitSignalForProtected(self): + self.protectedTrigger.emit() + +e = Emitter(mo) + +r = Reactor(mo) + +assert(not r.gotPrivateSlotCalledSignal) +assert(not r.gotProtectedSlotCalledSignal) +assert(not r.gotPublicSlotCalledSignal) + +e.emitSignalForPrivate() + +assert(r.gotPrivateSlotCalledSignal) + +e.emitSignalForProtected() + +assert(r.gotProtectedSlotCalledSignal) + +e.emitSignalForPublic() + +assert(r.gotPublicSlotCalledSignal) |