Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenFlipper-Free
OpenFlipper
Commits
5c97cd8e
Commit
5c97cd8e
authored
Jan 22, 2019
by
Jan Möbius
Browse files
QString type conversion
parent
ebade444
Changes
1
Show whitespace changes
Inline
Side-by-side
PythonInterpreter/PythonInterpreter.cc
View file @
5c97cd8e
...
...
@@ -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,85 +244,79 @@ 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"
));
PYBIND11_EMBEDDED_MODULE
(
openflipper
,
m
)
{
/**
* 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
();
// 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"
);
if
(
!
PyUnicode_Check
(
source
))
return
false
;
// 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_
;
}));
long
int
size
;
char
*
ptr
=
PyUnicode_AsUTF8AndSize
(
source
,
&
size
);
// Remove this!
// core.def("__del__",
// []() {
// std::cerr << "deleting Core" << std::endl;
// });
if
(
!
ptr
)
{
return
NULL
;
}
// core.def("updateView", &Core::updateView);
// core.def("clearAll", &Core::clearAll);
const
QMetaObject
*
meta
=
core_
->
metaObject
();
/* Now try to convert into a C++ int */
value
=
QString
::
fromUtf8
(
ptr
,
size
);
//std::cerr << "Number of methods:" << meta->methodCount() << std::end;
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return
(
!
PyErr_Occurred
()
);
}
for
(
int
i
=
0
;
i
<
meta
->
methodCount
()
;
++
i
)
{
/**
* 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
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
;
PYBIND11_EMBEDDED_MODULE
(
openflipper
,
m
)
{
QString
function
=
QString
::
fromLatin1
(
method
.
methodSignature
()).
remove
(
QRegExp
(
"[()]"
));
// 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"
);
QByteArray
ba
=
function
.
toLocal8Bit
();
// 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_
;
}));
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
;
});
}
core
.
def
(
"updateView"
,
&
Core
::
updateView
);
core
.
def
(
"clearAll"
,
&
Core
::
clearAll
);
}
}
}
core
.
def
(
"writeVersionNumbers"
,
&
Core
::
writeVersionNumbers
);
// 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"
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment