Commit ebade444 authored by Jan Möbius's avatar Jan Möbius

Use lambda functions to export void slots with no rreturn type

parent cef4e5aa
......@@ -46,9 +46,11 @@
#include "PyLogHook.h"
#include "PythonInterpreter.hh"
#include <QMetaObject>
#include <QMetaMethod>
namespace py = pybind11;
static Core* core_;
......@@ -184,7 +186,9 @@ bool PythonInterpreter::runScript(QString _script) {
try
{
std::cerr << "Now executing script" << std::endl;
std::cerr << "Now executing script:" << std::endl;
std::cerr << _script.toStdString() << std::endl;
py::exec((const char*)_script.toLatin1(), py::globals(), locals);
std::cerr << "Finished successfully" << std::endl;
......@@ -245,24 +249,69 @@ int add(int i, int j) {
//print("CCCCCCCCCc")
void jump(QString _method) {
}
PYBIND11_EMBEDDED_MODULE(openflipper, m) {
py::class_<Core> core(m, "core");
// core
// .def("__init__",
// [](Core &instance) {
// return core_;
// } );
core.def(py::init([]() { return core_; }));
// core.def("__del__",
// +[](const Core&) -> void {
// std::cerr << "deleting Core" << std::endl;
// });
core.def("updateView", &Core::updateView);
core.def("updateView", &Core::updateView);
core.def("clearAll", &Core::clearAll);
// Export our core. Make sure that the c++ worlds core objet is not deleted if
// the python side gets deleted!!
py::class_<Core,std::unique_ptr<Core, py::nodelete>> core(m, "core");
// On the c++ side we will just return the existing core instance
// and prevent the system to recreate a new core as we need
// to work on the existing one.
core.def(py::init([]() { return core_; }));
// Remove this!
// core.def("__del__",
// []() {
// std::cerr << "deleting Core" << std::endl;
// });
// core.def("updateView", &Core::updateView);
// core.def("clearAll", &Core::clearAll);
const QMetaObject* meta = core_->metaObject();
//std::cerr << "Number of methods:" << meta->methodCount() << std::end;
for ( int i = 0 ; i < meta->methodCount() ; ++i) {
QMetaMethod method = meta->method(i);
if ( method.access() == QMetaMethod::Public && method.methodType() == QMetaMethod::Slot) {
// First try to
if (method.parameterCount() == 0) {
std::cerr << QString::fromLatin1(method.methodSignature()).toStdString() << std::endl;
QString function = QString::fromLatin1(method.methodSignature()).remove(QRegExp("[()]"));
QByteArray ba = function.toLocal8Bit();
std::cerr << "Registering " << ba.data() << " with return type : " << method.returnType() << " which is " << method.typeName() << std::endl;
// We are only registering functions with no return type for now!
if ( QString(method.typeName()) == "void") {
core.def(ba.data(), [ method ](const Core& a) {
std::cerr << "Calling " << QString::fromLatin1(method.methodSignature()).toStdString() << std::endl;
method.invoke( core_ , Qt::DirectConnection);
});
} else {
core.def(ba.data(), [ method ](const Core& a) { std::cerr << "No implementation for calling : " << QString::fromLatin1(method.methodSignature()).toStdString() << " due to return type" << std::endl; });
}
}
}
}
// int id = meta->indexOfSlot( QMetaObject::normalizedSignature( _slotSignature ) );
// return ( id != -1 );
std::cerr << "Defined functions for core" << std::endl;
// m.def("targets", &targets);
// m.def("sources", &sources);
// m.def("meshes", &meshes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment