SWIG C++与回调的绑定
我正在为一些 C++ 代码编写一些 SWIG/Python 绑定。这是针对所谓的 Kinect Accidental API,我有电机和 LED 功能工作。解析和填充 RGB 和深度缓冲区的 Listener 类的回调似乎没有从 SWIG 调用。数据捕获线程显然启动并开始占用 CPU,但回调中没有调试线通过。填充数据缓冲区并从 python 轻松访问它们的更好方法是什么?
class KinectListener
{
public:
virtual ~KinectListener(){};
virtual void KinectDisconnected(Kinect *K) {};
virtual void DepthReceived(Kinect *K) {};
virtual void ColorReceived(Kinect *K) {};
virtual void AudioReceived(Kinect *K) {};
};
这是带有虚方法的侦听器类,该类的 Python 包装版本可以用于继承 C++ 类的侦听器吗?我在 C++ 中添加了一个最小的侦听器,现在剩下的工作是使用类型映射有效地访问数组。目前我正在使用这个幼稚的类型映射
%typemap(out) unsigned short [ANY] {
int i;
$result = PyList_New($1_dim0);
for (i = 0; i < $1_dim0; i++) {
PyObject *o = PyInt_FromLong((long)$1[i]);
PyList_SetItem($result,i,o);
}
}
更好的选择?
I am writing some SWIG/Python bindings for some C++ code. This is for what is called the Kinect Accidental API, I have the motor and led functions working. The callbacks to the Listener class which parse and populate the RGB and Depth buffers do not seem to get called from SWIG. The data capture threads evidently start up and start hogging the CPU, but no debug lines from the callback come through. What would be better way to populate data buffers and easily access them from python ?
class KinectListener
{
public:
virtual ~KinectListener(){};
virtual void KinectDisconnected(Kinect *K) {};
virtual void DepthReceived(Kinect *K) {};
virtual void ColorReceived(Kinect *K) {};
virtual void AudioReceived(Kinect *K) {};
};
Here is the listener class with the virtual methods, can the Python wrapped version of this class be used to inherit listeners for the c++ class ? I added a minimal listener in C++ and now the remaining work is to access the arrays efficiently with typemaps. Currently I am using this naive typemap
%typemap(out) unsigned short [ANY] {
int i;
$result = PyList_New($1_dim0);
for (i = 0; i < $1_dim0; i++) {
PyObject *o = PyInt_FromLong((long)$1[i]);
PyList_SetItem($result,i,o);
}
}
Better options ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
巧合的是,我目前正在研究 SWIG 的回调。
SWIG 2.0 文档是这样描述的:
我计划用手写的 JNI 编写一个 C 回调来调用 Java。如果还有其他方法,我也很想听听。
By coincidence, I happen to be looking into callbacks with SWIG at the moment.
The SWIG 2.0 documentation says this:
I'm planning to write a C callback with hand-written JNI to call into Java. If there's another way, I would also love to hear it.
有一种方法可以使用导演功能。
为您的 KinectListener 代理启用它,一行代码:
然后您可以在 python 代码中继承 KinectListener 类并定义您的函数。
There is a way using the directors feature.
Enable it for your KinectListener proxy, one line of code :
Then you can inherit from KinectListener class in python code and define your functions.