Commit 5c97cd8e authored by Jan Möbius's avatar Jan Möbius

QString type conversion

parent ebade444
......@@ -230,11 +230,6 @@ void PythonInterpreter::pyError(const char* w)
int add(int i, int j) {
return i + j;
}
//import openflipper
//
//a = openflipper.core()
......@@ -249,9 +244,56 @@ int add(int i, int j) {
//print("CCCCCCCCCc")
void jump(QString _method) {
namespace pybind11 { namespace detail {
template <> struct type_caster<QString> {
public:
/**
* This macro establishes the name 'str' in
* function signatures and declares a local variable
* 'value' of type QVariant
*/
PYBIND11_TYPE_CASTER(QString, _("str"));
/**
* Conversion part 1 (Python->C++): convert a PyObject into a inty
* instance or return false upon failure. The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool ) {
/* Extract PyObject from handle */
PyObject *source = src.ptr();
if (!PyUnicode_Check(source))
return false;
long int size;
char *ptr = PyUnicode_AsUTF8AndSize(source, &size);
if (!ptr) {
return NULL;
}
/* Now try to convert into a C++ int */
value = QString::fromUtf8(ptr, size);
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return ( !PyErr_Occurred() );
}
/**
* Conversion part 2 (C++ -> Python): convert an QVariant instance into
* a Python object. The second and third arguments are used to
* indicate the return value policy and parent object (for
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(QString src, return_value_policy /* policy */, handle /* parent */) {
return (PyUnicode_FromString( src.toUtf8().data()) );
}
};
}} // namespace pybind11::detail
}
PYBIND11_EMBEDDED_MODULE(openflipper, m) {
......@@ -264,70 +306,17 @@ PYBIND11_EMBEDDED_MODULE(openflipper, m) {
// 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("[()]"));
core.def("updateView", &Core::updateView);
core.def("clearAll", &Core::clearAll);
QByteArray ba = function.toLocal8Bit();
std::cerr << "Registering " << ba.data() << " with return type : " << method.returnType() << " which is " << method.typeName() << std::endl;
core.def("writeVersionNumbers", &Core::writeVersionNumbers);
// 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);
//
// m.def("updateView", &Core::updateView);
// m.def("clearAll", &Core::clearAll);
std::cerr << "CorePointer : " << core_ << std::endl;;
}
PYBIND11_EMBEDDED_MODULE(examples, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
std::cerr << "CorePointer : " << core_ << std::endl;;
}
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