Skip to content
GitLab
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
440acd7d
Commit
440acd7d
authored
Feb 14, 2019
by
Jan Möbius
Browse files
Added type conversion for Vector type and added all conversions into separate include file
parent
33d686d8
Changes
2
Hide whitespace changes
Inline
Side-by-side
PythonInterpreter/PythonInterpreter.cc
View file @
440acd7d
...
...
@@ -42,6 +42,7 @@
#include
<pybind11/include/pybind11/pybind11.h>
#include
<pybind11/include/pybind11/embed.h>
#include
<pybind11/include/pybind11/numpy.h>
#include
"PyLogHook.h"
...
...
@@ -51,6 +52,8 @@
#include
<QMetaObject>
#include
<QMetaMethod>
#include
"PythonTypeConversions.hh"
namespace
py
=
pybind11
;
static
Core
*
core_
;
...
...
@@ -246,55 +249,7 @@ void PythonInterpreter::pyError(const char* w)
}
}
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
;
Py_ssize_t
size
;
const
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
)
{
...
...
PythonInterpreter/PythonTypeConversions.hh
0 → 100644
View file @
440acd7d
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of Computer Graphics and Multimedia *
* All rights reserved. *
* www.openflipper.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenFlipper. *
*---------------------------------------------------------------------------*
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice, *
* this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. Neither the name of the copyright holder nor the names of its *
* contributors may be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
\*===========================================================================*/
#include
<pybind11/include/pybind11/pybind11.h>
#include
<pybind11/include/pybind11/numpy.h>
#include
<QString>
namespace
py
=
pybind11
;
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
;
Py_ssize_t
size
;
const
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
namespace
pybind11
{
namespace
detail
{
template
<
>
struct
type_caster
<
Vector
>
{
public:
/**
* This macro establishes the name 'str' in
* function signatures and declares a local variable
* 'value' of type QVariant
*/
PYBIND11_TYPE_CASTER
(
Vector
,
_
(
"Vector"
));
/**
* 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
(
!
PyList_Check
(
source
)
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
"Vector Conversion: Not a list."
);
throw
py
::
error_already_set
();
return
false
;
}
if
(
PyList_Size
(
source
)
==
3
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
"Vector Conversion: Size should be 3!"
);
throw
py
::
error_already_set
();
return
false
;
}
/* Now try to convert into a C++ int */
value
=
Vector
(
PyFloat_AsDouble
(
PyList_GetItem
(
source
,
0
)),
PyFloat_AsDouble
(
PyList_GetItem
(
source
,
1
)),
PyFloat_AsDouble
(
PyList_GetItem
(
source
,
2
)));
/* 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
(
Vector
src
,
return_value_policy
/* policy */
,
handle
/* parent */
)
{
return
py
::
array_t
<
double
>
({
src
[
0
],
src
[
1
],
src
[
2
]});
// return (PyUnicode_FromString( src.toUtf8().data()) );
}
};
}}
// namespace pybind11::detail
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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